使用fastjson 替换springMvc默认的jackson

两种方式解决:
一、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 {

@Bean
public HttpMessageConverters customConverters() {

    return new HttpMessageConverters(new FastJsonHttpMessageConverter());
}

@Override
public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(new AccessInterceptor()).addPathPatterns("/**");
    super.addInterceptors(registry);
}

}

展开阅读全文

没有更多推荐了,返回首页