SpringBoot 配置不返回为 null 的属性

前言:

在设计接口返回的时候,有些字段因为没有用到所以会是 null,这些字段直接返回会给前端会造成不必要的麻烦,而且有违程序猿审美,必须隐藏掉;同时还有另一种场景,某些字段在逻辑处理的时候是有用的,但是不能展示给前端,同样也需要隐藏掉。

一、 隐藏指定属性

Spring 注解 @JsonIgnore,只要加在指定属性上即可完成 json 序列化时自动忽略。

public class Data {

    private Integer id;
    private String info;

    @JsonIgnore
    private LocalDateTime localDateTime;
}

二、隐藏为 null 的字段

1. 方式一

bean 类上加注解 @JsonInclude(JsonInclude.Include.NON_NULL)

@JsonInclude(JsonInclude.Include.NON_NULL)
public class Data {

    private Integer id;
    private String info;
    private LocalDateTime localDateTime;
}

缺陷:所有涉及的 bean 都需要手动添加。

2. 方式二

在配置文件加入全局配置,如下

spring:
  jackson:
    # json 序列化排除值为 null 的属性
    default-property-inclusion: non_null 
    # 配置 Date 类的时间格式,如果不涉及可以不加
    date-format: yyyy-MM-dd HH:mm:ss
    # 配置 Date 类的时区,如果不涉及可以不加
    time-zone: GMT+8  

三、配置失效问题

很大可能出现的问题是上述方式二失效,或者换句话说是配置文件失效,目前这个情况在 springboot2.x 版本中出现的很频繁,这个主要涉及到的是 @EnableWebMvc 注解和 WebMvcConfigurationSupport (拦截器经常使用)类,追根究底是 WebMvcConfigurationSupport 导致自动配置不生效(@EnableWebMvc  注解引入的类继承了WebMvcConfigurationSupport),这块问题可以在网上查看一下解决方案,不想去查的可以参考以下解决方案:

1. 工程里面不要有 @EnableWebMvc 注解

2. 拦截器不要继承 WebMvcConfigurationSupport ,推荐实现 WebMvcConfigurer 接口

WebMvcConfigurationSupport 导致配置不生效可参考一下博客:

1.继承WebMvcConfigurationSupport后自动配置不生效的问题及如何配置拦截器_霓虹深处-CSDN博客

2. SpringBoot2继承了WebMvcConfigurationSupport后,WebMvcAutoConfiguration自动配置失效 - 菜鸟_L - 博客园 (cnblogs.com)

  • 9
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
回答: 当使用Spring Boot开发时,理论上是不会出现乱码的,因为Spring Boot默认编码为UTF-8。但是当客户端编码和服务器编码格式不一致时,就会导致乱码。解决这个问题有几种方法。首先,可以和客户端约定请求编码格式,强制约定为UTF-8。\[1\]其次,可以修改Spring Boot的配置文件,增加如下配置来设置编码格式为UTF-8:spring.http.encoding.force=true、spring.http.encoding.charset=UTF-8、spring.http.encoding.enabled=true、server.tomcat.uri-encoding=UTF-8。\[2\]另外,也可以在Configuration类中创建一个自定义的CharacterEncodingFilter来设置编码格式为UTF-8。\[3\]此外,在RequestMapping上添加produces属性,指定返回的数据类型为application/json;charset=utf-8,可以防止中文乱码。\[3\]如果还是出现乱码,可能是读取properties文件时出现了错误。 #### 引用[.reference_title] - *1* *2* [SpringBoot乱码问题解决](https://blog.csdn.net/qw463800202/article/details/103158653)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [解决SpringBoot返回中文乱码问题方法汇总](https://blog.csdn.net/m0_59278919/article/details/127340225)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值