Spring pattern matching request

Is it possible to put / ** a wildcard in the middle of the query display, for example: "/ some / resource / ** / somthing"

In Spring 3, I can do it

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

to display

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

for any number of parts of the path

However, this mapping is too greedy and will not allow me to map the additional @RequestMapping("/some/resource/**/somthing") 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?

+6
source share
1 answer

I think it is not possible to use this ant style in matching URLs as needed, because it will dwell on the next path separator character / .

I suggest you try 16.3.2.2. Regular expression URI patterns to display only the last part of the query (not tried this approach yet).

You can also match the rest of the query with PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE and apply some kind of expression there. Mark this post.

Otherwise, you must use query parameters to meet this condition 16.3.2.6. Request parameters and header values .

You can narrow down the matching of queries by using query parameter conditions such as "myParam", "! MyParam" or "myParam = myValue". The first two tests are for determining / the absence of query parameters, and the third is for a certain parameter value. Here is an example with a condition for the value of a query parameter.

In this case, you will display something like this using the options

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

or use the annotation request parameter with an optional attribute in the method signature:

 public void test(@RequestParam(value = "somthing", required=false) String str) { 
+7
source

Source: https://habr.com/ru/post/958077/


All Articles