8

I am trying to implement a JsonView to selectively serialize fields from an entity but the json that is serialized has empty objects with no fields. Below is my code:

ViewClass:

public class AuditReportView {
   public interface Summary {}
}

Entity:

@Entity
@SequenceGenerator(name = "AUDIT_REPORT_SEQUENCE_GENERATOR", sequenceName = "EJB_AUDIT_REPORT_SEQ", initialValue = 1, allocationSize = 1)
@Table(name = "DEVICE_AUDIT_REPORT")
@Data
public class AuditReport implements Serializable {

   private static final long serialVersionUID = 1246376778314918671L;

   @Id
   @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "AUDIT_REPORT_SEQUENCE_GENERATOR")
   @Column(name = "ID", nullable = false)
   @JsonView(AuditReportView.Summary.class)
   private Long id;

   @Column(name = "DEVICE_ID", nullable = false)
   @JsonView(AuditReportView.Summary.class)
   private String deviceId;

   @Column(name = "REPORT_TIMESTAMP", nullable = false)
   @JsonView(AuditReportView.Summary.class)
   private Calendar reportTimestamp;

   @Column(name = "USER_ID", nullable = false)
   @JsonView(AuditReportView.Summary.class)
   private long userId;

   @Column(name = "USERNAME", nullable = false)
   @JsonView(AuditReportView.Summary.class)
   private String username;

   @Column(name = "START_DATE", nullable = false)
   @JsonView(AuditReportView.Summary.class)
   private Calendar startDate;

   @Column(name = "END_DATE", nullable = false)
   @JsonView(AuditReportView.Summary.class)
   private Calendar endDate;

   @OneToMany(mappedBy = "auditReport", fetch = FetchType.LAZY, orphanRemoval = true, cascade={CascadeType.ALL})
   private Set<AuditEntry> auditEntries = new HashSet<AuditEntry>();
}

Controller:

   @JsonView(AuditReportView.Summary.class)
   @RequestMapping(method = RequestMethod.GET, value = "auditReportSummary")
   public @ResponseBody ResponseEntity<?> getAuditReportSummary()
   {
      final List<AuditReport> auditReports = auditDAO.getAuditReportSummary();

      return new ResponseEntity<>(auditReports, HttpStatus.OK);
   }

Json from Postman:

[
  {},
  {},
  {}
]

The database only has 3 results and when I debug it is definately pulling them out, it is just that no members are being serialized. I'm using Spring 4.3.7 and Jackson 2.8.7. Any ideas of what could be wrong or where to start debugging the issue?

Thanks

Jason
  • 220
  • 1
  • 14
  • I've the same issue. Any news regarding this ? – Marcel Jul 03 '17 at 09:14
  • Do you still have this problem? Question is old and a lot of changes could be made since then. – Michał Ziober Apr 02 '19 at 23:16
  • Hi @Json could you please paste the imports as well? I can see an '@Data' annotation, but I am not sure if it is the lombok one, if it is not, the getters and setters are missing. – lprakashv Apr 09 '19 at 05:45

3 Answers3

0

You must create getters and setters methods for attributes. I did it and it worked.

0

I guess the issue is due to the @ResponceBody ResponseEntity<?> Please try with the following code :

   @JsonView(AuditReportView.Summary.class)
   @RequestMapping(method = RequestMethod.GET, value = "auditReportSummary" produces = MediaType.APPLICATION_JSON_VALUE)
   public List<AuditReport getAuditReportSummary()
   {
      final List<AuditReport> auditReports = auditDAO.getAuditReportSummary();

      return auditReports;
   }

I am not much sure about it, but you can try if it works..

Ajinkya
  • 325
  • 3
  • 12
0

Try adding a default constructor - ex:

public AuditReport() {}

The default constructor is generated by the java compiler if no custom constructor is specified in the code. However if a custom constructor is specified, the default constructor is no longer automatically added which can break serialization libraries / spring, etc..

BUT - you haven't specified a constructor - how could this be?

One thing I noticed is that you're using Lombok - due to the Data annotation. Lombok can generate constructors for classes. So its possible one of the annotations or libraries you're using is adding a constructor, making the compiler skip generation of a default constructor, which may be breaking your serialization.

So, I hope adding a default constructor works out for you.

TheJeff
  • 3,665
  • 34
  • 52