使用JSONObject工具类
JSONObject.toJSONString(result, SerializerFeature.DisableCircularReferenceDetect);
后面需要制定SerializerFeature
直接使用Spring中的HttpMessageConverter
springboot中 :
- 继承WebMvcConfigurerAdapter
- 复写configureMessageConverters
- 定义SerializerFeature
添加配置
FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter(); //自定义配置... FastJsonConfig config = new FastJsonConfig(); SerializerFeature[] serializerFeatures = new SerializerFeature[]{ // 输出key是包含双引号 SerializerFeature.QuoteFieldNames, // 是否输出为null的字段,若为null 则显示该字段 SerializerFeature.WriteMapNullValue, // 数值字段如果为null,则输出为0 SerializerFeature.WriteNullNumberAsZero, // List字段如果为null,输出为[],而非null SerializerFeature.WriteNullListAsEmpty, // 字符类型字段如果为null,输出为"",而非null SerializerFeature.WriteNullStringAsEmpty, // Boolean字段如果为null,输出为false,而非null SerializerFeature.WriteNullBooleanAsFalse, // Date的日期转换器 SerializerFeature.WriteDateUseDateFormat, // 循环引用 SerializerFeature.DisableCircularReferenceDetect, }; config.setSerializerFeatures(serializerFeatures); config.setCharset(Charset.forName("UTF-8")); // Date 没问题 config.setDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); //config.set