I have a fairly simple task that I want to accomplish, but can't seem to find information for Spring MVC routing about it. I have a very simple controller that routes a path to a view:
@Controller
@RequestMapping(value = "/help")
public class HelpController {
private static final String HELP = "help";
@RequestMapping(method = RequestMethod.GET)
public String help(Model model, Locale locale) {
model.addAttribute("locale", locale);
return HELP;
}
}
I would like to throw a 404 if http://mysite.com/help.some.extension.is.entered, but Spring seems to resolve the example to /help. The javadoc says that the @RequestMapping annotation is just a servlet URI mapping, but I thought /help means it needs to be an exact match. Any clarification would be appreciated.
You can mention it in the @RequestMapping annotation
it is same as Servlet URL pattern only.
With Spring 3.0.X You can use the useDefaultSuffixPattern property.
You will need to remove
</mvc:annotation-driven>
Refer URL Pattern Restricting in SPRING MVC
For Spring 4 it's pretty easy to solve:
So you still can use
mvc:annotation-driven
for your config.The best way I can think of is to configure your RequestMappingHandlerMapping explicitly to not consider suffixpaths, this way:
However, if you have configured your Spring MVC using
mvc:annotation-driven
, this will not work, you will have to expand out the entire handlerAdapter definition, which is not that difficult to do, along these lines(this is not complete, you can look throughorg.springframework.web.servlet.config.AnnotationDrivenBeanDefinitionParser
for the entire definition):