spring-mvc中自動轉json問題
阿新 • • 發佈:2019-01-05
記錄是為了更好的成長!
在專案中經常要進行返回值轉json,但是我在配置的出現不能轉換的問題,花了很多時間終於搞定,簡單記錄如下:
1、第一種配置,也是我最開始的配置方式,一直不生效,配置如下:
(1)pom.xml引入fastjson依賴
(2)配置sping.xml,在網上看到的也都是這種配置
<mvc:annotation-driven> <mvc:message-converters register-defaults="true"> <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"> <property name="supportedMediaTypes" value="application/json"/> <property name="features"> <array> <value>WriteMapNullValue</value> <value>WriteDateUseDateFormat</value> </array> </property> </bean> </mvc:message-converters> </mvc:annotation-driven>
2、第二種配置,發現修改之後可以使用了
(1)引入依賴
<dependency> <groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-core</artifactId> <version>2.5.4</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.5.4</version> </dependency>
(2)修改spring.xml
<mvc:annotation-driven> <mvc:message-converters> <bean class="org.springframework.http.converter.StringHttpMessageConverter"/> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"/> </mvc:message-converters> </mvc:annotation-driven>
以上內容代表個人觀點,僅供參考,不喜勿噴。。。