javaspringspring-mvcdependency-injectionspring-boot

Spring MVC How to provide injectable for controller method


Spring MVC controller methods accespt different parameters that are injected before the method is invoked. Like HttpServletRequest, HttpServletResponse, java.security.Principal, etc.

@RequestMapping("/test")
public String test(HttpServletRequest req, Principal user){}

How can I declare something that can be injected in a controlelr method?

@RequestMapping("/test")
public String test(MyCustomInjectable myInjectable){}

More on the specific case:

I want to parse the HttpServletRequest in some servlet filter and construct an object that will be used in the controller method. More precisely I will parse a JWT token and access the claims.


Solution

  • There is an option to create custom HandlerMethodArgumentResolver.