自定義Jackson的ObjectMapper如何實(shí)現(xiàn)@ResponseBody的自定義渲染
XML
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="bean1" class="test.Bean1" /> <bean id="bean2" class="test.Bean2" /> </beans>
通常,我們可以采用下面的Java Config方式代替上面的Xml,實(shí)現(xiàn) fine-grained(細(xì)粒度) 配置。
Java Config
package test; @Configuration public class WebConfig { @Bean public Bean1 bean1(){ //…… } @Bean public Bean2 bean2(){ //…… } }
XXXConfigurer
但是,有時(shí)候我們希望:
一組功能相關(guān)的Bean之間能夠建立更直接更明確的關(guān)系。
那么我們可以選擇實(shí)現(xiàn) XXXConfigurer
這類 回調(diào)接口,然后使用 @Configuration注解該實(shí)現(xiàn)類,并Override它們的抽象方法。
例如:
功能 | 回調(diào)接口 |
---|---|
緩存 | org.springframework.cache.annotation.CachingConfigurer |
定時(shí)任務(wù) | org.springframework.scheduling.annotation.SchedulingConfigurer |
異步(并發(fā))任務(wù) | org.springframework.scheduling.annotation.AsyncConfigurer |
Spring MVC高級(jí)配置 | org.springframework.web.servlet.config.annotation.WebMvcConfigurer |
注意:
XXXConfigurer
接口的實(shí)現(xiàn)類無疑需要復(fù)寫其全部抽象方法(Java8之前,Spring舊版本),但是如果不希望覆蓋默認(rèn)或增加額外配置:
- 方法有返回值,則 return null。
- 方法無返回值,則不寫任何實(shí)現(xiàn)代碼。
- 當(dāng)然你也可以直接繼承其抽象適配器
XXXConfigurerAdapter
,根據(jù)配置需要復(fù)寫方法。 - 當(dāng)然
Java8
以后,接口方法有了默認(rèn)實(shí)現(xiàn)default
,在新版的Spring中,你可以直接實(shí)現(xiàn)XXXConfigurer
接口,并根據(jù)配置復(fù)寫方法。
示例:WebMvcConfigurer
我們以 WebMvcConfigurer 為例:自定義Jackson的ObjectMapper,實(shí)現(xiàn)@ResponseBody的自定義渲染?
- 解決方法是配置Spring MVC的 HttpMessageConverter 消息轉(zhuǎn)換器
先來看xml方式
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd"> <mvc:annotation-driven > <mvc:message-converters> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="objectMapper" ref="objectMapper"/> </bean> </mvc:message-converters> </mvc:annotation-driven> <bean id="objectMapper" class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean" p:serializationInclusion="NON_NULL"/>
WebMvcConfigurer的抽象適配器
我們還可以通過繼承 WebMvcConfigurer
的抽象適配器
org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter
來實(shí)現(xiàn)Spring MVC在Java Config 形式下的高級(jí)配置:
@Configuration @EnableWebMvc public class WebConfiguration extends WebMvcConfigurerAdapter { @Override public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { ObjectMapper mapper = new ObjectMapper(); mapper.setDefaultPropertyInclusion(Include.NON_NULL); converters.add(new MappingJackson2HttpMessageConverter(mapper)); } }
@EnableWebMvc
到這里,我們已經(jīng)實(shí)現(xiàn)了Spring MVC在Java Config 形式下的高級(jí)配置,但是需要注意,這里我們使用了 @EnableWebMvc
。
@EnableWebMvc源碼
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.TYPE) @Documented @Import(DelegatingWebMvcConfiguration.class) public @interface EnableWebMvc { }
第4行,導(dǎo)入了配置類
org.springframework.web.servlet.config.annotation.DelegatingWebMvcConfiguration
DelegatingWebMvcConfiguration源碼
@Configuration public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport { @Autowired(required = false) public void setConfigurers(List<WebMvcConfigurer> configurers) { if (!CollectionUtils.isEmpty(configurers)) { this.configurers.addWebMvcConfigurers(configurers); } } }
可以看到,DelegatingWebMvcConfiguration
的作用就是注入WebMvcConfigurer
的實(shí)現(xiàn)類, 而 DelegatingWebMvcConfiguration
又需要@EnableWebMvc
來導(dǎo)入。
所以如果你沒有使用Spring Boot,而是傳統(tǒng)的
Spring項(xiàng)目,又想要使用WebMvcConfigurer
來實(shí)現(xiàn)細(xì)粒度配置,你需要@EnableWebMvc
。
官方文檔上給出了另一種解決方式:
If WebMvcConfigurer does not expose some advanced setting that needs to be configured,
consider removing the @EnableWebMvc annotation and extending directly from
WebMvcConfigurationSupport or DelegatingWebMvcConfiguration,
如果WebMvcConfigurer
沒能解決你的需求,那么你可以考慮移除 @EnableWebMvc
并且直接繼承WebMvcConfigurationSupport
or DelegatingWebMvcConfiguration
。
注意:
Spring boot已經(jīng)通過Spring MVC的自動(dòng)配置類WebMvcAutoConfiguration
導(dǎo)入了DelegatingWebMvcConfiguration
,所以不再需要@EnableWebMvc
。
If you want to take complete control of Spring MVC, you can add your own @Configuration annotated with @EnableWebMvc.
相對(duì)的,如果你使用了@EnableWebMvc
,那么Spring MVC的自動(dòng)配置將被忽略。
@EnableXXX與<xxx:annotation-driven>的沖突
對(duì)于傳統(tǒng)Spring項(xiàng)目,Java Config的使用勢(shì)必導(dǎo)致Xml 與Java Config 同時(shí)存在于項(xiàng)目中。
這時(shí)候就需要我們?nèi)タ紤]Xml與annotation-based之間是否會(huì)產(chǎn)生沖突。
比如下面這些基本功能相同的標(biāo)簽和注解:
這里強(qiáng)調(diào)基本功能,因?yàn)镴ava Config的配置更細(xì)粒度,自然容易獲得其他擴(kuò)展導(dǎo)致某些功能不一致。
例如:@EnableWebMvc
導(dǎo)入了DelegatingWebMvcConfiguration
,從而實(shí)現(xiàn)了WebMvcConfigurer
接口的注入。
<cache:annotation-driven /> <!--@EnableCaching--> <task:annotation-driven scheduler=""/> <!--@EnableScheduling--> <task:annotation-driven executor=""/> <!--@EnableAsync--> <mvc:annotation-driven /> <!--@EnableWebMvc--> <!-- …… 等等,諸如此類 -->
這里還是以<mvc:annotation-driven />
與 @EnableWebMvc
為例,如果二者同時(shí)存在:
- 傳統(tǒng)Spring項(xiàng)目以
web.xml
加載dispatcher-servlet.xml
,毫無疑問配置在xml
中的<mvc:annotation-driven />
將會(huì)優(yōu)先生效。 - 所以,此時(shí)
@EnableWebMvc
不生效,WebMvcConfigurer
的實(shí)現(xiàn)類將不會(huì)被注入,Java Config的配置方式不會(huì)生效。 - 所以,這種情況需要二選一。
至于,其他**XXXConfigurer
** 在傳統(tǒng)Spring項(xiàng)目中的配置方式,與示例同理。
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringMVC中Controller層獲取前端請(qǐng)求參數(shù)的方式匯總
這篇文章主要介紹了SpringMVC中Controller層獲取前端請(qǐng)求參數(shù)的幾種方式,本文通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-08-08SpringBoot項(xiàng)目整合mybatis的方法步驟與實(shí)例
今天小編就為大家分享一篇關(guān)于SpringBoot項(xiàng)目整合mybatis的方法步驟與實(shí)例,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-03-03java反射機(jī)制給實(shí)體類相同字段自動(dòng)賦值實(shí)例
這篇文章主要介紹了java反射機(jī)制給實(shí)體類相同字段自動(dòng)賦值實(shí)例,具有2020-08-08Java中@JSONField注解用法、場(chǎng)景與實(shí)踐詳解
這篇文章主要給大家介紹了關(guān)于Java中@JSONField注解用法、場(chǎng)景與實(shí)踐的相關(guān)資料,并結(jié)合實(shí)際應(yīng)用場(chǎng)景,幫助開發(fā)者在項(xiàng)目中更高效地處理JSON數(shù)據(jù),文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-12-12Java?Lambda表達(dá)式常用的函數(shù)式接口
這篇文章主要介紹了Java?Lambda表達(dá)式常用的函數(shù)式接口,文章基于Java?Lambda表達(dá)式展開對(duì)常用的函數(shù)式接口的介紹,具有一的的參考價(jià)值需要的小伙伴可以參考一下2022-04-04關(guān)于SaCheckPermission權(quán)限校驗(yàn)注解
在若依框架(RuoYi)的前后端分離版4.8.x中,SaCheckPermission注解用于權(quán)限校驗(yàn),這個(gè)注解可以應(yīng)用在方法上,以確保只有具有相應(yīng)權(quán)限的用戶才能訪問該方法2024-11-11Java實(shí)現(xiàn)矩陣加減乘除及轉(zhuǎn)制等運(yùn)算功能示例
這篇文章主要介紹了Java實(shí)現(xiàn)矩陣加減乘除及轉(zhuǎn)制等運(yùn)算功能,結(jié)合實(shí)例形式總結(jié)分析了java常見的矩陣運(yùn)算實(shí)現(xiàn)技巧,需要的朋友可以參考下2018-01-01spring?boot?mybatis日志輸出到控制臺(tái)的方法實(shí)踐
在開發(fā)過程中我們往往需要打印出SQL語句,這樣就方便我們監(jiān)控問題,本文主要介紹了spring?boot?mybatis日志輸出到控制臺(tái)的方法實(shí)踐,具有一定的參考價(jià)值,感興趣的可以了解一下2024-05-05