2012-12-28 2 views
8

JSON 직렬화/비 직렬화에 Jackson (2.1.1)을 사용하고 있습니다. JAXB 주석이있는 기존 클래스가 있습니다. 이러한 주석의 대부분은 정확하며 잭슨과 함께 사용할 수 있습니다. 나는 이러한 클래스의 비 직렬화/직렬화를 약간 변경하기 위해 믹스 인을 사용하고 있습니다. 상기 내용을 토대로Jackson Jaxb 주석 우선 순위 - @XmlTransient를 무시하는 @JsonProperty

setAnnotationIntrospector(AnnotationIntrospector.pair(
       new JacksonAnnotationIntrospector(), 
       new JaxbAnnotationIntrospector(getTypeFactory()))); 

, 잭슨 주석 때문에 introspectors의 순서로, JAXB보다 우선 내 ObjectMapper 생성자에서

나는 다음을 수행합니다. 이것은 Jackson Jaxb docs을 기반으로합니다. 무시하고 싶은 필드의 경우 믹스 인의 필드에 @JsonIgnore을 추가하면 정상적으로 작동합니다. 무시하고 싶지 않은 기존 클래스에 @XmlTransient으로 표시된 두 개의 필드가 있습니다. 나는 믹스 인에서 필드에 @JsonProperty을 추가하려고 시도했지만 작동하지 않는 것 같습니다.

public interface FooMixIn { 
    @JsonIgnore String getBaz(); //ignore the baz property 
    @JsonProperty String getBar(); //override @XmlTransient with @JsonProperty 
} 

모든 아이디어를 어떻게 원래의 클래스를 수정하지 않고이 문제를 해결하려면 :

여기
public class Foo { 
    @XmlTransient public String getBar() {...} 
    public String getBaz() {...} 
} 

가 혼합 된 것입니다 : 여기

원래 클래스? 내가 믹스-에서했던 것과 같은 동작을 얻을 수가

public class Foo { 
    @JsonProperty @XmlTransient public String getBar() {...} 
    @JsonIgnore public String getBaz() {...} 
} 

:

내가 대신 믹스 인을 사용하는 회원들에게 @JsonProperty을 추가하는 시험. @XmlTransient가 제거되지 않으면 속성이 무시됩니다.

답변

7

문제는 어느 인트로 스페는 무시 마커를 검출하는 경우 AnnotationIntrospectorPair.hasIgnoreMarker() 메소드는 기본적 @JsonProperty을 무시한다는 것이다 :

public boolean hasIgnoreMarker(AnnotatedMember m) { 
     return _primary.hasIgnoreMarker(m) || _secondary.hasIgnoreMarker(m); 
    } 

REF : 대안이다 github

가 행 JaxbAnnotationIntrospector 서브 클래스

public class CustomJaxbAnnotationIntrospector extends JaxbAnnotationIntrospector { 
    public CustomJaxbAnnotationIntrospector(TypeFactory typeFactory) { 
     super(typeFactory); 
    } 

    @Override 
    public boolean hasIgnoreMarker(AnnotatedMember m) { 
     if (m.hasAnnotation(JsonProperty.class)) { 
      return false; 
     } else { 
      return super.hasIgnoreMarker(m); 
     } 
    } 
} 
이 가

그런 다음 바로 CustomJaxbAnnotationIntrospector를 사용하여 원하는 동작을 얻을 AnnotationIntrospectorPair

관련 문제