首页 > 其他分享 >FastJson 的一些配置

FastJson 的一些配置

时间:2022-09-20 10:24:40浏览次数:65  
标签:FastJson fastjson supportedMediaTypes 配置 springframework SerializerFeature impor

主要提到:关闭循环引用的配置

"$ref":"$.data[0].detail.wmsInboundorder.details[1]"

如下:

import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.boot.autoconfigure.web.HttpMessageConverters;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
 
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
 
/**
 * 自定义JSON转换器
 * 
 * @author larry.qi
 */
@Configuration
public class FastJSONConfiguration {
 
    @Bean
    public HttpMessageConverters httpMessageConverters() {
        HttpMessageConverter<?> fastjson = fastJsonHttpMessageConverter();
        return new HttpMessageConverters(fastjson);
    }
    
    @Bean
    public FastJsonHttpMessageConverter fastJsonHttpMessageConverter() {
        FastJsonHttpMessageConverter messageConverter = new FastJsonHttpMessageConverter();
        //set MediaTypes
        List<MediaType> supportedMediaTypes = new ArrayList<MediaType>();
        supportedMediaTypes.add(MediaType.APPLICATION_JSON);
        supportedMediaTypes.add(MediaType.TEXT_HTML);
        supportedMediaTypes.add(MediaType.TEXT_PLAIN);
        messageConverter.setSupportedMediaTypes(supportedMediaTypes);
 
        //fastJsonConfig
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
        fastJsonConfig.setCharset(Charset.forName("UTF-8"));
        fastJsonConfig.setSerializerFeatures(
                SerializerFeature.QuoteFieldNames, // 双引号
                SerializerFeature.WriteMapNullValue, // 输入空值字段
                SerializerFeature.WriteEnumUsingToString, // 枚举输出STRING
                SerializerFeature.WriteNullBooleanAsFalse, // 布尔类型如果为null输出false
                SerializerFeature.WriteNullListAsEmpty, // List字段如果为null输出为[]
                //SerializerFeature.WriteNullNumberAsZero, // number类型如果为null输出0
                //SerializerFeature.WriteNullStringAsEmpty, // 字符串类型如果为null输出""
                SerializerFeature.SortField, // 按字段名称排序后进行输出
                SerializerFeature.WriteDateUseDateFormat,// 设置日期格式
                SerializerFeature.DisableCircularReferenceDetect  //关闭循环引用
        );
        
        //SerializeConfig
        SerializeConfig config =new SerializeConfig();
        fastJsonConfig.setSerializeConfig(config);
        
        messageConverter.setFastJsonConfig(fastJsonConfig);
        return messageConverter;
    }
    
    
}

 

标签:FastJson,fastjson,supportedMediaTypes,配置,springframework,SerializerFeature,impor
From: https://www.cnblogs.com/hanjun0612/p/16710126.html

相关文章

  • 【第一章】frida基础配置
    python环境➜~python--versionPython3.9.13➜~pip--versionpip22.2.2fromD:\Programs\Python\Python39\lib\site-packages\pip(python3.9)安装fridapipin......
  • Android Studio 版本历史 与运行配置
    官网地址:国内访问地址:https://developer.android.google.cn/    DownloadAndroidStudio&AppTools-AndroidDevelopers(google.cn) Android开发者 | An......
  • Spring(六):Spring配置说明
    一、bean<beanid="user"class="com.jms.pojo.User"name="aaa,bbb"><constructor-argname="name"value="jms"/></bean> bean有三个属性:1.id......
  • ASP.NET Core 读取配置文件JSON 数据、数组
    配置访问接口publicIConfiguration_Config;public类名(IConfigurationConfig){_Config=Config;}配置文件数据示例{"AllowedHosts":"*","......
  • 音响配置日志
    音响配置日志开始初始化SDKInittcpNetwork-----------Currentnetworkmode:WAN-----------Networkinitializing,pleasewait...网络初始化成功!Music=D:/m1.m......
  • 在Linux环境下使用vscode配置C++调试环境
    在Linux环境下使用vscode配置C++调试环境序起因在课程CMU15445LAB0的编写以及debug过程中充斥着assert以及printf这种不优雅的debug方式,因此决定直接进行工业革命!使用......
  • 基础vue的一些知识补充
    一、:disabled该属性能接受布尔值,可以用于元素的使用。当值为true时,该元素将无法被使用,如button的disabled属性被设置为true后,将无法被点击,input的disabled属性被......
  • linux统信OS配置C#+VScode的asp.net MVC开发环境(参考Ubuntu)
    NETCore是一个免费和开源的软件框架,设计时考虑到了Linux和macOS。它是.NET框架的跨平台继承者,适用于Linux、macOS和Windows系统。Dotnet核心框架已经为引导项目提供了脚......
  • 【sonic】安装配置sonic及接入设备
    1、效果图  2、安装java15及以上版本并配置环境变量添加JAVA_HOME变量  添加CLASSPATH  添加pathhttps://blog.csdn.net/weixin_54557847/article/deta......
  • uniapp配置网络请求
    网络请求自己配置的uni网络请求 由于平台的限制,小程序项目中不支持axios,而且原生的uni.request()API功能较为简单,不支持拦截器等全局定制的功能。因......