Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exact RequestMapping with Spring

Tags:

java

spring

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

like image 859
bufferoverflow Avatar asked Apr 09 '13 20:04

bufferoverflow


People also ask

What does @RequestMapping do in Spring?

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.

What is the difference between @RequestMapping and @GetMapping?

@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.

Can we use @RequestMapping with @component?

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 method for RequestMapping?

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).


2 Answers

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:

  1. Remove <mvc:annotation-driven/> expanding it to an equivalent set of bean definitions where you can apply the useSuffixPatternMatch setting.

  2. 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.

like image 88
zagyi Avatar answered Nov 15 '22 16:11

zagyi


If you are using version 3.1/3.2, then you can try this without commenting or deleting

Write a post processor

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 />
like image 22
Praveen Rajanna Avatar answered Nov 15 '22 17:11

Praveen Rajanna