SpringMVC实现⾃定义参数注解
Spring MVC实现⾃定义参数注解
如果熟悉SpringMVC原理和相关代码的话,就⽐较容易实现。关键点就是HandlerMethodArgumentResolver类—>实现请求参数解析SpringMVC默认提供实现
1. 常见实现类-类图
2. 常见参数注解说明–更多实现可以查看HandlerMethodArgumentResolver⼦类
类名⽀持注解备注说明PathVariableMapMethodArgumentResolver⽀持注解@PathVariable能够实现参数转换成Map
RequestParamMapMethodArgumentResolver⽀持注解@RequestParam能够实现参数转换Map
RequestAttributeMethodArgumentResolver⽀持注解@RequestAttribute能够获取WebRequest参数转换到Map RequestHeaderMapMethodArgumentResolver⽀持注解@RequestHeader能够获取RequestHeader参数转换到Map
3. 默认添加HandlerMethodArgumentResolver实现类对应的⽅法–
org.springframework.web.hod.annotation.RequestMappingHandlerAdapter#getDefaultArgumentResolv ers
注意点:从代码可以参数优先级是通过添加顺序来控制的
private List<HandlerMethodArgumentResolver>getDefaultArgumentResolvers(){
List<HandlerMethodArgumentResolver> resolvers =new ArrayList<>(30);
// Annotation-based argument resolution
resolvers.add(new RequestParamMethodArgumentResolver(getBeanFactory(),false));
resolvers.add(new RequestParamMapMethodArgumentResolver());
resolvers.add(new PathVariableMethodArgumentResolver());
resolvers.add(new PathVariableMapMethodArgumentResolver());
resolvers.add(new MatrixVariableMethodArgumentResolver());
resolvers.add(new MatrixVariableMapMethodArgumentResolver());
resolvers.add(new ServletModelAttributeMethodProcessor(false));
resolvers.add(new RequestResponseBodyMethodProcessor(getMessageConverters(),questResponseBodyAdvice));  resolvers.add(new RequestPartMethodArgumentResolver(getMessageConverters(),questResponseBodyAdvice));  resolvers.add(new RequestHeaderMethodArgumentResolver(getBeanFactory()));
resolvers.add(new RequestHeaderMapMethodArgumentResolver());
resolvers.add(new ServletCookieValueMethodArgumentResolver(getBeanFactory()));
resolvers.add(new ExpressionValueMethodArgumentResolver(getBeanFactory()));
resolvers.add(new SessionAttributeMethodArgumentResolver());
resolvers.add(new RequestAttributeMethodArgumentResolver());
// Type-based argument resolution
resolvers.add(new ServletRequestMethodArgumentResolver());
resolvers.add(new ServletResponseMethodArgumentResolver());
resolvers.add(new HttpEntityMethodProcessor(getMessageConverters(),questResponseBodyAdvice));
resolvers.add(new RedirectAttributesMethodArgumentResolver());
resolvers.add(new ModelMethodProcessor());
resolvers.add(new MapMethodProcessor());
resolvers.add(new ErrorsMethodArgumentResolver());
resolvers.add(new SessionStatusMethodArgumentResolver());
resolvers.add(new UriComponentsBuilderMethodArgumentResolver());
if(KotlinDetector.isKotlinPresent()){
resolvers.add(new ContinuationHandlerMethodArgumentResolver());
}
// Custom arguments
if(getCustomArgumentResolvers()!=null){
resolvers.addAll(getCustomArgumentResolvers());
}
/
/ Catch-all
resolvers.add(new PrincipalMethodArgumentResolver());
resolvers.add(new RequestParamMethodArgumentResolver(getBeanFactory(),true));
resolvers.add(new ServletModelAttributeMethodProcessor(true));
return resolvers;
}
4. 参数属性来源
1、spring 容器Bean
2、request 参数
3、外部化配置属性
4、servlet 容器参数
实现⾃定义参数注解步骤
1. ⾃定义注解类
import*;
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.PARAMETER})
public@interface MyMvcReqParam {
String value()default"";
}
2. 实现HandlerMethodArgumentResolver类,并实现supportsParameter和resolveArgument⽅法
import MethodParameter;
import LinkedMultiValueMap;
import MultiValueMap;springmvc的注解有哪些
import StringUtils;
import WebDataBinderFactory;
import NativeWebRequest;
import HandlerMethodArgumentResolver;
import ModelAndViewContainer;
import Map;
/**
* 处理@MyMvcReqParam注解
*/
public class MyMvcReqParamMapMethodArgumentResolver implements HandlerMethodArgumentResolver {
@Override
public boolean supportsParameter(MethodParameter parameter){
MyMvcReqParam ann = ParameterAnnotation(MyMvcReqParam.class);
return(ann !=null&& Map.class.ParameterType())&&
!StringUtils.hasText(ann.value()));
}
@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinder Factory binderFactory)throws Exception {
/
/将参数转换成map对象
Map<String, String[]> parameterMap = ParameterMap();
MultiValueMap<String, String> result =new LinkedMultiValueMap<>(parameterMap.size());
parameterMap.forEach((key, values)->{
for(String value : values){
result.add(key, value);
}
});
return result;
}
}
3. 将实现HandlerMethodArgumentResolver类,添加到SpringMVC对应的RequestMappingHandlerAdapter类中
@Configuration
public class MyWebMvcConfig implements WebMvcConfigurer {
/**
* add formatter registry
*
* @param registry
*/
@Override
public void addFormatters(FormatterRegistry registry){
DateTimeFormatterRegistrar registrar =new DateTimeFormatterRegistrar();
registrar.setUseIsoFormat(true);
registry.addConverter(new StringToDataConverter());
}
/**
* 添加⾃定义HandlerMethodArgumentResolver
* @param resolvers
*/
@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers){
resolvers.add(new MyMvcReqParamMapMethodArgumentResolver());
WebMvcConfigurer.super.addArgumentResolvers(resolvers);
}
}
总结
如果要实现⼀个简单的⾃定义注解,只要按照上述步骤进⾏实现就⾏,如果要实现⽐较复杂的实现类,可以参考ModelAttributeMethodProcessor。如果要深⼊使⽤,需要对源码有⼀定了解,才能更好的利⽤Spring框架提供的功能进⾏扩展。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。