In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
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!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.