我正在尝试实现一个 JsonView 来选择性地序列化实体中的字段,但是被序列化的 json 具有没有字段的空对象。下面是我的代码:
视图类:
public class AuditReportView {
public interface Summary {}
}
实体:
@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>();
}
控制器:
@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:
[
{},
{},
{}
]
数据库只有 3 个结果,当我调试时肯定会将它们拉出来,只是没有成员被序列化。我正在使用 Spring 4.3.7 和 Jackson 2.8.7。关于可能出现什么问题或从哪里开始调试问题的任何想法?
谢谢