Jackson和fastJson使用

Jackson和fastJson使用

一、添加依赖
  <!--引入fastjson-->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
      <version>1.2.73</version>
    </dependency>
    <!--引入jackson-->
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-core</artifactId>
      <version>2.9.8</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.9.8</version>
    </dependency>

二、添加配置(springmvc-config.xml)

<!--配置jackson-->
    <bean id="mappingJackson2HttpMessageConverter"
          class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
        <property name="defaultCharset" value="utf-8"/>
        <property name="supportedMediaTypes">
            <list>
                <value>application/json;charset=utf-8</value>
            </list>
        </property>
    </bean>

    <!--配置fastJson-->
    <bean id="fastJsonHttpMessageConverter" class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
        <property name="supportedMediaTypes" value="application/json;charset=utf-8"></property>
        <property name="charset" value="utf-8"></property>
        <property name="features">
            <array>
                <!--对象转json,格式化对象中属性为null的属性,不会显示出来-->
                <value>WriteMapNullValue</value>
                <!--格式化时间-->
                <value>WriteDateUseDateFormat</value>
            </array>
        </property>
    </bean>
    
  <mvc:annotation-driven/>

    <!--处理返回数据-->
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <property name="messageConverters">
            <list>
                <!--配置fastJson转换器-->
                <ref bean="fastJsonHttpMessageConverter"></ref>
                <!--配置jackson转换器-->
                <!--<ref bean="mappingJackson2HttpMessageConverter"></ref>-->
            </list>
        </property>
    </bean>
上一篇:二叉搜索树


下一篇:com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class