java build返回空值_spring boot 下对JSON返回值去除null和空字段操作

在开发过程中,我们需要统一返回前端json格式的数据,但有些接口的返回值存在 null或者""这种没有意义的字段。

不仅影响理解,还浪费带宽,这时我们可以统一做一下处理,不返回空字段,或者把NULL转成“”,spring 内置的json处理框架是Jackson。我们可以对它配置一下达到目的

直接看代码,很简单.

/**

* 〈返回json空值去掉null和""〉 〈功能详细描述〉

*

* @author gogym

* @version 2017年10月13日

* @see JacksonConfig

* @since

*/

@Configuration

public class JacksonConfig

{

@Bean

@Primary

@ConditionalOnMissingBean(ObjectMapper.class)

public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder)

{

ObjectMapper objectMapper = builder.createXmlMapper(false).build();

// 通过该方法对mapper对象进行设置,所有序列化的对象都将按改规则进行系列化

// Include.Include.ALWAYS 默认

// Include.NON_DEFAULT 属性为默认值不序列化

// Include.NON_EMPTY 属性为 空("") 或者为 NULL 都不序列化,则返回的json是没有这个字段的。这样对移动端会更省流量

// Include.NON_NULL 属性为NULL 不序列化,就是为null的字段不参加序列化

//objectMapper.setSerializationInclusion(Include.NON_EMPTY);

// 字段保留,将null值转为""

objectMapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer()

{

@Override

public void serialize(Object o, JsonGenerator jsonGenerator,

SerializerProvider serializerProvider)

throws IOException, JsonProcessingException

{

jsonGenerator.writeString("");

}

});

return objectMapper;

}

}

补充知识:springboot RestController 配置fastjson,实体为null时不显示问题

Springboot 在和fastjson配合使用时,当返回实体为空时拦截不显示问题。在实际业务中,不管返回实体是否为空,都需要显示出来,如果为空则显示null。

解决方案,引入fastjson jar包

com.alibaba

fastjson

1.2.22

添加配置ResultConfig:

package com.message.config;

/**

* @author :zoboy

* @Description:

* @ Date: Created in 2019-11-18 10:29

*/

import com.alibaba.fastjson.serializer.SerializerFeature;

import com.alibaba.fastjson.support.config.FastJsonConfig;

import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;

import org.springframework.boot.autoconfigure.http.HttpMessageConverters;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import org.springframework.http.MediaType;

import org.springframework.http.converter.HttpMessageConverter;

import java.util.ArrayList;

import java.util.List;

@Configuration

public class ResultConfig {

/*注入Bean : HttpMessageConverters,以支持fastjson*/

@Bean

public HttpMessageConverters fastJsonHttpMessageConverters() {

FastJsonHttpMessageConverter fastConvert = new FastJsonHttpMessageConverter();

FastJsonConfig fastJsonConfig = new FastJsonConfig();

fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat,

SerializerFeature.WriteNullStringAsEmpty,

SerializerFeature.WriteNullNumberAsZero,

SerializerFeature.WriteNullListAsEmpty,

SerializerFeature.WriteMapNullValue,

SerializerFeature.DisableCheckSpecialChar);

fastJsonConfig.setDateFormat("yyyy-MM-dd hh:mm:ss");

//处理中文乱码问题

List fastMediaTypes = new ArrayList<>();

fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);

fastConvert.setSupportedMediaTypes(fastMediaTypes);

fastConvert.setFastJsonConfig(fastJsonConfig);

return new HttpMessageConverters((HttpMessageConverter>) fastConvert);

}

}

结果:

{

"code": "0",

"message": "成功!",

"data": null

}

解决问题!

以上这篇spring boot 下对JSON返回值去除null和空字段操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值