springboot配置Jackson返回统一默认值的实现示例

网友投稿 248 2022-12-24

springboot配置Jackson返回统一默认值的实现示例

在项目开发中,我们返回的数据或者对象没有的时候一般直接返回的null

有数据时的返回值

{

"flag": true,

"code": "10000",

"msg": "成功!",

"data": {

"id": 32,

"templateType": 1,

"templateName": "我的测试模板1",

"freightName": "我的测试标题1",

"listArea": [

{

"id": 968,

"templateId": 32,

"freightPrice": 15,

}

],

"templateDescEntity": {

"id": 1

"name": "xxx"

}

}

}

没有数据时的返回值

{

"flag": true,

"code": "10000",

"msg": "成功!",

"data": {

"id": 32,

"templateType": 1,

"templateName": null,

"freightName": null,

"listArea": null,

"templateDescEntity": null

}

}

这种情况下数据返回给前端,前端需要做大量的空值判断

如前端调使用属性data.templateDescEntity.id的时候就会直接报异常

此时我们可以使用返回值统一处理,配置如下

pom.xml添加

com.fasterxml.jackson.core

jackson-annotations

2.9.5

com.fasterxml.jackson.core

jackson-databind

java类添加配置

package com.ys.mall.core.product.config;

import com.fasterxml.jackson.core.jsonGenerator;

import com.fasterxml.jacnQImXkson.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;

/**

* 数据返回给前端时,设置null值默认为""

*

* @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() {

@Override

public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {

String fieldName = jsonGenerator.getOutputContext().getCurrentName();

try {

//反射获取字段类型

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;

nQImX } else if (Map.class.isAssignableFrom(field.getType())) {

//map型空值 或者 bean对象 返回{}

jsonGenerator.writeStartObject();

jsonGenerator.writeEndObject();

return;

}

} catch (NoSuchFieldException ignored) {

}

jsonGenerator.writeString("");

}

});

return objectMapper;

}

}

添加空值统一处理后的返回值

{

"flag": true,

"code": "10000",

"msg": "成功!",

"data": {

"id": 32,

"templateType": 1,

"templateName": "",

"freightName": "",

"listArea": [],

"templateDescEntity": {}

}

}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:SpringBoot如何返回页面的实现方法
下一篇:使用maven打包生成doc文档和打包源码
相关文章

 发表评论

暂时没有评论,来抢沙发吧~