Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Spring's @RequestMapping with wildcards

This is similar to this question, but I am still confused about my situation. I want to map this ant-style pattern to a controller method:

/results/**

That is, I want any URL like www.hostname.com/MyServlet/results/123/abc/456/def/ to go to this method. I have:

<servlet-mapping>
    <servlet-name>MyServlet</servlet-name>
    <url-pattern>/results/*</url-pattern>
</servlet-mapping>

and:

@RequestMapping(value="/**", method=RequestMethod.GET)
public ModelAndView handleRequest() {...}

This works to guide the request to my method, but leads me to several questions:

  1. What if I add another servlet mapping, like <url-pattern>/another-mapping/*</url-pattern>??? It will also get mapped to that method! How can I separate the two?
  2. Why does the url-pattern /results/* work, whereas /results/** doesn't? According to ant path styles, ** means to include nested / characters, whereas * stops at the next /. So, it should only successfully map a URL like /results/123, bot NOT /results/123/abc/. Right?
like image 829
Tony R Avatar asked May 10 '11 18:05

Tony R


People also ask

Can we use @RequestMapping at class level?

The @RequestMapping annotation can be applied to class-level and/or method-level in a controller. The class-level annotation maps a specific request path or pattern onto a controller. You can then apply additional method-level annotations to make mappings more specific to handler methods.

What is the use of @RequestMapping in spring boot?

RequestMapping annotation is used to map web requests onto specific handler classes and/or handler methods. @RequestMapping can be applied to the controller class as well as methods.

What will happen if handler methods @RequestMapping?

In the code snippet above, the method element of the @RequestMapping annotations indicates the HTTP method type of the HTTP request. All the handler methods will handle requests coming to the same URL ( /home), but will depend on the HTTP method being used.

Is @RequestMapping mandatory?

A @RequestMapping on the class level is not required. Without it, all paths are simply absolute, and not relative.


1 Answers

Perhaps in your servlet mapping you would want to direct all traffic to '/*'. This way, you can distinguish in your controller what method to use with different @RequestMapping's.

<servlet-mapping>
  <servlet-name>MyServlet</servlet-name>
  <url-pattern>/*</url-pattern>
</servlet-mapping>

and

@RequestMapping(value="/results/**", method=RequestMethod.GET)
public ModelAndView handleResults() {...}

@RequestMapping(value="/another-mapping/**", method=RequestMethod.GET)
public ModelAndView handleAnotherMapping() {...}

Hopefully the above will help with number 1. As far as number 2 goes, I do not think that you can use 'ant-style' pattern matchers (specifically **) in your web.xml domain descriptor.

like image 130
nicholas.hauschild Avatar answered Sep 26 '22 06:09

nicholas.hauschild