8

Is there a way to tell Spring to map request to different method by the type of path variable, if they are in the same place of the uri?
For example,

@RequestMapping("/path/{foo}")  

@RequestMapping("/path/{id}")

if foo is supposed to be string, id is int, is it possible to map correctly instead of looking into the request URI?

3
  • 2
    It is not possible to do this without a considerable amount of configuration. Can you give an example of the purpose of doing this? Commented Sep 24, 2013 at 17:16
  • People would like to change the format of some api into something like the first one, while I already have something like the second. Commented Sep 24, 2013 at 17:27
  • 2
    It's not worth the hassle. Change your handler methods instead. Commented Sep 24, 2013 at 17:33

1 Answer 1

12

According to the spring docs it is possible to use regex for path variables, here's the example from the docs:

@RequestMapping("/spring-web/{symbolicName:[a-z-]+}-{version:\\d\\.\\d\\.\\d}{extension:\\.[a-z]+}")
  public void handle(@PathVariable String version, @PathVariable String extension) {
    // ...
  }
}

(example taken from http://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/mvc.html#mvc-ann-requestmapping-patterns )

Judging from that, it should be possible to write something like this for your situation:

@RequestMapping("/path/{foo:[a-z]+}")  

@RequestMapping("/path/{id:[0-9]+}")
Sign up to request clarification or add additional context in comments.

1 Comment

Use @RequestMapping("/path/{foo:[A-Za-z]+}"), to get also uppercase for String.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.