I have the following in Spring
@RequestMapping("/hello")
However Spring automatically adds mappings for /hello/ as well as /hello.*. How do I do an exact URL match?
Only /hello should work, anything else should 404
One of the most important annotations in spring is the @RequestMapping Annotation which is used to map HTTP requests to handler methods of MVC and REST controllers. In Spring MVC applications, the DispatcherServlet (Front Controller) is responsible for routing incoming HTTP requests to handler methods of controllers.
@RequestMapping is used at the class level while @GetMapping is used to connect the methods. This is also an important Spring MVC interview question to knowing how and when to use both RequestMapping and GetMapping is crucial for Java developers.
Quite right, you can only use @RequestMapping on @Controller annotated classes. From the javadoc of the @Controller class: Base Controller interface, representing a component that receives HttpServletRequest and HttpServletResponse instances just like a HttpServlet [...]
What is the default request method type for the @RequestMapping ? @RequestMapping(value = "addGoal") public String addGoal(Model model) {...} The default is none, as indicated by the javadoc. You narrow the mapping by adding one or several methods (also indicated by the javadoc).
Turning off suffix matching (useSuffixPatternMatch
) on RequestMappingHandlerMapping
will solve your problem, but doing so is actually not so easy, if you use <mvc:annotation-driven/>
in your configuration (instead of manually wiring all the necessary infrastructure beans). In this case defining an additional bean of type RequestMappingHandlerMapping
won't have any effect.
You have two options:
Remove <mvc:annotation-driven/>
expanding it to an equivalent set of bean definitions where you can apply the useSuffixPatternMatch
setting.
Keep <mvc:annotation-driven/>
as it is, and use a much easier workaround described here: https://jira.springsource.org/browse/SPR-9371. This basically adds a BeanPostProcessor
which retrieves the RequestMappingHandlerMapping
bean created by the mvc namespace, and sets the above mentioned flag.
There is also another ticket requesting that it should be much easier to customize the RequestMappingHandlerMapping
created by the mvc namespace without applying hacks like above. You can consider voting on this ticket.
If you are using version 3.1/3.2, then you can try this without commenting or deleting
public class MvcConfigurationPostProcessor implements BeanPostProcessor, PriorityOrdered {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof RequestMappingHandlerMapping) {
((RequestMappingHandlerMapping) bean).setUseSuffixPatternMatch(false);
}
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
@Override
public int getOrder() {
return PriorityOrdered.HIGHEST_PRECEDENCE;
}
}
use the above post processor in xml config.
<bean class="com.beanprocbug.melia.MvcConfigurationPostProcessor" />
<mvc:annotation-driven />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With