解决@JsonInclude(JsonInclude.Include.NON_NULL)不起作用问题
作者:manonglanye 时间:2023-05-26 08:57:45
@JsonInclude(JsonInclude.Include.NON_NULL)不起作用
记录一下使用@JsonInclude(JsonInclude.Include.NON_NULL)不起作用的原因。
我想要的结果是如果只给code赋值,其他属性不赋值,那么我调用接口返回的结果里面只有code。但是遇到其他属性也存在着的情况(为null)。
原因
我在controller层使用了一个抽象controller类,该类里的A方法会把Object类对象转换成JSON对象,就是这个A方法的原因,把A去掉就可以了,直接返回ReturnResult对象。
@JsonInclude(JsonInclude.Include.NON_NULL)注解
用法含义
为null的字段不序列化
看示例
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Response {
private String name;
private String targetId;
private String characteristic;
}
假设这个Response类是返回给前端的响应。如果字段characteristic为null,加上该注解后前端拿到的响应格式应该是这个样子(为null的characteristic字段不会显示在序列化结果里):
{
"name": "xxx",
"targetId": "xxx"
}
来源:https://blog.csdn.net/manonglanye/article/details/103657927
标签:@JsonInclude,JsonInclude,Include,NON,NULL
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
浅谈关于spring profile的误解
2021-07-25 05:48:43
IDEA解决springboot热部署失效问题(推荐)
2023-08-12 10:40:49
![](https://img.aspxhome.com/file/2023/8/58148_0s.png)
C#采用FileSystemWatcher实现监视磁盘文件变更的方法
2023-12-10 02:52:49
![](https://img.aspxhome.com/file/2023/0/91260_0s.png)
C#实现网络小程序的步骤详解
2023-08-17 18:16:37
![](https://img.aspxhome.com/file/2023/9/98219_0s.png)
Unity中的静态批处理和动态批处理操作
2022-09-29 20:12:08
SpringMVC按Ctrl上传多个文件的方法
2023-11-25 07:19:04
![](https://img.aspxhome.com/file/2023/2/116172_0s.jpg)
C#图片查看器实现方法
2021-06-17 16:50:03
![](https://img.aspxhome.com/file/2023/1/72491_0s.gif)
详解Kotlin中的面向对象(二)
2021-12-29 03:10:00
Spring MVC4.1服务器端推送实现过程解析
2021-07-24 14:53:16
![](https://img.aspxhome.com/file/2023/9/69949_0s.jpg)
Java分布式事务管理框架之Seata
2023-09-28 11:50:36
![](https://img.aspxhome.com/file/2023/1/63731_0s.png)
Java使用Log4j记录日志的方法详解
2022-09-19 01:09:50
![](https://img.aspxhome.com/file/2023/6/78236_0s.png)
Android自定义输入法软键盘
2023-04-20 05:51:54
![](https://img.aspxhome.com/file/2023/1/99791_0s.jpg)
SpringBoot3.0自定stater模块的操作流程(chatGPT提供的49种场景)
2023-06-15 05:21:46
![](https://img.aspxhome.com/file/2023/3/68023_0s.png)
kafka消费者kafka-console-consumer接收不到数据的解决
2022-04-26 06:05:42
![](https://img.aspxhome.com/file/2023/3/116113_0s.png)
SpringBoot多种自定义错误页面方式小结
2021-12-12 00:56:03
Java实现中文算数验证码的实现示例(算数运算+-*/)
2023-10-23 03:08:09
![](https://img.aspxhome.com/file/2023/4/75774_0s.png)
android自定义view用path画长方形
2022-07-20 21:03:00
![](https://img.aspxhome.com/file/2023/7/89547_0s.gif)
Android升级支持库版本遇到的两个问题详解
2023-12-02 11:24:38
java中JVM中如何存取数据和相关信息详解
2023-08-10 03:49:46
![](https://img.aspxhome.com/file/2023/8/58048_0s.jpg)
dubbo服务链路跟踪方式
2023-08-24 09:54:21
![](https://img.aspxhome.com/file/2023/6/58296_0s.png)