• spring boot 2.0添加对fastjson的支持


    首先引入fastjson的maven依赖:

    <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>1.2.31</version>
            </dependency>
    

      然后添加一个配置类:

    package com.boot.demo;
    
    import com.alibaba.fastjson.serializer.SerializerFeature;
    import com.alibaba.fastjson.support.config.FastJsonConfig;
    import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
    import javafx.scene.chart.Chart;
    import org.apache.logging.log4j.Logger;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.http.MediaType;
    import org.springframework.http.converter.HttpMessageConverter;
    import org.springframework.http.converter.StringHttpMessageConverter;
    import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
    
    import java.nio.charset.Charset;
    import java.util.ArrayList;
    import java.util.Iterator;
    import java.util.List;
    
    @Configuration
    public class FastJsonConfiguration implements WebMvcConfigurer {
    
    
        @Override
        public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    
            /**
             * 先移除MappingJackson2HttpMessageConverter转换器,再加入fastjson转换器
             */
            Iterator<HttpMessageConverter<?>> iterator = converters.iterator();
            while(iterator.hasNext()){
                HttpMessageConverter<?> converter = iterator.next();
                if(converter instanceof MappingJackson2HttpMessageConverter){
                    iterator.remove();
                }
            }
            FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
            //自定义配置...
            FastJsonConfig config = new FastJsonConfig();
            config.setSerializerFeatures(SerializerFeature.DisableCircularReferenceDetect,
                    SerializerFeature.WriteMapNullValue
                    ,SerializerFeature.WriteNullListAsEmpty,
                    SerializerFeature.WriteNullBooleanAsFalse,
                    SerializerFeature.WriteNullStringAsEmpty,SerializerFeature.WriteNullNumberAsZero);
            fastJsonHttpMessageConverter.setFastJsonConfig(config);
    
            //解决返回的json数据中文乱码问题
            List<MediaType> fastMediaTypes = new ArrayList<MediaType>();
            fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
            fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes);
    
            //将转换器添加到转换列表
            converters.add(fastJsonHttpMessageConverter);
    
        }
    
    }
  • 相关阅读:
    Go断后,Dart冲前,Google的野心
    gcc dynamic load library
    Go http server 高并发
    还是Go 为了伟大的未来
    windows go dll 框架
    Go cookie
    Go web ajax project
    hdoj 2844 Coins
    hdoj 1203 I NEED A OFFER!
    hdoj 2546 饭卡
  • 原文地址:https://www.cnblogs.com/blog411032/p/10330827.html
Copyright © 2020-2023  润新知