2
如何配置ObjectMapper以僅映射用JsonProperty註釋的屬性? (並不一定是這個特別的註解,但這似乎是最明智的)如何配置Jackson ObjectMapper只顯示白名單屬性?
我在尋找像Gson的@Expose註釋和GsonBuilder()函數一樣的東西。excludeFieldsWithoutExposeAnnotation()。create()serializer Example。
class Foo {
public String secret;
@JsonProperty
public String biz;
}
class FooTest {
public static void main(String[] args) {
ObjectMapper m = new ObjectMapper();
// configure the mapper
Foo foo = new Foo();
foo.secret = "secret";
foo.biz = "bizzzz";
System.out.println(m.writeValueAsString(foo));
// I want {"biz": "bizzzz"}
m.readValue("{\"secret\": \"hack\", \"biz\": \"settable\"}", Foo.class);
// I want this to throw an exception like secret does not exist
}
}
感謝,贖金
的可能重複的[如何指定傑克遜只使用字段 - 優選全局](http://stackoverflow.com/questions/7105745/how-to-specify-jackson-to-only-use-fields - 優選-全球) – 2012-02-15 19:56:30