Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

182
Vistas
JsonView returning empty json objects

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

about 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

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

about 4 years ago · Santiago Trujillo Denunciar

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.

about 4 years ago · Santiago Trujillo Denunciar

0

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

about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda