The first embodiment: SpringBoot Jackson used to null values were converted to ""
Preface: inevitably encounter null values appear in the actual project, but we do not want to turn json NULL values appear, but will be converted to NULL value "" The empty string. So, how should we deal with it? In SpringBoot, the configuration can create a new class.
@Configuration public class JacksonConfig { @Bean @Primary @ConditionalOnMissingBean(ObjectMapper.class) public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) { ObjectMapper objectMapper = builder.createXmlMapper(false).build(); objectMapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>() { @Override public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException { jsonGenerator.writeString(""); } }); return objectMapper; } }
Thus, when it came to a NULL value, it will be converted to form "" of.
The second way:
That is, yml configuration file:
spring: jackson: default-property-inclusion: non_null
The third way:
It is to use annotations in the entity classes:
@JsonInclude(JsonInclude.Include.NON_NULL)