Skip to content

Instantly share code, notes, and snippets.

@yangl
Last active April 17, 2018 03:36
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save yangl/6710838 to your computer and use it in GitHub Desktop.
Save yangl/6710838 to your computer and use it in GitHub Desktop.
SpringMVC + FastJson + httl配置。springmvc在@responsebody时返回已格式化的json串,方便测试api接口时查看返回数据;·prettyPrint=true·搞定
<?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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment