zoukankan      html  css  js  c++  java
  • spring-mvc中自动转json问题

    记录是为了更好的成长!

    在项目中经常要进行返回值转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>

    以上内容代表个人观点,仅供参考,不喜勿喷。。。

  • 相关阅读:
    ADO.Net对Oracle数据库的操作(转)
    代码反思(1)
    继承与多态
    存储过程
    linux学习流程及内容概括
    Linux下终端快捷键
    查找算法
    epoll解读
    TCP/udp编程
    如何学习嵌入式
  • 原文地址:https://www.cnblogs.com/newbest/p/10226686.html
Copyright © 2011-2022 走看看