无法让Jackson XmlMapper将Pojo序列化为xml

时间:2018-10-22 23:54:45

标签: java xml jackson

我正在尝试使用Jackson XmlMapper将POJO转换为xml。我过去曾将此库用于json,但问题是它不适用于xml atm。有人可以帮我解决为什么会发生这种情况吗?我是新来的。

public class Pojo {

    public int x = 0;
}

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;

public class Serialization {

    public static void main(String[] args) throws JsonProcessingException {
        XmlMapper mapper = new XmlMapper();
        String xml = mapper.writeValueAsString(new Pojo());
        System.out.println(xml);
    }

}

我得到的错误如下:

Exception in thread "main" java.lang.NoClassDefFoundError: org/codehaus/stax2/ri/Stax2WriterAdapter
    at com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator.<init>(ToXmlGenerator.java:179)
    at com.fasterxml.jackson.dataformat.xml.XmlFactory.createGenerator(XmlFactory.java:461)
    at com.fasterxml.jackson.dataformat.xml.XmlFactory.createGenerator(XmlFactory.java:29)
    at com.fasterxml.jackson.databind.ObjectMapper.writeValueAsString(ObjectMapper.java:3219)
    at asd.Serialization.main(Serialization.java:10)
Caused by: java.lang.ClassNotFoundException: org.codehaus.stax2.ri.Stax2WriterAdapter
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    ... 5 more
Picked up _JAVA_OPTIONS: -Djava.net.preferIPv4Stack=true

0 个答案:

没有答案