,您可以指定@JsonIgnore
標註在方法或@JsonIgnoreProperties(value = {"myfield"})
註釋的類。
see examples here
編輯:您使用的 傑克遜的版本?因爲我正在使用的(2.5)使用@JsonIgnore
與@JsonProperty
一起工作完美。 還注意到制定者需要接收的參數實際上是由傑克遜
接口與固定的二傳手使用:
@JsonAutoDetect(fieldVisibility = Visibility.NONE)
public interface MyWrapper {
@JsonIgnore
String getMyField();
// annotation to deserialize
@JsonProperty("my_field")
void setMyField(String f);
}
實現(平平淡淡這裏)
public class Foo implements MyWrapper {
private String myField;
public Foo() {}
public Foo(String f) {
setMyField(f);
}
@Override
public String getMyField() {
return myField;
}
@Override
public void setMyField(String f) {
myField = f;
}
}
測試:
public static void main(String[] args) {
ObjectMapper mapper = new ObjectMapper();
// serialization - ignore field
try {
MyWrapper w = new Foo("value");
String json = mapper.writeValueAsString(w);
System.out.println("serialized MyWrapper: " + json);
} catch (Exception e) {
e.printStackTrace();
}
// de-serialization - read field
String json = "{\"my_field\":\"value\"}";
try (InputStream is = new ByteArrayInputStream(json.getBytes("UTF-8"))) {
MyWrapper w = (MyWrapper)mapper.readValue(is, Foo.class);
System.out.println("deserialized MyWrapper: input: " + json + " ; w.getMyField(): " + w.getMyField());
} catch (Exception e) {
e.printStackTrace();
}
}
輸出:
serialized MyWrapper: {}
deserialized MyWrapper: input: {"my_field":"value"} ; w.getMyField(): value
它完全忽略它然後,我需要包括它在反序列化和忽略序列化 –
解決方案似乎工作。看到編輯答案 –
謝謝!我最終做了自定義序列化器,但我會將其標記爲已接受。 –