简体   繁体   English

Spring MVC如何为控制器方法提供注入

[英]Spring MVC How to provide injectable for controller method

Spring MVC controller methods accespt different parameters that are injected before the method is invoked. Spring MVC控制器方法访问在调用方法之前注入的不同参数。 Like HttpServletRequest , HttpServletResponse , java.security.Principal , etc. HttpServletRequestHttpServletResponsejava.security.Principal等。

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

How can I declare something that can be injected in a controlelr method? 如何声明可以在controlelr方法中注入的内容?

@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. 我想在一些servlet过滤器中解析HttpServletRequest并构造一个将在控制器方法中使用的对象。 More precisely I will parse a JWT token and access the claims. 更准确地说,我将解析JWT令牌并访问声明。

可以选择创建自定义HandlerMethodArgumentResolver

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM