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;
}
1
  • 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? Commented Jun 10, 2023 at 18:47

6 Answers 6

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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

Comments

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

2 Comments

This is exactly what I want. i don't want to mess up the API mapping definition with something that is internal.
ServletUriComponentsBuilder.fromRequest(HttpServletRequest request) worked fine in interceptor.
1

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

1 Comment

It seams that Sangdol mean different urls, not http values. (So his term "values" is a bit confusing)
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
}

Comments

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

Comments

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.