Total Pageviews

2018/08/13

[JSON] How to ignore Inheritance properties with Jackson

How-To
AbstractCommonEntity Superclass:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
  package com.xxx.entity;
  
  
  import java.io.Serializable;
  import java.util.Date;
  
  import javax.persistence.Column;
  import javax.persistence.MappedSuperclass;
  
  import org.codehaus.jackson.annotate.JsonIgnoreProperties;
  import org.hibernate.annotations.GenerationTime;
  import org.hibernate.annotations.GeneratorType;
  import org.springframework.data.annotation.LastModifiedDate;
  
  import com.xxx.LoggedUserGenerator;
  
  import lombok.Data;
  
  @Data
  @MappedSuperclass
  public abstract class AbstractCommonEntity implements Serializable {
  
      private static final long serialVersionUID = 1L;
  
      @GeneratorType(type = LoggedUserGenerator.class, when = GenerationTime.ALWAYS)
      @Column(name = "LAST_MODIFIED_BY")
      private String lastModifiedBy;
  
      @LastModifiedDate
      @Column(name = "LAST_MODIFIED_DATETIME")
      private Date lastModifiedDatetime;
  
  }

Action subclass:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
  package com.xxx.entity;
  
  import java.io.Serializable;
  
  import javax.persistence.Column;
  import javax.persistence.Entity;
  import javax.persistence.EntityListeners;
  import javax.persistence.GeneratedValue;
  import javax.persistence.GenerationType;
  import javax.persistence.Id;
  import javax.persistence.SequenceGenerator;
  import javax.persistence.Table;
  
  import org.springframework.data.jpa.domain.support.AuditingEntityListener;
  
  import lombok.Data;
  import lombok.EqualsAndHashCode;
  
  @Entity
  @Table(name = "ACTION")
  @Data
  @EntityListeners(AuditingEntityListener.class)
  @EqualsAndHashCode(of = "id", callSuper = false)
  public class Action extends AbstractCommonEntity implements Serializable {
  
      private static final long serialVersionUID = 1L;
      
      @Id
      @SequenceGenerator(name = "ACTION_ID_SEQ", sequenceName = "ACTION_ID_SEQ", allocationSize = 1)
      @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ACTION_ID_SEQ")
      @Column(name = "ID")
      private Integer id;
      
      @Column(name = "NAME")
      private String name;
      
      @Column(name = "DEPEND_SLOTS")
      private String dependSlots;
  
      @Column(name = "PROJECT_ID")
      private Integer projectId;
  
  }

If I would like to genenrate JSON from Action subclass and ignore properties from its superclass, how to do it?

How-To
Simply add @JsonIgnoreProperties in superclass:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
  package com.xxx.entity;
  
  
  import java.io.Serializable;
  import java.util.Date;
  
  import javax.persistence.Column;
  import javax.persistence.MappedSuperclass;
  
  import org.codehaus.jackson.annotate.JsonIgnoreProperties;
  import org.hibernate.annotations.GenerationTime;
  import org.hibernate.annotations.GeneratorType;
  import org.springframework.data.annotation.LastModifiedDate;
  
  import com.xxx.LoggedUserGenerator;
  
  import lombok.Data;
  
  @Data
  @MappedSuperclass
  @JsonIgnoreProperties({ "lastModifiedBy", "lastModifiedDatetime" })
  public abstract class AbstractCommonEntity implements Serializable {
  
      private static final long serialVersionUID = 1L;
  
      @GeneratorType(type = LoggedUserGenerator.class, when = GenerationTime.ALWAYS)
      @Column(name = "LAST_MODIFIED_BY")
      private String lastModifiedBy;
  
      @LastModifiedDate
      @Column(name = "LAST_MODIFIED_DATETIME")
      private Date lastModifiedDatetime;
  
  }




No comments: