• SpringBoot 用fastjson替换到jackjson并解决中文乱码的问题


    在入口类里面进行配置,入口类继承WebMvcConfigurerAdapter并重写configureMessageConverters方法,在方法里继承并修改fastjson的编码格式

    package com.ljun;


    import java.util.ArrayList;
    import java.util.List;

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.context.annotation.Bean;
    import org.springframework.http.MediaType;
    import org.springframework.http.converter.HttpMessageConverter;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

    import com.alibaba.fastjson.serializer.SerializerFeature;
    import com.alibaba.fastjson.support.config.FastJsonConfig;
    import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;

    @SpringBootApplication
    public class App extends WebMvcConfigurerAdapter{

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    super.configureMessageConverters(converters);
    //1.需要先定義一個convert轉換信息對象
    FastJsonHttpMessageConverter fastJsonConverter = new FastJsonHttpMessageConverter();
    //2.添加fastJson配置信息,比如是否要格式化返回的json数据
    FastJsonConfig fastJsonConfig = new FastJsonConfig();
    fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
    //处理中文乱码问题
            List<MediaType> fastMediaTypes = new ArrayList<>();
            fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
            fastJsonConverter.setSupportedMediaTypes(fastMediaTypes);
    //3.在convert中添加配置信息
    fastJsonConverter.setFastJsonConfig(fastJsonConfig);
    //4.将converter添加到converters中
    converters.add(fastJsonConverter);
    }
    /**
    * 使用@Bean注入fastJsonMessageConverter
    * @return
    */
    // @Bean
    // public HttpMessageConverter fastJsonMessageConverters() {
    // //1.需要先定義一個convert轉換信息對象
    // FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
    // //2.添加fastJson配置信息,比如是否要格式化返回的json数据
    // FastJsonConfig fastJsonConfig = new FastJsonConfig();
    // fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
    // //3.在convert中添加配置信息
    // fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
    // //4.将converter添加到converters中
    // HttpMessageConverter httpMessageConverter = fastJsonHttpMessageConverter;
    // return httpMessageConverter;
    // }

    public static void main( String[] args ){
    SpringApplication.run(App.class, args);
    }

    }

    原地址https://www.cnblogs.com/xql4j/p/6729524.html

  • 相关阅读:
    css 颜色
    目标
    css单位
    自我介绍
    Grid Layout
    position
    【转】android源码分析之windowmanager (android悬浮窗口的实现)
    java线程池原理
    STM32 中断中调用freeRTOS API 需要注意的地方
    今天开通博客园啦~~~~
  • 原文地址:https://www.cnblogs.com/telwanggs/p/15830036.html
Copyright © 2020-2023  润新知