NoClassDefFoundError:使用jackson

时间:2018-03-14 07:17:11

标签: java xml jackson

当我尝试将jackson转换为json - 完整documentation时,我在使用XML时出现问题。

我的转换功能 -

public org.w3c.dom.Element jsonToXml(JsonNode json) throws JsonProcessingException, ParserConfigurationException{
        org.w3c.dom.Element toReturn = null;
        XmlMapper xmlMapper = new XmlMapper();
        String jsonString = xmlMapper.writeValueAsString(json.toString());
        javax.xml.parsers.DocumentBuilderFactory b = javax.xml.parsers.DocumentBuilderFactory.newInstance();
        DocumentBuilder db = b.newDocumentBuilder();
        Document doc = db.newDocument(); 
        toReturn = doc.createElement(jsonString); 
        //return Element
        return toReturn;
    }

收到此错误 - java.lang.NoClassDefFoundError: org/codehaus/stax2/ri/Stax2WriterAdapter,我在课堂上没有提及Stax2WriterAdapter

完整错误日志 -

com.ibm.ws.webcontainer.servlet.ServletWrapper service SRVE0068E: Uncaught exception created in one of the service methods of the servlet GtoQuotesServlets in application MatafServiceServletsEAR. Exception created : java.lang.NoClassDefFoundError: org/codehaus/stax2/ri/Stax2WriterAdapter at com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator.<init>(ToXmlGenerator.java:156) at com.fasterxml.jackson.dataformat.xml.XmlFactory.createGenerator(XmlFactory.java:366) at com.fasterxml.jackson.dataformat.xml.XmlFactory.createGenerator(XmlFactory.java:27) at com.fasterxml.jackson.databind.ObjectMapper.writeValueAsString(ObjectMapper.java:2927) at mataf.service.servlets.GtoQuotesServlets.jsonToXml(GtoQuotesServlets.java:333) at mataf.service.servlets.GtoQuotesServlets.writeTumlugimToLog(GtoQuotesServlets.java:177) at mataf.service.servlets.GtoQuotesServlets.doPost(GtoQuotesServlets.java:143) at javax.servlet.http.HttpServlet.service(HttpServlet.java:738) at javax.servlet.http.HttpServlet.service(HttpServlet.java:831) at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1661) at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:944) at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:507) at com.ibm.ws.webcontainer.servlet.ServletWrapperImpl.handleRequest(ServletWrapperImpl.java:181) at com.ibm.ws.webcontainer.webapp.WebApp.handleRequest(WebApp.java:3954) at com.ibm.ws.webcontainer.webapp.WebGroup.handleRequest(WebGroup.java:276) at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:945) at com.ibm.ws.webcontainer.WSWebContainer.handleRequest(WSWebContainer.java:1592) at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:191) at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:453) at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewRequest(HttpInboundLink.java:515) at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.processRequest(HttpInboundLink.java:306) at com.ibm.ws.http.channel.inbound.impl.HttpICLReadCallback.complete(HttpICLReadCallback.java:84) at com.ibm.ws.ssl.channel.impl.SSLReadServiceContext$SSLReadCompletedCallback.complete(SSLReadServiceContext.java:1819) at com.ibm.ws.tcp.channel.impl.AioReadCompletionListener.futureCompleted(AioReadCompletionListener.java:175) at com.ibm.io.async.AbstractAsyncFuture.invokeCallback(AbstractAsyncFuture.java:217) at com.ibm.io.async.AsyncChannelFuture.fireCompletionActions(AsyncChannelFuture.java:161) at com.ibm.io.async.AsyncFuture.completed(AsyncFuture.java:138) at com.ibm.io.async.ResultHandler.complete(ResultHandler.java:204) at com.ibm.io.async.ResultHandler.runEventProcessingLoop(ResultHandler.java:775) at com.ibm.io.async.ResultHandler$2.run(ResultHandler.java:905) at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java:1660)

1 个答案:

答案 0 :(得分:2)

您没有引用Stax2WriterAdapter,但是ToXmlGenerator。 您需要添加包含此类的jar。 根据{{​​3}}该jar的最后一个版本将是:

<dependency>
    <groupId>org.codehaus.woodstox</groupId>
    <artifactId>stax2-api</artifactId>
    <version>4.0.0</version>
</dependency>

顺便说一句,你引用了相当古老的图书馆和教程。我建议看一些maven central