SpringMVC has very friendly expansion support for parameters export and parameters, which facilitates you to have greater execution rights for data input and output. How can we do a series of processing through the results defined by SpringMVC?
Enter the ginseng
RequestBodyAdvice: Processing for all parameters with @RequestBody
Reference case: JsonViewRequestBodyAdvice
public class JsonViewRequestBodyAdvice extends RequestBodyAdviceAdapter { /** * Here is a pre-intercepting and matching operation. In fact, it tells you that only those that satisfy true will execute the following beforeBodyRead method. Here you can define your own business-related interception and matching* @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); } // Here is the specific pre-operation... The following example is to find out whether this parameter entry method has @JsonView modification @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]); }}Export ginseng
ResponseBodyAdvice: Processing for all parameters with @ResponseBody
Reference case:
@ControllerAdvicepublic 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) { // Do anything body is the returned result object, return body before processing; }}Things to note
The custom processing object class must be added with the @ControllerAdvice annotation!
Why?
In the initControllerAdviceCache() code, the RequestMappingHandlerAdapter class will execute a
List<ControllerAdviceBean> beans = ControllerAdviceBean.findAnnotatedBeans(getApplicationContext());AnnotationAwareOrderComparator.sort(beans);
The ControllerAdviceBean.findAnnotatedBeans method will look for classes with ControllerAdvice annotations on the class and will be added to the processing.
public static List<ControllerAdviceBean> findAnnotatedBeans(ApplicationContext applicationContext) { List<ControllerAdviceBean> beans = new ArrayList<ControllerAdviceBean>(); for (String name : BeanFactoryUtils.beanNamesForTypeIncludedAncestors(applicationContext, Object.class)) { if (applicationContext.findAnnotationOnBean(name, ControllerAdvice.class) != null) { beans.add(new ControllerAdviceBean(name, applicationContext)); } } return beans; }So you can define the incoming and outgoing parameters of the result according to your needs and do some special treatment...
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.