15

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;
}
Community
  • 1
  • 1
Sanghyun Lee
  • 21,644
  • 19
  • 100
  • 126
  • As a newbie, I'm wondering is how "Model model" value is received from GET request, because Get mapping accepts only Url and some headers from UI. Can you tell me the reaon? – EnthuCoder Jun 10 '23 at 18:47

6 Answers6

19

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.

Monil Soni
  • 353
  • 2
  • 6
Ralph
  • 118,862
  • 56
  • 287
  • 383
  • Thanks it works. I called it values because the name of property is value, but now I think it could be confusing. I'll correct some. – Sanghyun Lee Sep 29 '11 at 07:28
6

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
}
Sanghyun Lee
  • 21,644
  • 19
  • 100
  • 126
4

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;
    }
Zeeshan
  • 11,851
  • 21
  • 73
  • 98
  • This is exactly what I want. i don't want to mess up the API mapping definition with something that is internal. – toddcscar Jul 31 '19 at 21:29
  • `ServletUriComponentsBuilder.fromRequest(HttpServletRequest request)` worked fine in interceptor. – Bu Saeed Apr 28 '21 at 23:31
1

Use RequestParam annotation. You can also add a parameter of type HttpServletRequest to your method and then getParameters from that.

gkamal
  • 20,777
  • 4
  • 60
  • 57
  • It seams that Sangdol mean different urls, not http values. (So his term "values" is a bit confusing) – Ralph Sep 29 '11 at 05:57
0

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
}
0

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"
}
bafoly
  • 31
  • 1
  • 2