简体   繁体   中英

Java Jackson - XML to POJO

I have used Jackson before to convert JSON to POJO and that works fine, now I am trying to do it with XML and I have having difficulties.

I have the following code:

ObjectMapper xmlMapper = new XmlMapper();
GoodreadsResponse response = xmlMapper.readValue("<GoodreadsResponse><Request><authentication>true</authentication></Request><book><id>6465707</id></book></GoodreadsResponse>", GoodreadsResponse.class);
System.out.println(response);

GoodreadsResponse.java

@JacksonXmlRootElement(localName = "GoodreadsResponse")
public class GoodreadsResponse {
    @JacksonXmlProperty(localName = "book")
    private Book book;

    public Book getBook() { return book; }
}

Book.java

public class Book {
    @JacksonXmlProperty(localName = "id")
    private String id;

    public String getId() { return id; }
}

I get the following error:

Exception in thread "Thread-3" java.lang.NoClassDefFoundError: org/codehaus/stax2/ri/Stax2ReaderAdapter
at com.fasterxml.jackson.dataformat.xml.deser.XmlTokenStream.<init>(XmlTokenStream.java:1
at com.fasterxml.jackson.dataformat.xml.deser.XmlTokenStream.<init>(XmlTokenStream.java:108)
at com.fasterxml.jackson.dataformat.xml.deser.FromXmlParser.<init>(FromXmlParser.java:171)
at com.fasterxml.jackson.dataformat.xml.XmlFactory._createParser(XmlFactory.java:546)
at com.fasterxml.jackson.dataformat.xml.XmlFactory.createParser(XmlFactory.java:418)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2146)

Whatever I do I cannot get it working, I am not sure if my classes are correct or not.

Can someone have a look and see if they can point me in the right direction.

Thanks

我添加了strax2-api和jackson-module-jaxb-annotations库,它现在可以正常工作:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM