springboot null values returned in json empty string conversion

In a real project, we will inevitably encounter some no value. When we turn JSON, do not want these null occurs, for example, we expect all of null on the turn JSON have become "" "" this empty string, then how to do it?

Jackson's handling of null

 1 @Configuration
 2 public class JacksonConfig {
 3     @Bean
 4     @Primary
 5     @ConditionalOnMissingBean(ObjectMapper.class)
 6     public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
 7         ObjectMapper objectMapper = builder.createXmlMapper(false).build();
 8         objectMapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>() {
 9             @Override
10             public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
11                 jsonGenerator.writeString("");
12             }
13         });
14         return objectMapper;
15     }
16 }

 

fastjson

Use fastjson need to import-dependent (https://mvnrepository.com/search?q=fastjson)

1 <dependency>
2     <groupId>com.alibaba</groupId>
3     <artifactId>fastjson</artifactId>
4     <version>1.2.58</version>
5 </dependency>

When using fastjson, handling of null and Jackson is somewhat different, we need to inherit WebMvcConfigurationSupport class, then covered configureMessageConverters method. In the process, we can select the scene you want to achieve null conversion can be configured.

 1 import com.alibaba.fastjson.serializer.SerializerFeature;
 2 import com.alibaba.fastjson.support.config.FastJsonConfig;
 3 import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
 4 import org.springframework.context.annotation.Configuration;
 5 import org.springframework.http.MediaType;
 6 import org.springframework.http.converter.HttpMessageConverter;
 7 import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
 8 
 9 import java.nio.charset.Charset;
10 import java.util.ArrayList;
11 import java.util.List;
12 
13 @Configuration
14 public class fastJsonConfig extends WebMvcConfigurationSupport {
15 
16     /**
17      * 使用阿里 fastjson 作为 JSON MessageConverter
18      * @param converters
19      */
20     @Override
21     public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
22         FastJsonHttpMessageConverter converter = newFastJsonHttpMessageConverter ();
 23 is          FastJsonConfig config = new new FastJsonConfig ();
 24          config.setSerializerFeatures (
 25                  // reserved Map field blank 
26 is                  SerializerFeature.WriteMapNullValue,
 27                  // be converted into String type null "" 
28                  SerializerFeature.WriteNullStringAsEmpty,
 29                  / / the null type Number converted to 0 
30                  SerializerFeature.WriteNullNumberAsZero,
 31 is                  // the type of List converted to null [] 
32                  SerializerFeature.WriteNullListAsEmpty,
 33 is                  //The null is turned into a Boolean to false 
34 is                  SerializerFeature.WriteNullBooleanAsFalse,
 35                  // avoid circular references 
36                  SerializerFeature.DisableCircularReferenceDetect);
 37 [  
38 is          converter.setFastJsonConfig (config);
 39          converter.setDefaultCharset (Charset.forName ( "UTF-. 8" )) ;
 40          List <the MediaType> = mediaTypeList new new the ArrayList <> ();
 41 is          // solve the Chinese garbled, equivalent to adding attributes produces on the Controller in @RequestMapping = "file application / JSON" 
42 is          mediaTypeList.add (the MediaType. APPLICATION_JSON);
 43         converter.setSupportedMediaTypes(mediaTypeList);
44         converters.add(converter);
45     }
46 }

 

Jackson VS fastjson

Options FASTJSON Jackson
The degree of difficulty to get started easily medium
Advanced features support medium rich
Official documents, examples of support Chinese English
JSON processing speed Slightly faster fast

Contrasting Jackson and fastjson, and there is a lot of information can be viewed online, you can select the appropriate framework according to their actual situation. From the expansion point of view, fastjson Jackson is not flexible, difficult to get started, or from the speed point of view, fastjson can be considered, it is also more convenient .

 

Guess you like

Origin www.cnblogs.com/long88-club/p/11361174.html