Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I access path variables in my custom HandlerMethodArgumentResolver

considering this spring MVC handler method:

  @RequestMapping(value = "/profile/{id}", method = RequestMethod.GET)
  public String displayProfile(Person person, ModelMap model) { ... }

my question: can I access the value of id in this resolver without having to parse the URl manually:

public class PersonReslover implements HandlerMethodArgumentResolver { .. }
like image 428
Java Fan Avatar asked Dec 04 '22 04:12

Java Fan


1 Answers

Using NativeWebRequest reference, which is accessible from resolveArgument() method of HandlerMethodArgumentResolver.

For example:

 private Map<String, String> getPathVariables(NativeWebRequest webRequest) {

    HttpServletRequest httpServletRequest = webRequest.getNativeRequest(HttpServletRequest.class);
    return (Map<String, String>) httpServletRequest.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
}
like image 198
Kanti Avatar answered May 20 '23 08:05

Kanti