SpringBoot Return to the JSON data on the page. If there is a data null, return the empty string.
SpringBoot uses Jackson to resolve JSON data.
Let’s do the configuration:
Package com.yhcode.config;
Import com.fasterxml.jackson.core.jsongnerator;
Import com.fasterxml.jackson.databind.jsonserializer;
Import com.fasterxml.jackson.dataBind.objectMapper;
Import com.fasterxml.jackson.databind.serializerProvider;
Import org.SpringFramework.Boot.autoConfigure.Condition.ConditionalonMissingBean;
Import org.springframework.context.annotation.bean;
Import org.springframework.context.annotation.configuration;
Import org.springframework.context.annotation.primary;
Import org.Springframework.http.converter.json.jackson2ObjectMapperbuilder;
Import java.io.ioException;
/**
* Process Null value returned by Jackson
*
*/
@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, JSONGENARTOR JSONGENATOR, SerializerProvider Serprovider) Throws IOEXception {
jsongenerator.writeString ("");
}
});
Return objectmapper;
}
}
This can turn the NULL value back to JSON into an empty string.