为了账号安全,请及时绑定邮箱和手机立即绑定

HandlerMethodArgumentResolver的原理是什么?

HandlerMethodArgumentResolver的原理是什么?

Qyouu 2019-01-16 09:06:46
如题:在一个controller的请求方法中,有两个自定义的参数注解@UserInfo和@SchoolInfo @RequestMapping(value = "/filter/test2") public String test2(@RequestBody String o, @UserInfo UserInfoVo userInfoVo, @SchoolInfo SchoolInfoVo schoolInfoVo) { System.out.println("Controller中获取的请求数据:" + o); System.out.println("自定义注解UserInfo的值:" + userInfoVo); System.out.println("自定义注解SchoolInfo的值:" + schoolInfoVo); return "OK"; } 实现两个参数解析器如下: public class LoginUserInfoMethodArgumentResolver implements HandlerMethodArgumentResolver { @Override public boolean supportsParameter(MethodParameter parameter) { // return parameter.getParameterType().equals(UserInfoVo.class); System.out.println("login" + parameter.hasParameterAnnotation(UserInfo.class)); return parameter.hasParameterAnnotation(UserInfo.class); } @Override public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception { HttpServletRequest httpServletRequest = webRequest.getNativeRequest(HttpServletRequest.class); String str = HttpHelper.getBodyString(httpServletRequest); System.out.println("参数解析器login获取的请求数据:" + str); UserInfoVo u = new UserInfoVo(); u.setName("洪桓"); u.setAge(233); return u; } } public class SchoolInfoMethodArgumentResolver implements HandlerMethodArgumentResolver { @Override public boolean supportsParameter(MethodParameter parameter) { System.out.println("school" + parameter.hasParameterAnnotation(SchoolInfo.class)); return parameter.hasParameterAnnotation(SchoolInfo.class); } @Override public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception { HttpServletRequest httpServletRequest = webRequest.getNativeRequest(HttpServletRequest.class); String str = HttpHelper.getBodyString(httpServletRequest); System.out.println("参数解析器school获取的请求数据:" + str); SchoolInfoVo schoolInfoVo = new SchoolInfoVo(); schoolInfoVo.setName("HNU"); schoolInfoVo.setYear(1980); return schoolInfoVo; } } WebMvcConfigurerAdapter中实现顺序如下: @Override public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) { super.addArgumentResolvers(argumentResolvers); argumentResolvers.add(new LoginUserInfoMethodArgumentResolver()); argumentResolvers.add(new SchoolInfoMethodArgumentResolver()); } 最后输出结果 logintrue loginfalse schooltrue 如果按照list的添加顺序算的话,第一遍扫描判断parameter.hasParameterAnnotation(UserInfo.class)是true,第二遍扫描parameter.hasParameterAnnotation(UserInfo.class)和parameter.hasParameterAnnotation(SchoolInfo.class)分别是flase和true,为什么第二次参数解析器中会返回false
查看完整描述

1 回答

?
墨色风雨

TA贡献1853条经验 获得超6个赞

第一次是传 @UserInfo UserInfoVo userInfoVo 进去,所以是true

第二次是传 @SchoolInfo SchoolInfoVo schoolInfoVo 进去,所以是false,然后遍历到 SchoolInfoMethodArgumentResolver 返回true了

查看完整回答
反对 回复 2019-02-12
  • 1 回答
  • 0 关注
  • 1349 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信