@JsonSerialize注解的include方法和Inclusion枚举都被标注已经过期,不建议使用,@JsonSerialize的替代品是什么?

1.@JsonSerialize 是 Jackson 库中的一个注解,用于指定在将 Java 对象序列化为 JSON 格式时,如何进行自定义的序列化处理。通过这个注解,指定一个自定义的序列化器(serializer),以控制对象的序列化过程。

2.@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)的替代品为@JsonInclude(JsonInclude.Include.NON_NULL),上述的注解的作用是自定义在对象序列化过程中若属性字段值为Null,则步进行序列化处理。

替换如下:

原@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)使用如下:

package com.hz.pojo;

import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import lombok.Data;

import java.io.Serializable;

@Data
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
public class User implements Serializable {
    private static final long serialVersionUID = -8858768106901204002L;
    Integer id;
    String name;
    String age;
    String sex;
}

替换为@JsonInclude(JsonInclude.Include.NON_NULL)的使用如下:

         

package com.hz.pojo;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.Data;

import java.io.Serializable;

@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class User implements Serializable {
    private static final long serialVersionUID = -8858768106901204002L;
    Integer id;
    String name;
    String age;
    String sex;
}

 

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现在使用@JsonFormat注解时,时分秒都为0,可以按照以下步骤进行处理: 1. 在项目中添加jackson插件的依赖。可以在pom.xml文件中添加以下代码: ``` <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> </dependency> ``` 2. 在需要格式化日期的实体类中,使用@JsonFormat注解并指定日期格式和时区。例如: ``` @JsonFormat(pattern = "yyyy-MM-dd 00:00:00", timezone = "GMT+8") private Date updateTime; ``` 3. 如果想要项目配置文件统一处理日期格式,可以在配置文件中添加以下配置: ``` spring: jackson: time-zone: GMT+8 date-format: yyyy-MM-dd 00:00:00 default-property-inclusion: non_empty ``` 这样,无论在哪个实体类中使用@JsonFormat注解,都会自动将时分秒设置为0。 请注意,以上步骤针对的是在Java代码中使用@JsonFormat注解的情况。如果是在JavaScript中对JSON日期进行格式化,可以参考中提到的方法来实现。 : Springboot前后端时间传输 @JsonFormat 得到时分秒为00:00:00 处理 : 无时分秒 function jsonDateFormat(jsonDate) {//json日期格式转换为正常格式 try { var date = new Date(parseInt(jsonDate.replace("/Date(", "").replace(")/", ""), 10)); var month = date.getMonth() ... : 自己实现的一个功能里,需要拿到数据库里的时间,根据网上搜到的博客, java 实体类中日期属性格式化 @JSONFORMAT 日期格式自动格式化。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [JAVA----Springboot前后端时间传输 @JsonFormat 得到时分秒为00:00:00](https://blog.csdn.net/damys/article/details/114026091)[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^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [Javascript将JSON日期格式化](https://download.csdn.net/download/weixin_38625708/13138876)[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^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [springboot前后端时间传输,@JsonFormat 得到时分秒为00:00:00,拿不到时分秒 解决办法](https://blog.csdn.net/godelgnis/article/details/88656298)[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^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值