Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 507
Evgeni Dimitrov Avatar asked Aug 03 '16 10:08

Evgeni Dimitrov


1 Answers

There is an option to create custom HandlerMethodArgumentResolver.

like image 194
luboskrnac Avatar answered Oct 26 '22 13:10

luboskrnac