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!
}
}
이것은 제 모르는 것처럼 보일 수 있지만 표시 줄을 일시적으로 표시하는 것은 어떨까요? – DwB