在項(xiàng)目開(kāi)發(fā)中,我們返回的數(shù)據(jù)或者對(duì)象沒(méi)有的時(shí)候一般直接返回的null
有數(shù)據(jù)時(shí)的返回值
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
{ "flag" : true , "code" : "10000" , "msg" : "成功!" , "data" : { "id" : 32, "templateType" : 1, "templateName" : "我的測(cè)試模板1" , "freightName" : "我的測(cè)試標(biāo)題1" , "listArea" : [ { "id" : 968, "templateId" : 32, "freightPrice" : 15, } ], "templateDescEntity" : { "id" : 1 "name" : "xxx" } } } |
沒(méi)有數(shù)據(jù)時(shí)的返回值
1
2
3
4
5
6
7
8
9
10
11
12
13
|
{ "flag" : true , "code" : "10000" , "msg" : "成功!" , "data" : { "id" : 32, "templateType" : 1, "templateName" : null , "freightName" : null , "listArea" : null , "templateDescEntity" : null } } |
這種情況下數(shù)據(jù)返回給前端,前端需要做大量的空值判斷
如前端調(diào)使用屬性data.templateDescEntity.id的時(shí)候就會(huì)直接報(bào)異常
此時(shí)我們可以使用返回值統(tǒng)一處理,配置如下
pom.xml添加
1
2
3
4
5
6
7
8
9
|
< dependency > < groupId >com.fasterxml.jackson.core</ groupId > < artifactId >jackson-annotations</ artifactId > < version >2.9.5</ version > </ dependency > < dependency > < groupId >com.fasterxml.jackson.core</ groupId > < artifactId >jackson-databind</ artifactId > </ dependency > |
java類(lèi)添加配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
package com.ys.mall.core.product.config; import com.fasterxml.jackson.core.JsonGenerator; 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; import java.lang.reflect.Field; import java.util.Collection; import java.util.List; import java.util.Map; import java.util.Objects; /** * 數(shù)據(jù)返回給前端時(shí),設(shè)置null值默認(rèn)為"" * * @author cgh * @date 2020/12/14 10:35 */ @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 { String fieldName = jsonGenerator.getOutputContext().getCurrentName(); try { //反射獲取字段類(lèi)型 Field field = jsonGenerator.getCurrentValue().getClass().getDeclaredField(fieldName); if (CharSequence. class .isAssignableFrom(field.getType())) { //字符串型空值"" jsonGenerator.writeString( "" ); return ; } else if (Collection. class .isAssignableFrom(field.getType())) { //列表型空值返回[] jsonGenerator.writeStartArray(); jsonGenerator.writeEndArray(); return ; } else if (Map. class .isAssignableFrom(field.getType())) { //map型空值 或者 bean對(duì)象 返回{} jsonGenerator.writeStartObject(); jsonGenerator.writeEndObject(); return ; } } catch (NoSuchFieldException ignored) { } jsonGenerator.writeString( "" ); } }); return objectMapper; } } |
添加空值統(tǒng)一處理后的返回值
1
2
3
4
5
6
7
8
9
10
11
12
13
|
{ "flag" : true , "code" : "10000" , "msg" : "成功!" , "data" : { "id" : 32, "templateType" : 1, "templateName" : "" , "freightName" : "" , "listArea" : [], "templateDescEntity" : {} } } |
到此這篇關(guān)于springboot配置Jackson返回統(tǒng)一默認(rèn)值的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)springboot Jackson返回統(tǒng)一默認(rèn)值內(nèi)容請(qǐng)搜索服務(wù)器之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持服務(wù)器之家!
原文鏈接:https://juejin.cn/post/6989848776361902088