java jackson通过方法序列化json

时间:2015-07-17 06:40:21

标签: java json serialization jackson

我希望我的类可以通过getter / setter方法而不是字段声明自动序列化。

class SerializationTest{

 String notSerialized = "not serialized";

 public String getSerialized() {
    return "serialized";
 }
}

serialized应该在生成的json中可见,而notSerialized则不应该。{/ p>

2 个答案:

答案 0 :(得分:0)

您可以使用@JsonAutoDetect注释指定在类中序列化的内容。像这样:

@JsonAutoDetect( fieldVisibility = Visibility.NONE, 
                 getterVisibility = Visibility.PUBLIC_ONLY,
                 isGetterVisibility = Visibility.PUBLIC_ONLY, 
                 setterVisibility = Visibility.PUBLIC_ONLY)

class SerializationTest{

 String notSerialized = "not serialized";

 String getSerialized() {
    return "serialized";
 }
}

答案 1 :(得分:0)

您可以更改可见性设置,或者只是添加注释以指示要检测非公共字段:

@JsonProperty
String notSerialized = "not serialized";