Find which url was used to access the controller when multiple url mapping to the same controller method

Viewed 109

I see Spring MVC multiple url mapping to the same controller method

So now I have a method defined as

@RequestMapping(value = {"/aaa", "/bbb", "/ccc/xxx"}, method = RequestMethod.POST)
public String foo() {
    // was it called from /aaa or /bbb
}

At run time, I want to know if the controller was called from /aaa or /bbb

1 Answers

You can use HttpServletRequest#getServletPath which:

Returns the part of this request's URL that calls the servlet. This path starts with a "/" character and includes either the servlet name or a path to the servlet, but does not include any extra path information or a query string.

As follow:

@RequestMapping(value = {"/aaa", "/bbb", "/ccc/xxx"}, method = RequestMethod.POST)
public String foo(HttpServletRequest request) {
  String path = request.getServletPath(); // -> gives "/aaa", "/bbb" or "/ccc/xxx"
}
Related