Reputation: 23002
When I map multiple values to @RequestMapping(like Multiple Spring @RequestMapping annotations), can I get the requested value(URL)?
Like this:
@RequestMapping(value={"/center", "/left"}, method=RequestMethod.GET)
public String getCenter(Model model) throws Exception {
String requestedValue = getRequestedValue(); // I want this.
// I want to do something like this with requested value.
String result;
if (requestedValue.equals("center")
result = "center";
else if (requestedValue.equals("left")
result = "left";
return result;
}
Upvotes: 15
Views: 32952
Reputation: 120791
You can have the Request (HttpServletRequest
) itself as an parameter of the handler method. So you can then inspect the request url to get the "value".
@RequestMapping(value={"/center", "/left"}, method=RequestMethod.GET)
public String getCenter(Model model, HttpServletRequest request) throws Exception {
String whatYouCallValue = request.getServletPath();
....
Javadoc: https://docs.oracle.com/javaee/7/api/javax/servlet/http/HttpServletRequest.html#getServletPath--
Btw: if I understand you right, you want to have different urls, not different values.
Upvotes: 19
Reputation: 12421
From Spring 3.1.0, you can use ServletUriComponentsBuilder
@RequestMapping(value={"/center", "/left"}, method=RequestMethod.GET)
public String getCenter(Model model) throws Exception {
UriComponentsBuilder builder = ServletUriComponentsBuilder.fromCurrentRequest();
String requestedValue = builder.buildAndExpand().getPath(); // I want this.
System.out.println(requestedValue);
// I want to do something like this with requested value.
String result="fail";
if (requestedValue.equals("center"))
result = "center";
else if (requestedValue.equals("left"))
result = "left";
return result;
}
Upvotes: 4
Reputation: 31
Following regex will make your method to be executed only for the urls /center
and /left
. And you can get the value with @PathVariable
annotation.
@GetMapping("/{path:^center$|^left$}")
public ResponseEntity<?> whatIsThePath(@PathVariable String path){
// path is either "center" or "left"
}
Upvotes: 0
Reputation: 1
Addition to the best answer @Hugh_Lee: This method will work for all not mapped requests. If you want to use this method just for two (or several) cases only, e.g. "/center" and "/left", you may do following. Rename "center" to "positionCenter", "left" to "positionLeft" (or add another common word). So the code would be like this:
@RequestMapping(value={"/{path:position+[A-Za-z-]+}"}, method=RequestMethod.GET)
public String getCenter(@PathVariable String path) throws Exception {
// "path" is what I want
}
Upvotes: 0
Reputation: 23002
From Spring 3.1.0, you can use URI Template Patterns with Regular Expressions.
@RequestMapping(value={"/{path:[a-z-]+}"}, method=RequestMethod.GET)
public String getCenter(@PathVariable String path) throws Exception {
// "path" is what I want
}
Upvotes: 6
Reputation: 21000
Use RequestParam annotation. You can also add a parameter of type HttpServletRequest to your method and then getParameters from that.
Upvotes: 1