SpringMVC的Body參數(shù)攔截的問題
SpringMVC對出參和入參有非常友好的拓展支持,方便你對數(shù)據的輸入和輸出有更大的執(zhí)行權,我們如何通過SpringMVC定義的結果做一系列處理呢?
入參
RequestBodyAdvice : 針對所有以@RequestBody的參數(shù)做處理
參考案例 : JsonViewRequestBodyAdvice
public class JsonViewRequestBodyAdvice extends RequestBodyAdviceAdapter { /** * 這里是一個前置攔截匹配操作,其實就是告訴你滿足為true的才會執(zhí)行下面的beforeBodyRead方法,這里可以定義自己業(yè)務相關的攔截匹配 * @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的參數(shù)做處理
參考案例:
@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
類在執(zhí)行initControllerAdviceCache()
做初始化的時候會執(zhí)行一個
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中的轉發(fā)重定向和攔截器的示例
- SpringMVC中的攔截器詳解及代碼示例
- springMVC攔截器HandlerInterceptor用法代碼示例
- springmvc用于方法鑒權的注解攔截器的解決方案代碼
- springmvc限流攔截器的示例代碼
- SpringMVC攔截器實現(xiàn)單點登錄
- SpringMVC攔截器實現(xiàn)監(jiān)聽session是否過期詳解
- Spring MVC實現(xiàn)的登錄攔截器代碼分享
- 防止SpringMVC攔截器攔截js等靜態(tài)資源文件的解決方法
- 詳解SpringMVC攔截器配置及使用方法
- Spring MVC攔截器_動力節(jié)點Java學院整理
相關文章
SpringBoot 策略模式實現(xiàn)切換上傳文件模式
策略模式是指有一定行動內容的相對穩(wěn)定的策略名稱,這篇文章主要介紹了SpringBoot 策略模式 切換上傳文件模式,需要的朋友可以參考下2023-11-11Eclipse創(chuàng)建JavaWeb工程的完整步驟記錄
很多新手不知道Eclipse怎么創(chuàng)建Java Web項目,一起來看看吧,這篇文章主要給大家介紹了關于Eclipse創(chuàng)建JavaWeb工程的完整步驟,需要的朋友可以參考下2023-10-10RabbitMQ 的消息持久化與 Spring AMQP 的實現(xiàn)詳解
這篇文章主要介紹了RabbitMQ 的消息持久化與 Spring AMQP 的實現(xiàn)剖析詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-08-08詳解mybatis #{}和${}的區(qū)別、傳參、基本語法
這篇文章主要介紹了mybatis #{}和${}的區(qū)別、傳參、基本語法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-07-07