Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the filtering method of springboot response json null value

Shulou Source: shulou.com Published: 2022-06-02 09:37:15 10月11日 Update

This article introduces the relevant knowledge of "what is the filtering method of springboot response json null value". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Springboot response json null value filter spring: jackson: default-property-inclusion: non_null

You only need to configure the above in application.yml.

Springboot processing returns a null value of json

In the development of the back-end data interface project, it is often encountered that there are null values in the returned data, which leads to the need for judgment and processing at the front end, otherwise it is easy to occur in the case of undefined, how to easily convert null values into empty strings?

Take the SpringBoot project as an example, SSM is the same.

1. Create a new configuration class (JsonConfig.java) import com.fasterxml.jackson.core.JsonGenerator;import com.fasterxml.jackson.core.JsonProcessingException;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;@Configurationpublic class JsonConfig {@ Bean @ Primary @ ConditionalOnMissingBean (ObjectMapper.class) public ObjectMapper jacksonObjectMapper (Jackson2ObjectMapperBuilder builder) {ObjectMapper objectMapper = builder.createXmlMapper (false) .build () / / set the mapper object through this method, and all serialized objects will be serialized according to the change rule / / Include.Include.ALWAYS default / / Include.NON_DEFAULT property is the default value / / Include.NON_EMPTY property is empty ("") or NULL is not serialized, then the returned json does not have this field. This will save more traffic on the mobile side / / Include.NON_NULL attribute is NULL does not serialize, that is, the field for null does not participate in serialization / / objectMapper.setSerializationInclusion (Include.NON_EMPTY) / / keep the null value to "" objectMapper.getSerializerProvider () .setNullValueSerializer (new JsonSerializer () {@ Override public void serialize (Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {jsonGenerator.writeString ("")) }}); return objectMapper;}} 2, in the startup class Application

Remember to add Scan annotations to prevent the configuration class from being scanned.

This is the end of the content of "what is the filtering method of springboot response json null value". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Sequence content fields properties configuration method object situation data more knowledge project processing convenience practicality achievement next front end dilemma reality Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Shulou Technology NVidia Shulou Information vpn