jackson设置返回null为空字符串的操作

网友投稿 2062 2023-03-30

jackson设置返回null为空字符串的操作

jackson设置返回null为空字符串的操作

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() {

@Override

public void serialize(Object paramT, JsonGenerator paramJsonGenerator,

SerializerProvider paramSerializerProvider) throws IOException {

//设置返回null转为 空字符串""

paramJsonGenerator.writeString("");

}

});

return objectMapper;

}

}

2、设置null字段不返回给前端:

//1.实体上

@aZtUWfbKcJsonInclude(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 不序列化

来验证下结果,很显然,我们成功了,age、address、sex三个字段值为null,现在看到是空字符串:

补充知识:SpringBoot Jackson 将null转字符串"" ,List、Array转[],Int转0

如下所示:

import com.fasterxml.jackson.core.JsonGenerator;

import com.fasterxml.jackson.core.JsonProcessingException;

import com.fasterxml.jackson.databind.BeanDescription;

import com.fasterxml.jackson.databind.JsonSerializer;

import com.fasterxml.jackson.databind.SerializationConfig;

import com.fasterxml.jackson.databind.SerializerProvider;

import com.fasterxml.jackson.databind.ser.BeanPropertyWriter;

import com.fasterxml.jackson.databind.ser.BeanSerializerModifier;

import org.apache.commons.lang3.StringUtils;

import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;

import java.io.IOException;

import java.util.Collection;

import java.util.List;

/**

* 使用官方自带的json格式类库,fastjson因为content type问题时不时控制台报错、无法直接返回二进制等问题

*/

class JacksonHttpMessageConverter extends MappingJackson2HttpMessageConverter {

/**

* 处理数组类型的null值

*/

public class NullArrayJsonSerializer extends JsonSerializer {

@Override

public void serialize(Object value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {

if (value == null) {

jgen.writeStartArray();

jgen.writeEndArray();

}

}

}

/**

* 处理字符串类型的null值

*/

public class NullStringJsonSerializer extends JsonSerializer {

@Override

public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {

jsonGenerator.writeString(StringUtils.EMPTY);

}

}

/**

* 处理数字类型的null值

*/

public class NullNumberJsonSerializer extends JsonSerializer {

@Override

public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {

jsonGenerator.writeNumber(0);

}

}

/**

* 处理布尔类型的null值

*/

public class NullBooleanJsonSerializer extenhttp://ds JsonSerializer {

@Override

public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {

jsonGenerator.writeBoolean(false);

}

}

public class MyBeanSerializerModifier extends BeanSerializerModifier {

@Override

public List changeProperties(SerializationConfig config, BeanDescription beanDesc, List beanProperties) {

//循环所有的beanPropertyWriter

for (Object beanProperty : beanProperties) {

BeanPropertyWriter writer = (BeanPropertyWriter) beanProperty;

//判断字段的类型,如果是array,list,set则注册nullSerializer

if (isArrayType(writer)) {

//给writer注册一个自己的nullSerializer

writer.assignNullSerializer(new NullArrayJsonSerializer());

} else if (isNumberType(writer)) {

writer.assignNullSerializer(new NullNumberJsonSerializer());

} else if (isBooleanType(writer)) {

writer.assignNullSerializer(new NullBooleanJsonSerializer());

} else if (isStringType(writer)) {

writer.assignNullSerializer(new NullStringJsonSerializer());

}

}

return beanProperties;

}

/**

* 是否是数组

*/

private boolean isArrayType(BeanPropertyWriter writer) {

Class> clazz = writer.getType().getRawClass();

return aZtUWfbKcclazz.isArray() || Collection.class.isAssignableFrom(clazz);

}

/**

* 是否是string

*/

private boolean isStringType(BeanPropertyWriter writer) {

Class> clazz = writer.getType().getRawClass();

return CharSequence.class.isAssignableFrom(clazz) || Character.class.isAssignableFrom(clazz);

}

/**

* 是否是int

*/

private boolean isNumberType(BeanPropertyWriter writer) {

Class> clazz = writer.getType().getRawClass();

return Number.class.isAssignableFrom(clazz);

}

/**

* 是否是boolean

*/

private boolean isBooleanType(BeanPropertyWriter writer) {

Class> clazz = writer.getType().getRawClass();

return clazz.equals(Boolean.class);

}

}

JacksonHttpMessageConverter() {

getObjectMapper().setSerializerFactory(getObjectMapper().getSerializerFactory().withSerializerModifier(new MyBeanSerializerModifier()));

}

}

一定要在下面配置消息转换器,不然没效果

@Configuration

public class WebMvcConfig extends WebMvcConfigurationSupport {

@Override

protected void configureMessageConverters(List> converters) {

super.configureMessageConverters(converters);

converters.add(new JacksonHttpMessageConverter());

}

}

如果想要更丰富的可以在:MyBeanSerializerModifier中自定义。

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Jackson序列化和反序列化忽略字段操作
下一篇:电脑是如何打开小程序功能(电脑怎样打开小程序)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~