JsonView returning empty json objects
Asked Answered
W

5

9

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

Walkyrie answered 5/4, 2017 at 22:16 Comment(3)
I've the same issue. Any news regarding this ?Siskind
Do you still have this problem? Question is old and a lot of changes could be made since then.Ravioli
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.Ipoh
S
2

Probably because of spring disabled MapperFeature.DEFAULT_VIEW_INCLUSION in org.springframework.http.converter.json.Jackson2ObjectMapperBuilder#customizeDefaultFeatures

use:

spring:
  jackson:
    mapper:
      default-view-inclusion: true

or:

@Bean
public Jackson2ObjectMapperBuilderCustomizer commonJacksonObjectMapperCustomizer() {
    return b -> b.defaultViewInclusion(true);
}

Sacculus answered 23/11, 2023 at 8:4 Comment(0)
C
1

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

Curtis answered 10/11, 2017 at 11:19 Comment(1)
Yes, but here the OP has used Lombok annotation @Data for that , shouldnt be the case thoughTeressaterete
G
1

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..

Glisson answered 9/4, 2019 at 17:7 Comment(0)
D
1

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.

Domeniga answered 9/4, 2019 at 20:3 Comment(0)
M
0

In my case, add every property with @JsonView, with 2+ diffrent jsonview class to distinguish may help.

Monaural answered 19/9 at 2:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.