This commit is contained in:
xiangwbs
2018-03-07 12:56:01 +08:00
parent 3c42a8f482
commit 0f8cd63512
3 changed files with 33 additions and 3 deletions
@@ -1,6 +1,8 @@
package com.xwbing.demo;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xwbing.domain.entity.model.EntityModel;
/**
* 项目名称: boot-module-demo
@@ -10,8 +12,8 @@ import com.alibaba.fastjson.JSONObject;
*/
public class demo {
public static void main(String[] args) {
JSONObject jsonObject = new JSONObject();
jsonObject.put("aa", null);
System.out.println(jsonObject);
EntityModel jsonObject=new EntityModel();
String s = JSONObject.toJSONString(jsonObject, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullStringAsEmpty, SerializerFeature.WriteNullListAsEmpty);
System.out.println(s);
}
}
@@ -0,0 +1,17 @@
package com.xwbing.domain.entity.model;
import java.util.List;
/**
* 说明: 实体模型
* 项目名称: boot-module-demo
* 创建时间: 2017/5/10 16:36
* 作者: xiangwb
*/
public class EntityModel {
private String character ;
private int number;
private double decimal;
private boolean b;
private List list;
}
@@ -1,5 +1,7 @@
package com.xwbing.configuration;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.xwbing.handler.LoginInterceptor;
import org.springframework.context.annotation.Bean;
@@ -102,12 +104,21 @@ public class DispatcherServletConfig extends WebMvcConfigurerAdapter {
@Bean
public HttpMessageConverter getFastJsonHttpMessageConverter() {
FastJsonHttpMessageConverter httpMessageConverter = new FastJsonHttpMessageConverter();
//设置支持的Content-Type
List<MediaType> mediaTypes = new ArrayList<>();
mediaTypes.add(MediaType.APPLICATION_JSON);
mediaTypes.add(MediaType.APPLICATION_FORM_URLENCODED);
mediaTypes.add(MediaType.APPLICATION_OCTET_STREAM);
mediaTypes.add(MediaType.TEXT_HTML);//避免IE出现下载JSON文件的情况
httpMessageConverter.setSupportedMediaTypes(mediaTypes);
//不忽略对象属性中的null值
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(
SerializerFeature.PrettyFormat,
// SerializerFeature.WriteMapNullValue,//输出所有为null的字段
SerializerFeature.WriteNullListAsEmpty,//List字段如果为null,输出为[],而非null
SerializerFeature.WriteNullStringAsEmpty);//字符类型字段如果为null,输出为"",而非null
httpMessageConverter.setFastJsonConfig(fastJsonConfig);
return httpMessageConverter;
}
}