as the title: in a controller request method, there are two custom parameter annotations @ UserInfo and @ 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";
}
implement the two parameter parsers as follows:
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;
}
}
The order of implementation in WebMvcConfigurerAdapter is as follows:
@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
super.addArgumentResolvers(argumentResolvers);
argumentResolvers.add(new LoginUserInfoMethodArgumentResolver());
argumentResolvers.add(new SchoolInfoMethodArgumentResolver());
}
final output
logintrue
loginfalse
schooltrue
if calculated according to the order in which list is added, the first scan determines that parameter.hasParameterAnnotation (UserInfo.class) is true, and the second scan parameter.hasParameterAnnotation (UserInfo.class) and parameter.hasParameterAnnotation (SchoolInfo.class) are flase and true, respectively. Why false is returned in the second parameter parser