json序列化时忽略属性设置


@JsonInclude(JsonInclude.Include.NON_NULL)   值为null的字段不参与序列化

@JsonIgnore  每次json处理都忽略该属性

eg:

package com.example.demo.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.*;
import org.hibernate.annotations.CreationTimestamp;

import javax.persistence.*;
import java.util.Date;

@Entity
@Table(name = "users")
@Data
@Builder
@ToString(callSuper = true)
@NoArgsConstructor
@AllArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
public class Users {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int id;
    private String name;
    private int age;
    @JsonIgnore
    @Column(updatable = false)
    @CreationTimestamp
    private Date createTime;
}

补充:

  (1)为日期指定json时的格式

     @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone = "GMT+8") 
     private Date createTime;

  (2)@CreationTimestamp 及 @UpdateTimestamp 是Hibernate 提供的时间注解

  (3)@Column(nullable = false, updatable = false)  不能为空,不能修改


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM