hsluo
hsluo

Reputation: 1702

Spring RequestMapping: differentiate PathVariable with different types

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?

Upvotes: 8

Views: 6403

Answers (1)

Krešimir Nesek
Krešimir Nesek

Reputation: 5492

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]+}")

Upvotes: 12

Related Questions