zoukankan      html  css  js  c++  java
  • Spring Boot-FastJson

    依赖:

    <!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>fastjson</artifactId>
                <version>1.2.60</version>
            </dependency>

    然后在启动类AppApplication里添加:

    @Bean
        public HttpMessageConverters fastJsonHttpMessageConverters() {
            FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
            FastJsonConfig fastJsonConfig = new FastJsonConfig();
            fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
            fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig);
            HttpMessageConverter<?> converter = fastJsonHttpMessageConverter;
            return new HttpMessageConverters(converter);
        }

    还有一种方法是:

    @Configuration
    public class WebConfig implements WebMvcConfigurer {
    
        @Override
        public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
            FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
            FastJsonConfig fastJsonConfig = new FastJsonConfig();
            fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat,
                    SerializerFeature.WriteMapNullValue,
                    SerializerFeature.WriteNullStringAsEmpty,
                    SerializerFeature.DisableCircularReferenceDetect,
                    SerializerFeature.WriteNullListAsEmpty,
                    SerializerFeature.WriteDateUseDateFormat);
            List<MediaType> fastMediaTypes = new ArrayList<>();
         //需要你在请求头中写contentType,不然会有"Content type 'appliction/json' not supported" fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
    fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes); fastJsonHttpMessageConverter.setFastJsonConfig(fastJsonConfig); converters.add(fastJsonHttpMessageConverter); } }
  • 相关阅读:
    什么是软件架构?
    子系统、框架与架构
    今天开始锻炼身体
    程序语言中基本数值类型的分类
    软件架构的作用
    软件架构要设计到什么程度
    软件架构视图
    更多资料
    How to:如何在调用外部文件时调试文件路径(常见于使用LaunchAppAndWait和LaunchApp函数)
    installshield卸载时提示重启动的原因以及解决办法
  • 原文地址:https://www.cnblogs.com/KuroNJQ/p/11188191.html
Copyright © 2011-2022 走看看