Java – jsonview annotation with include / exclude attribute
I have a use case where it seems more appropriate to use the jsonview annotation with exclusion information, for example:
@JSONView(Views.Report1.class,include=false)
This is not directly supported by Jackson (1.9.2) (including attributes), because I want to know if Jackson has a simple solution to achieve this goal
Use case:
>Report1: this view should contain ID, name, Info1, info2, info3, info4 attributes > report2: this view should have ID, info4 attributes > report [3..] Many other ideas are possible
private int id; private String name; private String info1; private String info2; //ignore if view=report2 @JsonView(ReportViews.Report2.class,include=false) private String info3; private String info4;
My use case is to exclude attributes based on views (or report IDS) Using the jsonview method, I need to add all views to the info3 attribute except report2 to exclude them inappropriate.
In this case, what should be the correct method? Custom jsonview in addition to exclusion / inclusion will be the correct solution if no similar one is already available
Solution
There seems to be no way to exclude fields from a particular view in this way
However, by using interfaces to compose views from the required elements, you can build views very flexibly
In the above example, I will try this:
public class ReportViews { public interface NeedsInfo3 {}; public static class Report1 implements NeedsInfo3 {}; public static class Report2 {}; }
Then use the field - specific view in your model
private int id; private String name; private String info1; private String info2; @JsonView(ReportViews.NeedsInfo3.class) private String info3; private String info4;