最近在项目中遇到了后台将返回数据中的null值全处理成了空字符串,导致前端在使用集合类型的时候报错,看了下当前代码处理的逻辑,是把所有的null值转换成了空字符串,对集合类型的数据没有做特别处理。
随后自己搭建了一个springboot项目来测试各种方案,最后找到了两篇文章,取长补短(虽然我不会写,但是会改啊),实现了返回数据中的null值处理,集合类型的字段返回空集合,其他类型的字段返回空字符串。
参考文章链接:
https://www.jianshu.com/p/e93a417bf36d
https://blog.csdn.net/qq_36804701/article/details/81116119
效果如下图:
下面是实现的代码(自己也不是很懂,就按自己的理解说了)。
1. MyNullArrayJsonSerializer.java
该类会将字段类型为集合类型的null值处理成空集合
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import java.io.IOException;
/**
* 处理数组类型的null值
*/
public class MyNullArrayJsonSerializer extends JsonSerializer {
@Override
public void serialize(Object value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
if (value == null) {
jgen.writeStartArray();
jgen.writeEndArray();
}
}
}
2.MyNullJsonSerializer.java
该类会将字段类型为字符串、int等类型的null值处理成空字符串
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import java.io.IOException;
/**
* 处理字符串等类型的null值
*/
public class MyNullJsonSerializer extends JsonSerializer{
@Override
public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider)
throws IOException, JsonProcessingException {
jsonGenerator.writeString("");
}
}
3.MyBeanSerializerModifier.java
该类控制将null值处理成空集合还是空字符串
package com.answer.waka.framework.platform.configuration;
import com.fasterxml.jackson.databind.BeanDescription;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializationConfig;
import com.fasterxml.jackson.databind.ser.BeanPropertyWriter;
import com.fasterxml.jackson.databind.ser.BeanSerializerModifier;
import java.util.List;
import java.util.Set;
public class MyBeanSerializerModifier extends BeanSerializerModifier {
// 数组类型
private JsonSerializer _nullArrayJsonSerializer = new MyNullArrayJsonSerializer();
// 字符串等类型
private JsonSerializer _nullJsonSerializer = new MyNullJsonSerializer();
@Override
public List<BeanPropertyWriter> changeProperties(SerializationConfig config, BeanDescription beanDesc,
List beanProperties) {
//循环所有的beanPropertyWriter
for (int i = 0; i < beanProperties.size(); i++) {
BeanPropertyWriter writer = (BeanPropertyWriter) beanProperties.get(i);
//判断字段的类型,如果是array,list,set则注册nullSerializer
if (isArrayType(writer)) {
//给writer注册一个自己的nullSerializer
writer.assignNullSerializer(this._nullArrayJsonSerializer);
} else {
writer.assignNullSerializer(this._nullJsonSerializer);
}
}
return beanProperties;
}
//判断是什么类型
protected boolean isArrayType(BeanPropertyWriter writer) {
Class clazz = writer.getPropertyType();
return clazz.isArray() || clazz.equals(List.class) || clazz.equals(Set.class);
}
}
4.JsonConfig.java
配置类,启动项目时会实例化
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
/**
* 处理返回值中的null值
*/
@Configuration
public class JsonConfig {
@Bean
public MappingJackson2HttpMessageConverter mappingJacksonHttpMessageConverter() {
final MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
ObjectMapper mapper = converter.getObjectMapper();
// 为mapper注册一个带有SerializerModifier的Factory,此modifier主要做的事情为:当序列化类型为array,list、set时,当值为空时,序列化成[]
mapper.setSerializerFactory(mapper.getSerializerFactory().withSerializerModifier(new MyBeanSerializerModifier()));
return converter;
}
}
至此,处理完成。
github地址: https://github.com/zhou1002553773/null-value-process.git