2013-02-05 2 views
8

xml 속성으로 jackson을 통해 java var (예 : int)를 직렬화하는 방법은 무엇입니까? 이것을 실현하기 위해 특정 잭슨이나 json 주석 (@XmlAttribute @ javax.xml.bind.annotation.XmlAttribute)을 찾을 수 없습니다.jackson을 사용하여 java 객체를 XML 속성으로 직렬화하는 방법은 무엇입니까?

<point x="100" y="100" z="100"/> 

을하지만 내가 가진 모든는 다음과 같습니다 :

public class Point { 

    private int x, y, z; 

    public Point(final int x, final int y, final int z) { 
     this.x = x; 
     this.y = y; 
     this.z = z; 
    } 

    @javax.xml.bind.annotation.XmlAttribute 
    public int getX() { 
     return x; 
    } 
    ... 
} 

내가 원하는 것은

<point> 
    <x>100</x> 
    <y>100</y> 
    <z>100</z> 
</point> 

속성 대신 요소를 얻을 수있는 방법이 있나요? 도움 주셔서 감사합니다!

+0

int 유형에는 문제가 없습니다. 내가 시도한 것은 무엇 이었는가, 나는 속성 대신 xml 요소를 얻었다. – Divine

답변

13

좋아요. 해결책을 찾았습니다.

당신이 잭슨 - DATAFORMAT-XML을

File file = new File("PointTest.xml"); 
XmlMapper xmlMapper = new XmlMapper(); 
xmlMapper.writeValue(file, new Point(100, 100, 100)); 

했다

@JacksonXmlProperty (isAttribute = TRUE) 누락 된 TAG

그래서 그냥 변화를 사용하는 경우 AnnotaionIntrospector를 등록 할 필요는 없었다 getter :

@JacksonXmlProperty(isAttribute=true) 
public int getX() { 
    return x; 
} 

잘 작동합니다. 그냥이에 따라 방법 :

https://github.com/FasterXML/jackson-dataformat-xml

이 @JacksonXmlProperty는 XML 네임 스페이스와 속성에 대한 로컬 이름을 지정할 수 있습니다; 속성이 XML 요소 또는 속성으로 쓰여질 지 여부도 포함됩니다.

1

JaxbAnnotationIntrospector을 등록하셨습니까?

ObjectMapper mapper = new ObjectMapper(); 
AnnotationIntrospector introspector = new JaxbAnnotationIntrospector(); 
// make deserializer use JAXB annotations (only) 
mapper.getDeserializationConfig().setAnnotationIntrospector(introspector); 
// make serializer use JAXB annotations (only) 
mapper.getSerializationConfig().setAnnotationIntrospector(introspector); 
+0

코드가 사용되지 않는 것 같지만 사용 해보겠습니다. – Divine

관련 문제