Spring4.X的restFul 使用Gson返回json配置

Spring4.X的restFul 使用Gson返回json配置

Spring4.X里面加入了Gson支持和并且提高了对restful的支持,因为Gson不需要依赖其他架包,并且可用性及速度都还不错,特别是使用十分的简单,所以在架构restful时选择了Gson。但是因为spring最先支持的是jackson,所以导致网络上很多jackson的Spring restful配置,很少有Gson的配置,并且多数架构还是基于Spring3.X。所以在此记录下一些Gson的Spring 4.x配置。

* 1. 架包*
这里选择的架包是spring-4.2.6和gson-2.6.2。

* 2. 配置*
Spring Gson 的配置使用org.springframework.http.converter.json.GsonHttpMessageConverter;返回的消息头最好改为text/plain;charset=UTF-8;使用jquery的ajax只要返回type:”json”,完全不影响使用。如果使用application/json;charset=UTF-8的话在ie里面的地址栏访问会显示下载,影响调试。在<mvc:annotation-driven>里面配置返回的json配置,使用传统bean配置似乎无效,可能我哪里配的不太对吧!
具体配置如下:

<!-- 消息转换器 输出对象转JSON支持 -->
    <mvc:annotation-driven>
        <mvc:message-converters register-defaults="true">
            <!-- 避免返回String乱码 -->
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <value>text/plain;charset=UTF-8</value>
                        <value>text/html;charset=UTF-8</value>
                    </list>
                </property>
            </bean>
            <!-- Json转换,property 避免IE执行AJAX时,返回JSON出现下载文件 -->
            <bean
                class="org.springframework.http.converter.json.GsonHttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <value>text/plain;charset=UTF-8</value>
                        <value>application/json;charset=UTF-8</value>
                    </list>
                </property>
                <!-- 输出json结果格式化 -->
                <property name="gson">
                    <bean class="org.springframework.http.converter.json.GsonFactoryBean">
                        <!-- 输出null,false则不输出null值
                        <property name="serializeNulls" value="true" /> -->
                        <!-- 不对html标签转码,false会导致所有html标签转码为 \ue300 格式 -->
                        <property name="disableHtmlEscaping" value="true" />
                        <!-- 格式化日期 -->
                        <property name="dateFormatPattern" value="yyyy-MM-dd HH:mm:ss:SSS" />
                    </bean>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>

spring restful需要配置 <mvc:default-servlet-handler />,否则静态资源文件无法访问

<!-- 静态资源配置,拦截器不拦截静态资源 -->
    <mvc:default-servlet-handler />
    <!-- 静态资源映射,静态资源存放WEB-INF下,避免页面使用restful风格时死循环 -->
    <mvc:resources location="/WEB-INF/UIIndex/" mapping="/UIIndex/**" />

整体配置如下,我的配置是前端全部使用html,不使用ajax,所有数据都使用ajax + json来获取,使前后端完全分离。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:cache="http://www.springframework.org/schema/cache"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.2.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
    http://www.springframework.org/schema/cache
    http://www.springframework.org/schema/cache/spring-cache-4.2.xsd">

    <!-- 静态资源配置,拦截器不拦截静态资源 -->
    <mvc:default-servlet-handler />
    <!-- 静态资源映射,静态资源存放WEB-INF下,避免页面使用restful风格时死循环 -->
    <mvc:resources location="/WEB-INF/UIIndex/" mapping="/UIIndex/**" />

    <context:component-scan base-package="com.znv.rest.controller" />
    <context:component-scan base-package="com.znv.rest.service" />

    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <!--<property name="maxUploadSize" value="500000" /> -->
    </bean>

    <!-- html视图配置 -->
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/UIIndex/" />
        <property name="suffix">
            <value>.html</value>
        </property>
        <property name="contentType" value="text/html;charset=UTF-8" />
    </bean>

    <!-- 消息转换器 输出对象转JSON支持 -->
    <mvc:annotation-driven>
        <mvc:message-converters register-defaults="true">
            <!-- 避免返回String乱码 -->
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <value>text/plain;charset=UTF-8</value>
                        <value>text/html;charset=UTF-8</value>
                    </list>
                </property>
            </bean>
            <!-- Json转换,property 避免IE执行AJAX时,返回JSON出现下载文件 -->
            <bean
                class="org.springframework.http.converter.json.GsonHttpMessageConverter">
                <property name="supportedMediaTypes">
                    <list>
                        <value>text/plain;charset=UTF-8</value>
                        <value>application/json;charset=UTF-8</value>
                    </list>
                </property>
                <!-- 输出json结果格式化 -->
                <property name="gson">
                    <bean class="org.springframework.http.converter.json.GsonFactoryBean">
                        <!-- 输出null,false则不输出null值
                        <property name="serializeNulls" value="true" /> -->
                        <!-- 不对html标签转码,false会导致所有html标签转码为 \ue300 格式 -->
                        <property name="disableHtmlEscaping" value="true" />
                        <!-- 格式化日期 -->
                        <property name="dateFormatPattern" value="yyyy-MM-dd HH:mm:ss:SSS" />
                    </bean>
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>

    <!-- Spring注册拦截器 -->
    <mvc:interceptors>
        <mvc:interceptor>
            <!-- 匹配url路径, 如果不配置或/**,将拦截所有的Controller -->
            <mvc:mapping path="/*" />
            <mvc:mapping path="/*/*" />
            <mvc:mapping path="/*/pages/**" />
            <bean class="com.znv.rest.interceptor.CommonInterceptor">
                <property name="allowUrls">
                    <list>
                        <!-- 如果请求中包含以下路径,则不进行拦截 -->
                        <value>/bootstrap</value>
                        <value>/dist</value>
                        <value>/plugins</value>
                    </list>
                </property>
            </bean>
        </mvc:interceptor>
    </mvc:interceptors>

    <bean id="exceptionResolver" class="com.znv.rest.common.ServiceExceptionHandler" />

</beans>

猜你喜欢

转载自blog.csdn.net/chenfei2341/article/details/51700311