2012-02-09 3 views
3

나는 (이 예제에서) Foo 유형의 하위 클래스 인 객체를 포함하는 객체 그래프를 가지고 있습니다. Foo 클래스에는 bar라는 속성이있어서 객체 그래프로 직렬화하고 싶지 않습니다. 그래서 기본적으로 나는 Foo 타입의 객체를 직렬화 할 때마다 바를 제외한 모든 것을 출력한다고 말할 방법이 필요합니다.Jackson JSON 직렬화의 속성을 전역 적으로 제거하는 방법은 무엇입니까?

class Foo { // this is an external dependency 
    public long getBar() { return null; } 
} 

class Fuzz extends Foo { 
    public long getBiz() { return null; } 
} 

public static void main(String[] args) { 
    ObjectMapper mapper = new ObjectMapper(); 
    // I want to set a configuration on the mapper to 
    // exclude bar from all things that are type Foo 

    Fuzz fuzz = new Fuzz(); 
    System.out.println(mapper.writeValueAsString(fuzz)); 
    // writes {"bar": null, "biz": null} what I want is {"biz": null} 
} 

감사합니다, 몸값

편집 : 내가 사용 끝날 것 코드를 포함 StaxMan 제안, 사용 (바 예를 위하여위한 게터 제작) @JsonIgnore에서

interface Mixin { 
    @JsonIgnore long getBar(); 
} 

class Example { 
    public static void main() { 
     ObjectMapper mapper = new ObjectMapper(); 
     mapper.getSerializationConfig().addMixInAnnotations(Foo.class, Mixin.class); 
     Fuzz fuzz = new Fuzz(); 
     System.out.println(mapper.writeValueAsString(fuzz)); 
     // writes {"biz": null} whoo! 
    } 
} 
+0

이것은 제 모르는 것처럼 보일 수 있지만 표시 줄을 일시적으로 표시하는 것은 어떨까요? – DwB

답변

3

을 제외하고 또는 @JsonIgnoreProperties (특히 Mix-in Annotations을 통해), '@JsonIgnoreType'으로 전역 적으로 무시할 특정 유형을 정의 할 수도 있습니다. 타사 유형의 경우에도이 기능을 혼합 주석으로 적용 할 수 있습니다.

관련 문제