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 { /** * 這里是一個(gè)前置攔截匹配操作,其實(shí)就是告訴你滿足為true的才會(huì)執(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); } // 這里就是具體的前置操作了... 下面的例子就是查找這個(gè)入?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; } }
注意事項(xiàng)
自定義的處理對象類上必須得加上@ControllerAdvice注解!
為什么?
源碼中RequestMappingHandlerAdapter
類在執(zhí)行initControllerAdviceCache()
做初始化的時(shí)候會(huì)執(zhí)行一個(gè)
List<ControllerAdviceBean> beans = ControllerAdviceBean.findAnnotatedBeans(getApplicationContext()); AnnotationAwareOrderComparator.sort(beans);
而ControllerAdviceBean.findAnnotatedBeans方法會(huì)查找類上有ControllerAdvice注解的類才會(huì)加入到處理當(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攔截器實(shí)現(xiàn)單點(diǎn)登錄
- SpringMVC攔截器實(shí)現(xiàn)監(jiān)聽session是否過期詳解
- Spring MVC實(shí)現(xiàn)的登錄攔截器代碼分享
- 防止SpringMVC攔截器攔截js等靜態(tài)資源文件的解決方法
- 詳解SpringMVC攔截器配置及使用方法
- Spring MVC攔截器_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
相關(guān)文章
關(guān)于springboot 配置date字段返回時(shí)間戳的問題
這篇文章主要介紹了springboot 配置date字段返回時(shí)間戳的問題,在springboot2.0后,spring會(huì)將Date字段自動(dòng)給轉(zhuǎn)成UTC字符串了(在沒有配置的情況下),所以date需要轉(zhuǎn)換成時(shí)間戳還是yyyy-MM-dd HH:mm:ss,具體解決方法跟隨小編一起看看吧2021-07-07SpringBoot 策略模式實(shí)現(xiàn)切換上傳文件模式
策略模式是指有一定行動(dòng)內(nèi)容的相對穩(wěn)定的策略名稱,這篇文章主要介紹了SpringBoot 策略模式 切換上傳文件模式,需要的朋友可以參考下2023-11-11Eclipse創(chuàng)建JavaWeb工程的完整步驟記錄
很多新手不知道Eclipse怎么創(chuàng)建Java Web項(xiàng)目,一起來看看吧,這篇文章主要給大家介紹了關(guān)于Eclipse創(chuàng)建JavaWeb工程的完整步驟,需要的朋友可以參考下2023-10-10如何寫好一個(gè)Spring組件的實(shí)現(xiàn)步驟
這篇文章主要介紹了如何寫好一個(gè)Spring組件的實(shí)現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06RabbitMQ 的消息持久化與 Spring AMQP 的實(shí)現(xiàn)詳解
這篇文章主要介紹了RabbitMQ 的消息持久化與 Spring AMQP 的實(shí)現(xiàn)剖析詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-08-08詳解mybatis #{}和${}的區(qū)別、傳參、基本語法
這篇文章主要介紹了mybatis #{}和${}的區(qū)別、傳參、基本語法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07