SpringMVC整合JSON

1、FastJson

1.1、引入pom依赖

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.76</version>
</dependency>

1.2、整合

<!-- fastjson配置 -->
<bean id="fastJsonConfig" class="com.alibaba.fastjson.support.config.FastJsonConfig">
    <!-- 配置序列化规则 -->    
    <property name="serializerFeatures">
            <list>
                <!--格式化输出-->
                <value>PrettyFormat</value>
                <!--输出空的字段-->
                <value>WriteMapNullValue</value>
                <!--输出空的集合-->
                <value>WriteNullListAsEmpty</value>
                <!--输出空的字符串-->
                <value>WriteNullStringAsEmpty</value>
                <!--输出空的数字-->
                <value>WriteNullNumberAsZero</value>
            </list>
        </property>
        <!--设置编码格式-->
        <property name="charset" value="UTF-8"/>
        <!--设置日期格式-->
        <property name="dateFormat" value="yyyy-MM-dd HH:mm:ss"/>
    </bean>

<!-- 返回参数转换类 -->
<bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"
      id="fastJsonHttpMessageConverter">
    <property name="supportedMediaTypes" value="application/json"/><!-- 支持的媒体类型 -->
    <property name="fastJsonConfig" ref="fastJsonConfig"/><!-- fastjson配置 -->
</bean>

<!-- 字符串转换器 -->
<bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter">
    <!--设置编码格式-->
    <constructor-arg value="UTF-8" index="0"/>
    <!--设置支持的媒体类型-->
    <property name="supportedMediaTypes">
        <list>
            <!--支持text/plain格式-->
            <value>text/plain;charset=UTF-8</value>
            <!--支持application/json格式-->
            <value>application/json;charset=UTF-8</value>
        </list>
    </property>
</bean>

<!-- 开启SpringMVC注解驱动 -->
<mvc:annotation-driven>
    <!-- 添加自定义的转换器 -->
    <mvc:message-converters>
        <!-- 添加字符串转换器 -->
        <ref bean="stringHttpMessageConverter"/>
        <!-- 添加返回参数转换类 -->
        <ref bean="fastJsonHttpMessageConverter"/>
    </mvc:message-converters>
</mvc:annotation-driven>

2、Jackson

2.1、引入pom依赖

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.13.2.2</version>
</dependency>

2.2、整合

<!-- 字符串转换器 -->
<bean class="org.springframework.http.converter.StringHttpMessageConverter" id="stringHttpMessageConverter">
    <!-- 设置字符编码 -->
    <property name="defaultCharset" value="UTF-8"/>
</bean>
<!-- 将json转换为对象 -->
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" id="jackson2HttpMessageConverter">
    <property name="objectMapper">
        <!-- json转化器 -->
        <bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
            <!-- 处理空对象 -->
            <property name="failOnEmptyBeans" value="false"/>
        </bean>
    </property>
</bean>


<!-- 开启SpringMVC注解驱动 -->
<mvc:annotation-driven>
    <mvc:message-converters>
        <ref bean="stringHttpMessageConverter" />
        <ref bean="jackson2HttpMessageConverter" />
    </mvc:message-converters>
</mvc:annotation-driven>