【发布时间】:2018-08-22 15:03:20
【问题描述】:
当我尝试将json 转换为XML - 完整documentation 时,我在使用jackson 时遇到问题。
我的转换函数 -
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)
【问题讨论】:
-
这主要是因为您没有使用该库所需的依赖项。你能检查一下吗。如果您使用 maven 或 gradle 进行依赖管理,那么它应该可以解决您的问题。