SpringMVC的Body参数拦截的问题

SpringMVC对出参和入参有非常友好的拓展支持,方便你对数据的输入和输出有更大的执行权,我们如何通过SpringMVC定义的结果做一系列处理呢?

入参

RequestBodyAdvice : 针对所有以@RequestBody的参数做处理

参考案例 : JsonViewRequestBodyAdvice

public class JsonViewRequestBodyAdvice extends RequestBodyAdviceAdapter {

/**

* 这里是一个前置拦截匹配操作,其实就是告诉你满足为true的才会执行下面的beforeBodyRead方法,这里可以定义自己业务相关的拦截匹配

* @param methodParameter

* @param targetType

* @param converterType

* @return

*/

@Override

public boolean supports(MethodParameter methodParameter, Type targetType,

Class<? extends HttpMessageConverter<?>> converterType) {

return (AbstractJackson2HttpMessageConverter.class.isAssignableFrom(converterType) &&

methodParameter.getParameterAnnotation(JsonView.class) != null);

}

// 这里就是具体的前置操作了... 下面的例子就是查找这个入参方法是否有@JsonView修饰

@Override

public HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter methodParameter,

Type targetType, Class<? extends HttpMessageConverter<?>> selectedConverterType) throws IOException {

JsonView annotation = methodParameter.getParameterAnnotation(JsonView.class);

Class<?>[] classes = annotation.value();

if (classes.length != 1) {

throw new IllegalArgumentException(

"@JsonView only supported for request body advice with exactly 1 class argument: " + methodParameter);

}

return new MappingJacksonInputMessage(inputMessage.getBody(), inputMessage.getHeaders(), classes[0]);

}

}

出参

ResponseBodyAdvice: 针对所有以@ResponseBody的参数做处理

参考案例:

@ControllerAdvice

public class LogResponseBodyAdvice implements ResponseBodyAdvice {

/**

*

* @param returnType

* @param converterType

* @return

*/

@Override

public boolean supports(MethodParameter returnType, Class converterType) {

return true;

}

@Override

public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {

// 做任何事情 body 就是返回的结果对象,没有处理之前

return body;

}

}

注意事项

自定义的处理对象类上必须得加上@ControllerAdvice注解!

为什么?

源码中RequestMappingHandlerAdapter类在执行initControllerAdviceCache()做初始化的时候会执行一个

List<ControllerAdviceBean> beans = ControllerAdviceBean.findAnnotatedBeans(getApplicationContext());

AnnotationAwareOrderComparator.sort(beans);

而ControllerAdviceBean.findAnnotatedBeans方法会查找类上有ControllerAdvice注解的类才会加入到处理当中..

public static List<ControllerAdviceBean> findAnnotatedBeans(ApplicationContext applicationContext) {

List<ControllerAdviceBean> beans = new ArrayList<ControllerAdviceBean>();

for (String name : BeanFactoryUtils.beanNamesForTypeIncludingAncestors(applicationContext, Object.class)) {

if (applicationContext.findAnnotationOnBean(name, ControllerAdvice.class) != null) {

beans.add(new ControllerAdviceBean(name, applicationContext));

}

}

return beans;

}

所以大家可以根据自己的需要,定义结果的入参和出参结果做一些特殊处理.....

以上是 SpringMVC的Body参数拦截的问题 的全部内容, 来源链接: utcz.com/z/319103.html

回到顶部