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

Viewed 32

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.

1 Answers

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")
Related