我尝试从xml转换为json。它可以很好地处理正常的文本数据内容。当xml标记中的数据是二进制时,我们不能将xml解组为java对象。你能帮忙分享一下我们如何将xml转换成二进制的json吗。
java代码:
public <T> String xmlToJson(Class<T> clazz, String xmlString, boolean includeRoot) {
StringWriter writer = new StringWriter();
String jsonString = "{}";
try {
XMLInputFactory xif = XMLInputFactory.newInstance();
xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false);
xif.setProperty(XMLInputFactory.SUPPORT_DTD, true);
XMLStreamReader xsr = xif.createXMLStreamReader(new StringReader(xmlString));
JAXBContext jc = JAXBContext.newInstance(clazz);
Unmarshaller unmarshaller = jc.createUnmarshaller();
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty("eclipselink.media-type", "application/json");
marshaller.setProperty("eclipselink.json.include-root", includeRoot);
marshaller.setProperty(MarshallerProperties.JSON_ATTRIBUTE_PREFIX, "@");
JAXBElement<T> addressFromXML = unmarshaller.unmarshal(xsr, clazz); //ERROR at this line for binary data
marshaller.marshal(addressFromXML, writer);
jsonString = writer.toString();
} catch (Exception e) {
e.printStackTrace();
}
return jsonString;
}
错误如下:
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Handler dispatch failed; nested exception is java.lang.NoClassDefFoundError: javax/mail/internet/MimeMultipart] with root cause
java.lang.NoClassDefFoundError: javax/mail/internet/MimeMultipart
at org.eclipse.persistence.internal.oxm.XMLBinaryDataHelper.initializeDataTypes(XMLBinaryDataHelper.java:74)
at org.eclipse.persistence.internal.oxm.XMLBinaryDataHelper.<init>(XMLBinaryDataHelper.java:54)
at org.eclipse.persistence.internal.oxm.XMLBinaryDataHelper.getXMLBinaryDataHelper(XMLBinaryDataHelper.java:60)
at org.eclipse.persistence.internal.oxm.XMLInlineBinaryHandler.endElement(XMLInlineBinaryHandler.java:126)
at org.eclipse.persistence.internal.oxm.record.deferred.EndElementEvent.processEvent(EndElementEvent.java:37)
at org.eclipse.persistence.internal.oxm.record.deferred.DeferredContentHandler.executeEvents(DeferredContentHandler.java:64)
at org.eclipse.persistence.internal.oxm.record.deferred.BinaryMappingContentHandler.executeEvents(BinaryMappingContentHandler.java:75)
at org.eclipse.persistence.internal.oxm.record.deferred.BinaryMappingContentHandler.processSimpleElement(BinaryMappingContentHandler.java:67)
at org.eclipse.persistence.internal.oxm.record.deferred.DeferredContentHandler.endElement(DeferredContentHandler.java:122)
at org.eclipse.persistence.internal.oxm.record.XMLStreamReaderReader.parseEvent(XMLStreamReaderReader.java:150)
at org.eclipse.persistence.internal.oxm.record.XMLStreamReaderReader.parse(XMLStreamReaderReader.java:100)
at org.eclipse.persistence.internal.oxm.record.XMLStreamReaderReader.parse(XMLStreamReaderReader.java:87)
at org.eclipse.persistence.internal.oxm.record.SAXUnmarshaller.unmarshal(SAXUnmarshaller.java:1016)
at org.eclipse.persistence.internal.oxm.XMLUnmarshaller.unmarshal(XMLUnmarshaller.java:657)
at org.eclipse.persistence.jaxb.JAXBUnmarshaller.unmarshal(JAXBUnmarshaller.java:460)
1条答案
按热度按时间qyyhg6bp1#
类路径上缺少java邮件api。
如果您使用的是maven,那么您应该能够通过添加布局依赖项来解决这个问题:
有关java邮件api的详细信息