1
A가 기본 클래스 인 A -> B와 같은 클래스 계층 구조가있는 경우 기본 클래스의 필드 만 포함하고 제외 할 잭슨 직렬화 중에 어떻게 지정할 수 있습니까? 하위 클래스? 내가 직렬화하는 것은 B (서브 클래스)이다.잭슨 직렬화 중에 계층 구조에서 특정 클래스 유형 선택
감사합니다!
A가 기본 클래스 인 A -> B와 같은 클래스 계층 구조가있는 경우 기본 클래스의 필드 만 포함하고 제외 할 잭슨 직렬화 중에 어떻게 지정할 수 있습니까? 하위 클래스? 내가 직렬화하는 것은 B (서브 클래스)이다.잭슨 직렬화 중에 계층 구조에서 특정 클래스 유형 선택
감사합니다!
은 this similar question에서 설명한대로 사용할 수 있습니다.
또한 an annotation introspector 또는 Jackson JSON filter을 쓰면 하위 클래스의 모든 것을 제외 할 수 있습니다.
특수 주석이있는 계층 구조에서 모든 속성을 제외하는 방법을 보여주는 예제를 작성했습니다.
public class JacksonExcludeFromSubclass {
@Retention(RetentionPolicy.RUNTIME)
public static @interface IgnoreTypeProperties {};
public static class SuperThing {
public final String superField;
public SuperThing(String superField) {
this.superField = superField;
}
}
@JsonFilter("filter")
@IgnoreTypeProperties
public static class Thing extends SuperThing {
public final String thingField;
public Thing(String superField, String thingField) {
super(superField);
this.thingField = thingField;
}
}
public static class ExcludeFromSuperClass extends SimpleBeanPropertyFilter {
@Override
protected boolean include(BeanPropertyWriter writer) {
return true;
}
@Override
protected boolean include(PropertyWriter writer) {
if (writer instanceof BeanPropertyWriter) {
AnnotatedMember member = ((BeanPropertyWriter) writer).getMember();
return member.getDeclaringClass().getAnnotation(IgnoreTypeProperties.class) == null;
}
return true;
}
}
public static void main(String[] args) throws JsonProcessingException {
Thing t = new Thing("super", "thing");
ObjectMapper mapper = new ObjectMapper();
mapper.setFilters(new SimpleFilterProvider().addFilter("filter", new ExcludeFromSuperClass()));
System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(t));
}
출력 :
{
"superField" : "super"
}
감사합니다 많이! 대답과 예제를 정말 고맙게 생각합니다. – sobychacko