如何将POJO映射到几个JSON演示文稿?
我在用杰克逊.
我想要下面的代码
@JsonIgnorePropertiesStreamA({ "value2" })
@JsonIgnorePropertiesOtherWay({ "value3" })
public class Value {
public int value;
public int value2;
public int value3;
}
Run Code Online (Sandbox Code Playgroud)
如何与杰克逊一起做?或者其他图书馆可以做什么?
您使用JSON视图
class Views {
static class PublicView { }
static class StreamA extends PublicView { }
static class OtherWay extends PublicView { }
}
public class Value {
@JsonView(Views.PublicView.class) public int value;
@JsonView(Views.OtherWay.class) public int value2;
@JsonView(Views.StreamA.class) public int value3;
}
String json = new ObjectMapper()
.writerWithView(Views.OtherWay.class)
.writeValueAsString(valueInstance);
Run Code Online (Sandbox Code Playgroud)
请注意,这些是包容性而非排他性; 您创建一个包含所需字段的视图.