如果禁用所有自动检测,则应仅序列化已注释的属性-无论是属性本身还是吸气剂。这是一个简单的例子:
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; }}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)