ssm之用FastJSON实现后台自动返回json数据给页面

ssm之用FastJSON实现后台自动返回json数据给页面

网有很多关于该部分的内容,但大部分都是教怎返回json字符串而不是通过配置实现返回json格式的对象。而在现实开发中,大部分都用ajax来请求后端,而得到对象的json数据,比如微信小程序和angularjs等。废话有的多,下面开始,在此仅作整合参考。
1.肯定是引入所需要的jar包
我自己用的是阿里的FastJson,网上还有很多用jackson
maven依赖如下:

<!-- json -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.46</version>
        </dependency>

2.在springmvc的配置文件springmvc.xml中添加如下代码
如果没有其它的处理器加进来

<mvc:annotation-driven>
<!--springmvc 3.1之前版本不支持这种写法,注意看你springmvc的.xsd的约束文件版本-->
        <mvc:message-converters
                register-defaults="true">
            <!-- 避免IE执行AJAX时,返回JSON出现下载文件 -->
            <!-- FastJson -->
            <bean id="fastJsonHttpMessageConverter"
                  class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <!-- 这里顺序不能反,一定先写text/html,不然ie下出现下载提示 -->
                        <value>application/json;charset=UTF-8</value>
                    </list>
                </property>
                <property name="features">
                    <array
                            value-type="com.alibaba.fastjson.serializer.SerializerFeature">
                        <!-- 避免循环引用 -->
                        <value>DisableCircularReferenceDetect</value>
                        <!-- 是否输出值为null的字段 -->
                         <value>WriteMapNullValue</value>
                         <!--->
                         <value>QuoteFieldNames</value>
                        <!-- 数值字段如果为null,输出为0,而非null -->
                        <!-- <value>WriteNullNumberAsZero</value>-->
                        <!-- 字符类型字段如果为null,输出为"",而非null -->
                        <value>WriteNullStringAsEmpty</value>
                        <!-- List字段如果为null,输出为[],而非null -->
                        <value>WriteNullListAsEmpty</value>
                        <!-- Boolean字段如果为null,输出为false,而非null -->
                        <!--  <value>WriteNullBooleanAsFalse</value>-->
                    </array>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>

至于其中更多value及其用法参考fastJson的SerializerFeature属性
由于我还添加了一个接受时间转换的转换器,所以写成下面这样

<mvc:annotation-driven conversion-service="conversionService">
        <mvc:message-converters
                register-defaults="true">
            <!-- 避免IE执行AJAX时,返回JSON出现下载文件 -->
            <!-- FastJson -->
            <bean id="fastJsonHttpMessageConverter"
                  class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <!-- 这里顺序不能反,一定先写text/html,不然ie下出现下载提示 -->
                        <value>application/json;charset=UTF-8</value>
                    </list>
                </property>
                <property name="features">
                    <array value-type="com.alibaba.fastjson.serializer.SerializerFeature">
                        <value>WriteNullStringAsEmpty</value>
                        <value>WriteNullListAsEmpty</value>
                        <value>WriteMapNullValue</value>
                        <value>QuoteFieldNames</value>
                    </array>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>
    //转换器
<bean id="conversionService"     
 class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
        <property name="converters">
            <set>
                <ref bean="dateConvert"/>
            </set>
        </property>
    </bean>

3.配置完成,只要在controller层需要返回json格式的对象的方法加上@ResponseBody注解即可
效果如下:
在这里插入图片描述
至此本文结束,如有不当之处,往多加指正;

猜你喜欢

转载自blog.csdn.net/jiang18238032891/article/details/88368695
今日推荐