使用fastjson 替换springMvc默认的jackson

来源:互联网 发布:淘宝面试问题及答案 编辑:程序博客网 时间:2024/06/05 02:22

两种方式解决:
一、xml配置方式

<mvc:annotation-driven>    <mvc:message-converters register-defaults="true">        <!-- 配置Fastjson支持 -->        <bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter">            <property name="supportedMediaTypes">                <list>                    <value>text/html;charset=UTF-8</value>                    <value>application/json</value>                </list>            </property>            <property name="features">                <list>                    <value>WriteMapNullValue</value>                    <value>QuoteFieldNames</value>                </list>            </property>        </bean>    </mvc:message-converters></mvc:annotation-driven>

二、注解方式

/**
* 该类覆盖了原有的HttpMessageConverters,只剩下一个json的处理类,采用fastJson
*/
@Configuration
public class WebConfigure extends WebMvcConfigurerAdapter {

@Beanpublic HttpMessageConverters customConverters() {    return new HttpMessageConverters(new FastJsonHttpMessageConverter());}@Overridepublic void addInterceptors(InterceptorRegistry registry) {    registry.addInterceptor(new AccessInterceptor()).addPathPatterns("/**");    super.addInterceptors(registry);}

}