springboot 不响应字段为空_SpringBoot项目中处理返回json的null值(springboot项目为例)...

本文介绍了如何在SpringBoot项目中处理返回的JSON数据中的null值,通过自定义配置类JsonConfig,将null转换为空字符串,减少前端处理负担。同时展示了在RespObject类中使用@JsonInclude注解避免字段为null时出现在JSON响应中。
摘要由CSDN通过智能技术生成

在后端数据接口项目开发中,经常遇到返回的数据中有null值,导致前端需要进行判断处理,否则容易出现undefined的情况,如何便捷的将null值转换为空字符串?

以SpringBoot项目为例,SSM同理。

1、新建配置类(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;

@Configuration

public class JsonConfig {

@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;

}

}

2、在启动类Application中,记得添加Scan注解,防止无法扫描到配置类。

ps:下面看下spring boot 使用 json 响应时去除 null 的字段

import java.io.Serializable;

import com.fasterxml.jackson.annotation.JsonInclude;

import com.fasterxml.jackson.annotation.JsonInclude.Include;

public class RespObject implements Serializable {

private static final long serialVersionUID = -1560603887556641494L;

....

@JsonInclude(Include.NON_NULL)

public Object respMsg;

@JsonInclude(Include.NON_NULL)

public Object respData;

....

}

总结

以上所述是小编给大家介绍的SpringBoot项目中处理返回json的null值,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对我们网站的支持!

如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

本文标题: SpringBoot项目中处理返回json的null值(springboot项目为例)

本文地址: http://www.cppcns.com/wangluo/javascript/283260.html

在Spring Boot项目,如果你发现JSON响应有category、categoryId和categoryName字段显示为null,这可能是由于数据源的问题或者是依赖注入没有正确设置导致的。Spring Boot本身支持自动装配(Autowired),但如果某个对象未被注入,它会被视为null。以下是一些解决步骤: 1. **检查数据源**: - 确保你在数据库或外部API获取的数据这三个字段是有的。如果有数据查询问题,可能需要检查DAO或Repository层的代码。 2. **依赖注入**: - 检查这些字段是否有对应的Spring Bean。如果是实体类字段,确保该类已被扫描到且配置为@Entity或@Component等注解以便于Spring管理。 ```java @Entity public class Category { private Long categoryId; private String categoryName; // getters and setters } ``` - 如果是服务类(Service)需要这个Category对象,确保你已经使用@Autowired将其注入了。 ```java @Service @Autowired public CategoryService categoryService; ``` 3. **异常处理**: - 如果是由于某些条件导致返回null,记得添加try-catch块捕获空指针异常,并提供默认或者明确地返回错误信息。 4. **Lombok辅助**: 使用Lombok的@NonNull注解可以帮助标记字段不应该为null,但这不会强制Spring注入非null,只是提示你的代码可能存在问题。 5. **使用Optional**: 对于可能存在null的对象,考虑使用Java 8的Optional类,这样可以在处理结果时更清晰地表达意图。 最后,如果你是在控制器层遇到这个问题,确保Controller类里的getters和setters已经被Spring扫描到了。检查是否正确映射了HTTP请求到业务逻辑,并确认是否正确返回响应
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值