2012-02-15 4 views
2

JsonProperty로 주석 된 속성 만 매핑하도록 ObjectMapper를 구성하려면 어떻게해야합니까? 내가 GSON의 @Expose 주석과 GsonBuilder()처럼 작동 뭔가를 찾고 있어요화이트리스트 속성 만 표시하도록 Jackson ObjectMapper를 구성하는 방법은 무엇입니까?

(이 특정 주석 될 필요가 있지만, 가장 합리적인 것 같았다하지 않습니다). excludeFieldsWithoutExposeAnnotation을().() 직렬 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 
    } 
} 

감사합니다, 몸값

+0

중복 가능성 [필드 만 사용 잭슨을 지정하는 방법 - 바람직 전역 (http://stackoverflow.com/questions/7105745/how-to-specify-jackson-to-only-use-fields - 바람직하게 - 세계적으로) –

답변

2

duplicate question에서 나는 필드 중 하나를 사용하지 않으려는 제외.

ObjectMapper mapper = new ObjectMapper(); 
mapper.setVisibilityChecker(mapper.getSerializationConfig().getDefaultVisibilityChecker() 
      .withFieldVisibility(JsonAutoDetect.Visibility.NONE) 
      .withGetterVisibility(JsonAutoDetect.Visibility.NONE) 
      .withSetterVisibility(JsonAutoDetect.Visibility.NONE) 
      .withCreatorVisibility(JsonAutoDetect.Visibility.NONE)); 
관련 문제