I have the following JsonViews defined:
public class EntityJsonView {
public static class Detailed extends Abbreviated {
}
public static class AuditedDetailed extends Detailed {
}
public static class Abbreviated {
}
}
Then I have these classes:
public Class Customer {
@JsonView(EntityJsonView.Abbreviated.class)
private Integer id;
@JsonView(EntityJsonView.Abbreviated.class)
private String name;
@JsonView(EntityJsonView.Detailed.class)
private String phone;
@JsonView(EntityJsonView.Detailed.class)
private List<Invoice> invoices;
}
public Class Invoice {
@JsonView(EntityJsonView.Abbreviated.class)
private Integer id;
@JsonView(EntityJsonView.Detailed.class)
private Customer customer;
@JsonView(EntityJsonView.Detailed.class)
private Employee salesman;
@JsonView(EntityJsonView.Abbreviated.class)
private Date invoiceDate;
@JsonView(EntityJsonView.Abbreviated.class)
private Double amount;
}
I return my customer list like this:
@JsonView(EntityJsonView.Detailed.class)
public ResponseEntity<List<Customer>> getCustomerList() {
List<Customer> custs = customerService.getAll();
return new ResponseEntity<List<Customer>>(custs , HttpStatus.OK);
}
While I want the Customer instances to be serialized using the Detailed view, I want the nested Invoice instances to be serialized using the Abbreviated view. By the same token, when I serialize a list of Invoices using the Detailed view, I want the nested Customer instances to be serialized using the Abbreviated view. This is not just a problem of recursion because there are lots of other attributes I want to remove as well.
I've searched high and low for a solution but perhaps I'm not using the right keywords.
My predecessor in this job accomplished this using @JsonIgnoreProperties but that is proving to be a maintenance problem. When a new attribute is added to a class, I have to hunt down all the ignore lists and decide if it needs to be ignored or not. It would be easier if there was a corresponding @JsonIncludeProperties.
Has anyone found a better way to accomplish this?