xml SpringMVC + FastJson + httl配置.springmvc在@ResponseBody时返回已经格式化的json串,方便测试api接口时查看返回数据;·prett
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了xml SpringMVC + FastJson + httl配置.springmvc在@ResponseBody时返回已经格式化的json串,方便测试api接口时查看返回数据;·prett相关的知识,希望对你有一定的参考价值。
<?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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 自动扫描且只扫描@Controller -->
<context:component-scan base-package="com.jd.couponservice.test.controller" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
<context:include-filter type="annotation"
expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>
<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager">
<mvc:message-converters register-defaults="true">
<!-- 将StringHttpMessageCOnverter的默认编码设为UTF-8 -->
<bean class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg value="UTF-8"/>
<!-- 去掉response中的Accept-Charset -->
<property name="writeAcceptCharset" value="false"/>
</bean>
<!-- 将Jackson2HttpMessageConverter的默认格式化输出设为true -->
<!--<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="prettyPrint" value="false"/>
</bean>-->
<!-- 启用fastjson的json格式化输出、日期格式化输出 在使用FastJson 1.2.11+版本时注意以下区别! -->
<!-- 详见 https://github.com/alibaba/fastjson/wiki/FastJsonHttpMessageConverter_CN -->
<!-- spring mvc 4.2以上版本配置 -->
<bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">
<property name="fastJsonConfig">
<bean class="com.alibaba.fastjson.support.config.FastJsonConfig">
<property name="serializerFeatures">
<array value-type="com.alibaba.fastjson.serializer.SerializerFeature">
<!--<value>PrettyFormat</value>-->
<value>WriteDateUseDateFormat</value>
<value>WriteMapNullValue</value>
<value>WriteNullStringAsEmpty</value>
<value>WriteNullNumberAsZero</value>
<value>WriteNullBooleanAsFalse</value>
</array>
</property>
</bean>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<!-- REST中根据URL后缀自动判定Content-Type及相应的View -->
<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
<property name="mediaTypes">
<value>
json=application/json
xml=application/xml
</value>
</property>
</bean>
<bean id="viewResolver" class="httl.web.springmvc.HttlViewResolver">
<property name="contentType" value="text/html; charset=UTF-8"/>
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".httl"/>
</bean>
<mvc:default-servlet-handler/>
<!-- 静态文件客户端缓存:60 * 60 * 24 * 10 -->
<mvc:resources location="/public/" mapping="/public/**" cache-period="864000"/>
<!-- 定义无需Controller的url<->view直接映射 -->
<mvc:view-controller path="/" view-name="index"/>
<mvc:view-controller path="/web/mashup-client" view-name="/web/mashup-client"/>
<!-- 将Controller抛出的异常转到特定View, 保持SiteMesh的装饰效果 -->
<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="exceptionMappings">
<props>
<prop key="org.apache.shiro.authz.UnauthorizedException">error/403</prop>
<prop key="java.lang.Throwable">error/500</prop>
</props>
</property>
</bean>
</beans>
以上是关于xml SpringMVC + FastJson + httl配置.springmvc在@ResponseBody时返回已经格式化的json串,方便测试api接口时查看返回数据;·prett的主要内容,如果未能解决你的问题,请参考以下文章
SpringMVC(十三):SpringMVC 与fastjson集成
spring-springmvc搭建springMVC添加对静态资源访问的支持及对Fastjson的支持