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