Spring HandlerMethodArgumentResolver
Posted kwpsz
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring HandlerMethodArgumentResolver相关的知识,希望对你有一定的参考价值。
Spring 4.3.2
Spring请求的参数会经过HandlerMethodArgumentResolver的转换,接口HandlerMethodArgumentResolver定义了两个方法
boolean supportsParameter(MethodParameter parameter); Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception;
当supportsParameter返回true时会调用resolveArgument转换参数
源代码如下org.springframework.web.method.support.HandlerMethodArgumentResolverComposite
1 private HandlerMethodArgumentResolver getArgumentResolver(MethodParameter parameter) { 2 HandlerMethodArgumentResolver result = this.argumentResolverCache.get(parameter); 3 if (result == null) { 4 for (HandlerMethodArgumentResolver methodArgumentResolver : this.argumentResolvers) { 5 if (logger.isTraceEnabled()) { 6 logger.trace("Testing if argument resolver [" + methodArgumentResolver + "] supports [" + 7 parameter.getGenericParameterType() + "]"); 8 } 9 if (methodArgumentResolver.supportsParameter(parameter)) { 10 result = methodArgumentResolver; 11 this.argumentResolverCache.put(parameter, result); 12 break; 13 } 14 } 15 } 16 return result; 17 }
默认有26个实现HandlerMethodArgumentResolver的类,在org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter中添加
源代码如下
注释是HandlerMethodArgumentResolver处理的参数类型,?代表还未看懂的
1 private List<HandlerMethodArgumentResolver> getDefaultArgumentResolvers() { 2 List<HandlerMethodArgumentResolver> resolvers = new ArrayList<HandlerMethodArgumentResolver>(); 3 4 // Annotation-based argument resolution 5 resolvers.add(new RequestParamMethodArgumentResolver(getBeanFactory(), false)); // @RequestParam 6 resolvers.add(new RequestParamMapMethodArgumentResolver()); // @RequestParam Map [Map | MultiValueMap] 7 resolvers.add(new PathVariableMethodArgumentResolver()); // @PathVariable 8 resolvers.add(new PathVariableMapMethodArgumentResolver()); // @PathVariable Map 9 resolvers.add(new MatrixVariableMethodArgumentResolver()); // @MatrixVariable 10 resolvers.add(new MatrixVariableMapMethodArgumentResolver()); // @MatrixVariable Map 11 resolvers.add(new ServletModelAttributeMethodProcessor(false)); // @ModelAttribute | ? 12 resolvers.add(new RequestResponseBodyMethodProcessor(getMessageConverters(), this.requestResponseBodyAdvice)); // @RequestBody 13 resolvers.add(new RequestPartMethodArgumentResolver(getMessageConverters(), this.requestResponseBodyAdvice)); // @RequestPart | ? 14 resolvers.add(new RequestHeaderMethodArgumentResolver(getBeanFactory())); // @RequestHeader 15 resolvers.add(new RequestHeaderMapMethodArgumentResolver()); // @RequestHeader Map 16 resolvers.add(new ServletCookieValueMethodArgumentResolver(getBeanFactory())); // @CookieValue 17 resolvers.add(new ExpressionValueMethodArgumentResolver(getBeanFactory())); // @Value 18 resolvers.add(new SessionAttributeMethodArgumentResolver()); // @SessionAttribute 19 resolvers.add(new RequestAttributeMethodArgumentResolver()); // @RequestAttribute 20 21 // Type-based argument resolution 22 resolvers.add(new ServletRequestMethodArgumentResolver()); // WebRequest... 23 resolvers.add(new ServletResponseMethodArgumentResolver()); // ServletResponse | OutputStream | Writer 24 resolvers.add(new HttpEntityMethodProcessor(getMessageConverters(), this.requestResponseBodyAdvice)); // HttpEntity | RequestEntity 25 resolvers.add(new RedirectAttributesMethodArgumentResolver()); // RedirectAttributes 26 resolvers.add(new ModelMethodProcessor()); // Model 27 resolvers.add(new MapMethodProcessor()); // Map 28 resolvers.add(new ErrorsMethodArgumentResolver()); // Errors 29 resolvers.add(new SessionStatusMethodArgumentResolver()); // SessionStatus 30 resolvers.add(new UriComponentsBuilderMethodArgumentResolver()); // UriComponentBuilder | ServletUriComponentBuilder 31 32 // Custom arguments 33 if (getCustomArgumentResolvers() != null) { 34 resolvers.addAll(getCustomArgumentResolvers()); // 自定义 35 } 36 37 // Catch-all 38 resolvers.add(new RequestParamMethodArgumentResolver(getBeanFactory(), true)); // 普通类型 39 resolvers.add(new ServletModelAttributeMethodProcessor(true)); // 普通类型 40 41 return resolvers; 42 }
以上是关于Spring HandlerMethodArgumentResolver的主要内容,如果未能解决你的问题,请参考以下文章
Spring全家桶笔记:Spring+Spring Boot+Spring Cloud+Spring MVC
学习笔记——Spring简介;Spring搭建步骤;Spring的特性;Spring中getBean三种方式;Spring中的标签
Spring框架--Spring事务管理和Spring事务传播行为