'How to hide one property in JSON response which is used in different apis (Public and Internal)

One POJO is used in different apis (Public and Internal), Need to hide a field in public api json response but it should be available in internal api response.



Solution 1:[1]

If you are using Jackson Json, you can do it using JsonViews

public class MyApiViews{
 public static class Both{}
 public static class Internal extends Both{}
public static class External extends Both{}
}

Now in your Pojo, use @JsonView on field level like

public class MyAPI{

@JsonView(MyApiViews.Both.class)
private String field1;


@JsonView(MyApiViews.Internal.class)
private String field2;

@JsonView(MyApiViews.External.class)
private String field3;

}

Now, if you are using Spring etc, you can use

@JsonView(MyApiViews.Internal.class)
@RequestMapping("/test")

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Chetan Ahirrao