我在我的Jersey项目中有一个自定义的InjectableProvider,它可以处理请求/会话以形成我的RESTful服务所需的用户ID.这意味着我可以编写如下服务方法:
@GET
@Produces(MediaType.APPLICATION_JSON)
public String getByAccountIdAndGroup(@InjectAccount(required=false) String accountId, @DefaultValue("default") @QueryParam("productGroup") String productGroup) {
...
}
@InjectAccount是我的自定义注释.
我想在同一个项目中为SpringMVC控制器做类似的事情.也就是说,我希望能够写:
@RequestMapping(method = RequestMethod.GET)
public String showForm(Model model, @InjectAccountSpringMvc String accountId) {
...
}
我期望必须分解公共代码而不是直接重用Jersey InjectableProvider.
可能更多以Spring为中心的方法是将一个线程范围bean注入到我的控制器中,但我很好奇是否可以在Spring MVC中完成Jersey样式.使用线程范围方法,注入的服务可能如下所示:
@Component
@Scope("THREAD")
public class AccountResolver {
@Autowired
private HttpServletRequest request
private String theAccount=null;
public String getAccount() {
if ( theAccount == null ) {
theAccount=resolveAccount();
}
return theAccount;
}
}
这不是很整洁,会使控制器直接测试更加困难.
任何想法/指针赞赏!
阿尔菲.
最佳答案 是的,这可以在Spring完成.您需要实现并注册自己的
HandlerMethodArgumentResolver
.
supportsParameter方法将只查找参数的注释. resolveArgument方法将执行检索在调用处理程序方法时使用的参数的逻辑.