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)  不能为空,不能修改

posted @ 2020-04-09 23:04  慕尘  阅读(3650)  评论(0编辑  收藏  举报