杰克逊:如何仅序列化带注释的属性

杰克逊:如何仅序列化带注释的属性,第1张

杰克逊:如何仅序列化带注释属性

如果禁用所有自动检测,则应仅序列化已注释的属性-无论是属性本身还是吸气剂。这是一个简单的例子:

private ObjectMapper om;@Beforepublic void setUp() throws Exception {    om = new ObjectMapper();    // disable auto detection    om.disable(MapperFeature.AUTO_DETECT_CREATORS, MapperFeature.AUTO_DETECT_FIELDS, MapperFeature.AUTO_DETECT_GETTERS, MapperFeature.AUTO_DETECT_IS_GETTERS);    // if you want to prevent an exception when classes have no annotated properties    om.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);}@Testpublic void test() throws Exception {    BlahClass blahClass = new BlahClass(5, "email", true);    String s = om.writevalueAsString(blahClass);    System.out.println(s);}public static class BlahClass {    @JsonProperty("id")    public Integer id;    @JsonProperty("email")    public String email;    public boolean isThing;    public BlahClass(Integer id, String email, boolean thing) {        this.id = id;        this.email = email;        isThing = thing;    }}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5642585.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存