spring项目中,前端获取服务器返回的json时,里面有null值,如果没做校验,就会导致js报错,但是不推荐每个返回的地方手动处理,而是应该在框架中统一配置。
前面关于json日期格式化的文章中,提到了spring默认使用的是jackson来做json序列化,相对应的converter是MappingJackson2HttpMessageConverter,我们依旧还是要利用它提供的ObjectMapper来解决上面的问题。
方法一:
1、继承ObjectMapper,自定义mapper
packagecom.ssm.common;importcom.fasterxml.jackson.core.JsonGenerator;importcom.fasterxml.jackson.databind.DeserializationFeature;importcom.fasterxml.jackson.databind.JsonSerializer;importcom.fasterxml.jackson.databind.ObjectMapper;importcom.fasterxml.jackson.databind.SerializationFeature;importcom.fasterxml.jackson.databind.SerializerProvider;importjava.io.IOException;importjava.text.SimpleDateFormat;/*** SpringMVC中@ResponseBody返回json时,默认使用jackson,但是不会自动将null转成空字符串,
* 所以这里继承ObjectMapper重写构造方法,在构造方法中进行手动处理
* Created by SongFei on 2016/12/26.*/
public class JsonMapper extendsObjectMapper {publicJsonMapper() {super();//null值转空字符串
this.getSerializerProvider().setNullValueSerializer(new JsonSerializer() {
@Overridepublic void serialize(Object value, JsonGenerator generator, SerializerProvider provider) throwsIOException {
generator.writeString("");
}
});//不返回时间戳格式
this.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);//遇到不认识的字段,不抛异常
this.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);//指定自己想要的日期格式,如:yyyy-MM-dd HH:mm:ss
this.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
}
}
2、在配置文件中的标签中对messageconverter做特殊处理,引入自定义的ObjectMapper
application/json;charset=UTF-8
text/json;charset=UTF-8
上述两个步骤确认无误之后,应该浏览器返回就会把null转为"", (但是经过测试,如果在控制台打印,还会是null,因为还没有运行到view层呢!)
方法二:
1、将接口返回的字段为null的设置为空字符串:
importcom.fasterxml.jackson.core.JsonGenerator;importcom.fasterxml.jackson.databind.JsonSerializer;importcom.fasterxml.jackson.databind.ObjectMapper;importcom.fasterxml.jackson.databind.SerializerProvider;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;importjava.io.IOException;
@Configurationpublic classCustomConfig {
@BeanpublicObjectMapper objectMapper() {
ObjectMapper objectMapper= newObjectMapper();
objectMapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer() {
@Overridepublic voidserialize(Object paramT, JsonGenerator paramJsonGenerator,
SerializerProvider paramSerializerProvider)throwsIOException {//设置返回null转为 空字符串""
paramJsonGenerator.writeString("");
}
});returnobjectMapper;
}
}
2、设置null字段不返回给前端:
//1.实体上
@JsonInclude(Include.NON_NULL)//将该标记放在属性上,如果该属性为NULL则不参与序列化//如果放在类上边,那对这个类的全部属性起作用//Include.Include.ALWAYS 默认//Include.NON_DEFAULT 属性为默认值不序列化//Include.NON_EMPTY 属性为 空(“”) 或者为 NULL 都不序列化//Include.NON_NULL 属性为NULL 不序列化//2.代码上
ObjectMapper mapper = newObjectMapper();
mapper.setSerializationInclusion(Include.NON_NULL);//通过该方法对mapper对象进行设置,所有序列化的对象都将按改规则进行系列化//Include.Include.ALWAYS 默认//Include.NON_DEFAULT 属性为默认值不序列化//Include.NON_EMPTY 属性为 空(“”) 或者为 NULL 都不序列化//Include.NON_NULL 属性为NULL 不序列化
如果是springboot项目,就更简单了,直接在config类中对ObjectMapper操作,都不用自己重新写:
@BeanpublicMappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter= newMappingJackson2HttpMessageConverter();
List supported = new ArrayList<>();
supported.add(MediaType.APPLICATION_JSON_UTF8);
mappingJackson2HttpMessageConverter.setSupportedMediaTypes(supported);//JsonMapper
ObjectMapper objectMapper = newObjectMapper();//null值的处理,必须要放在第一步,否则会将下面的日期格式化覆盖掉//null ==> ""
objectMapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer() {
@Overridepublic void serialize(Object value, JsonGenerator gen, SerializerProvider serializers) throwsIOException {
gen.writeString("");
}
});//处理默认日期格式化:yyyy-MM-dd HH:mm:ss
objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false);
objectMapper.setDateFormat(newSimpleDateFormat(Constants.TIME_PATTERN));
mappingJackson2HttpMessageConverter.setObjectMapper(objectMapper);returnmappingJackson2HttpMessageConverter;
}
同样原理,对时间转换进行操作: https://www.cnblogs.com/fps2tao/p/13528671.html
转 : https://www.jiweichengzhu.com/article/225600001e034f2691345ac14673b108
转: https://blog.csdn.net/qq_40741855/article/details/106095808