SpringMvc切換Json轉(zhuǎn)換工具的操作代碼
SpringMvc切換Json轉(zhuǎn)換工具
SpringBoot切換使用goolge的Gson作為SpringMvc的Json轉(zhuǎn)換工具
<!-- gson依賴 --> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> </dependency>
@Configuration public class JsonWebConfig { @Bean public GsonHttpMessageConverter gsonHttpMessageConverter() { return new GsonHttpMessageConverter(); } }
補(bǔ)充:
Spring使用json轉(zhuǎn)換工具
一、直接調(diào)用
1、導(dǎo)入坐標(biāo)
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.9.0</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.0</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.9.0</version> </dependency>
2、使用工具
User user = new User(); user.setName("lisi"); user.setAge(18); //使用json的轉(zhuǎn)換工具 ObjectMapper objectMapper = new ObjectMapper(); String json = objectMapper.writeValueAsString(user); return json;
二、使用適配器
1、在spring-mvc.xml中進(jìn)行配置
<!-- 配置處理器映射器--> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> <property name="messageConverters"> <list> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/> </list> </property> </bean>
2、直接使用,返回的就是json格式
User user = new User(); user.setName("lisi"); user.setAge(18); return user;
三、使用注解配置
1、在spring-mvc.xml中進(jìn)行配置,先添加命名空間
<!-- mvc的注解驅(qū)動(dòng)--> <mvc:annotation-driven/>
到此這篇關(guān)于SpringMvc切換Json轉(zhuǎn)換工具的文章就介紹到這了,更多相關(guān)SpringMvc Json轉(zhuǎn)換工具內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot實(shí)現(xiàn)發(fā)送電子郵件
這篇文章主要介紹了SpringBoot實(shí)現(xiàn)發(fā)送電子郵件,電子郵件是—種用電子手段提供信息交換的通信方式,是互聯(lián)網(wǎng)應(yīng)用最廣的服務(wù)。通過網(wǎng)絡(luò)的電子郵件系統(tǒng),用戶可以非??焖俚姆绞?,與世界上任何一個(gè)角落的網(wǎng)絡(luò)用戶聯(lián)系,下面就來看看SpringBoot如何實(shí)現(xiàn)發(fā)送電子郵件吧2022-01-01在SpringBoot項(xiàng)目中利用maven的generate插件
今天小編就為大家分享一篇關(guān)于在SpringBoot項(xiàng)目中利用maven的generate插件,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-01-01SpringMVC配置攔截器實(shí)現(xiàn)登錄控制的方法
這篇文章主要介紹了SpringMVC配置攔截器實(shí)現(xiàn)登錄控制的方法,SpringMVC讀取Cookie判斷用戶是否登錄,對(duì)每一個(gè)action都要進(jìn)行判斷,有興趣的可以了解一下。2017-03-03java線性表的存儲(chǔ)結(jié)構(gòu)及其代碼實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了Java數(shù)據(jù)結(jié)構(gòu)學(xué)習(xí)筆記第一篇,線性表的存儲(chǔ)結(jié)構(gòu)及其代碼實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09Spring mvc 實(shí)現(xiàn)用戶登錄的方法(攔截器)
這篇文章主要介紹了Spring mvc 實(shí)現(xiàn)用戶登錄的方法(攔截器),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-07-07利用Spring MVC+Mybatis實(shí)現(xiàn)Mysql分頁數(shù)據(jù)查詢的過程詳解
這篇文章主要給大家介紹了關(guān)于利用Spring MVC+Mybatis實(shí)現(xiàn)Mysql分頁數(shù)據(jù)查詢的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-08-08