Skip to content

Instantly share code, notes, and snippets.

@MikeN123
Created February 7, 2014 22:50
Show Gist options
  • Save MikeN123/8873622 to your computer and use it in GitHub Desktop.
Save MikeN123/8873622 to your computer and use it in GitHub Desktop.
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.core.PriorityOrdered;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
/**
* This postprocessor sets {@code useSuffixPatternMatch} and {@code useTrailingSlashMatch} to {@code false}, so we have
* exact URL matching. This prevents 404's, wrong base URL's, and automatic matching on extensions (.json), which is
* useful when using {@code PathParam}s.
*
* @author David Melia
* @author Erik van Paassen
* @see <a href="https://jira.springsource.org/browse/SPR-9371">SPR-9371: Cannot amend properties in
* RequestMappingHandlerMapping (e.g. useSuffixPatternMatch)...</a>
* @see <a href="https://jira.springsource.org/browse/SPR-10163">SPR-10163: Make it super-easy to configure the
* RequestMappingHandlerMapping via XML</a>
*/
public class MvcConfigurationPostProcessor implements BeanPostProcessor, PriorityOrdered {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof RequestMappingHandlerMapping) {
RequestMappingHandlerMapping requestMappingHandlerMapping = (RequestMappingHandlerMapping) bean;
requestMappingHandlerMapping.setUseSuffixPatternMatch(false);
requestMappingHandlerMapping.setUseTrailingSlashMatch(false);
// URL decode after request mapping, not before.
requestMappingHandlerMapping.setUrlDecode(false);
// Workaround to make the previous fix work. See https://jira.springsource.org/browse/SPR-11101.
requestMappingHandlerMapping.setAlwaysUseFullPath(true);
}
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
@Override
public int getOrder() {
return PriorityOrdered.HIGHEST_PRECEDENCE;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment