Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring request mapping wildcard exceptions

Can I put /** wildcard in a middle of request mapping such as: "/some/resource/**/somthing"

In Spring 3 I can do this

@RequestMapping("/some/resource/**")

to map

/some/resource/A  -> ControllerMethod1
/some/resource/A/B -> ControllerMethod1
/some/resource/A/B/C/D/E/F -> ControllerMethod1

for any number of paths parts

However this mapping is too greedy and will not allow me to map a sub URL @RequestMapping("/some/resource/**/somthing") to another controller such as

/some/resource/A/somthing  -> ControllerMethod2
/some/resource/A/B/somthing -> ControllerMethod2
/some/resource/A/B/C/D/E/F/somthing -> ControllerMethod2

How can i do this?

like image 788
Rob McFeely Avatar asked Nov 14 '13 15:11

Rob McFeely


People also ask

What is @RequestMapping annotation in spring?

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. Today we will look into various usage of this annotation with example and other annotations @PathVariable and @RequestParam .

Can two controllers have same request mapping?

You cannot. A URL can only be mapped to a single controller. It has to be unique.

What is true about @RequestMapping annotation in Spring MVC?

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.

Which of the following is the method where RequestMapping is used with RequestParam?

The @RequestParam annotation is used with @RequestMapping to bind a web request parameter to the parameter of the handler method.


1 Answers

I thinks it's not possible to use that ant style in url mapping as you require, because it will stop on the next path separator character '/'.

I would suggest you to try 16.3.2.2. URI Template Patterns with Regular Expressions in order to map just the last part of the request (haven't tried this approach yet).

Also you can match the rest of the request using PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, and apply some expression there. Check this post.

Otherwise you should use request parameters to match that condition 16.3.2.6. Request Parameters and Header Values.

You can narrow request matching through request parameter conditions such as "myParam", "!myParam", or "myParam=myValue". The first two test for request parameter presense/absence and the third for a specific parameter value. Here is an example with a request parameter value condition.

In this case you will map something like that using params

@RequestMapping(value = {"/some/resource/**"},  params="somthing")

or use the annotation request parameter with not required attribute in method signature:

public void test(@RequestParam(value = "somthing", required=false) String str) {
like image 81
jmventar Avatar answered Sep 25 '22 03:09

jmventar