SpringMVC的Body參數(shù)攔截的問題
SpringMVC對出參和入?yún)⒂蟹浅S押玫耐卣怪С?方便你對數(shù)據(jù)的輸入和輸出有更大的執(zhí)行權(quán),我們?nèi)绾瓮ㄟ^SpringMVC定義的結(jié)果做一系列處理呢?
入?yún)?/strong>
RequestBodyAdvice : 針對所有以@RequestBody的參數(shù)做處理
參考案例 : JsonViewRequestBodyAdvice
public class JsonViewRequestBodyAdvice extends RequestBodyAdviceAdapter {
/**
* 這里是一個前置攔截匹配操作,其實就是告訴你滿足為true的才會執(zhí)行下面的beforeBodyRead方法,這里可以定義自己業(yè)務(wù)相關(guān)的攔截匹配
* @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);
}
// 這里就是具體的前置操作了... 下面的例子就是查找這個入?yún)⒎椒ㄊ欠裼蠤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 就是返回的結(jié)果對象,沒有處理之前
return body;
}
}
注意事項
自定義的處理對象類上必須得加上@ControllerAdvice注解!
為什么?
源碼中RequestMappingHandlerAdapter類在執(zhí)行initControllerAdviceCache()做初始化的時候會執(zhí)行一個
List<ControllerAdviceBean> beans = ControllerAdviceBean.findAnnotatedBeans(getApplicationContext()); AnnotationAwareOrderComparator.sort(beans);
而ControllerAdviceBean.findAnnotatedBeans方法會查找類上有ControllerAdvice注解的類才會加入到處理當(dāng)中..
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;
}
所以大家可以根據(jù)自己的需要,定義結(jié)果的入?yún)⒑统鰠⒔Y(jié)果做一些特殊處理.....
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Springmvc中的轉(zhuǎn)發(fā)重定向和攔截器的示例
- SpringMVC中的攔截器詳解及代碼示例
- springMVC攔截器HandlerInterceptor用法代碼示例
- springmvc用于方法鑒權(quán)的注解攔截器的解決方案代碼
- springmvc限流攔截器的示例代碼
- SpringMVC攔截器實現(xiàn)單點登錄
- SpringMVC攔截器實現(xiàn)監(jiān)聽session是否過期詳解
- Spring MVC實現(xiàn)的登錄攔截器代碼分享
- 防止SpringMVC攔截器攔截js等靜態(tài)資源文件的解決方法
- 詳解SpringMVC攔截器配置及使用方法
- Spring MVC攔截器_動力節(jié)點Java學(xué)院整理
相關(guān)文章
microlog4android將Android Log日志寫到SD卡文件中實現(xiàn)方法
這篇文章主要介紹了microlog4android將Android Log日志寫到SD卡文件中實現(xiàn)方法的相關(guān)資料,需要的朋友可以參考下2016-10-10
IntelliJ IDEA 2021.1 EAP 1 發(fā)布支持 Java 16 和 WSL 2
這篇文章主要介紹了IntelliJ IDEA 2021.1 EAP 1 發(fā)布支持 Java 16 和 WSL 2,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-02-02
SpringBoot實現(xiàn)自定義Redis的連接的流程步驟
Spring Boot 自定義 Redis 主要是指在基于 Spring Boot 的應(yīng)用程序中,當(dāng)你需要更深入地控制或擴(kuò)展對 Redis 數(shù)據(jù)庫的操作,而不是僅僅依賴 Spring Data Redis 的默認(rèn)配置,本文給大家介紹了SpringBoot實現(xiàn)自定義Redis的連接的流程步驟,需要的朋友可以參考下2024-09-09
Java實現(xiàn)商品管理系統(tǒng)代碼實例講解
這篇文章主要介紹了Java實現(xiàn)商品管理系統(tǒng)代碼實例講解,文中代碼實例講解的很清楚,有需要的同學(xué)可以借鑒參考下2021-02-02

