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
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..
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.
You must create getters and setters methods for attributes. I did it and it worked.