1

I try to convert from xml to json. It work well with normal text data content. When the data in a xml tag is a binary, we can not unmarshal the xml to java object. Can you help to share how we convert from xml to json for binary.

Java Code:

public <T> String xmlToJson(Class<T> clazz, String xmlString, boolean includeRoot) throws CustomException {
    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) {
        logger.error(e.getMessage());
        throw new CustomException(e.getMessage());
    }
    return jsonString;
}

Here is the error:

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)
4
  • But it fail for binary data content - and where is error? It would help if you don't swallow exception in try/catch block ... Commented Jan 8, 2021 at 7:01
  • Hi @rkosegi, I have just update my question which include the stack trace. This is the first time I try to parse xml with binary content to object and from object to json. Commented Jan 8, 2021 at 7:12
  • <xs:element name="content" minOccurs="0"> <xs:annotation> <xs:documentation>binary content</xs:documentation> </xs:annotation> <xs:simpleType> <xs:restriction base="xs:base64Binary"> </xs:restriction> </xs:simpleType> </xs:element> Commented Jan 8, 2021 at 7:13
  • Underscore-java library has a static method U.xmlToJson(xmlstring). Commented Feb 21, 2021 at 8:59

1 Answer 1

1
java.lang.NoClassDefFoundError: javax/mail/internet/MimeMultipart

You are missing java mail-api on your classpath.

If you're using maven, you should be able to fix that be adding floowing dependencies:

    <dependency>
        <groupId>javax.mail</groupId>
        <artifactId>javax.mail-api</artifactId>
    </dependency>
    <dependency>
        <groupId>com.sun.mail</groupId>
        <artifactId>javax.mail</artifactId>
    </dependency>

More information about Java Mail API

Sign up to request clarification or add additional context in comments.

4 Comments

Thank rkosegi. It is working well. I still not understand why javax.mail will relate to unmarshal from xml to object. Can you help to explain?
After I add library <dependency> <groupId>javax.mail</groupId> <artifactId>javax.mail-api</artifactId> <version>1.6.2</version> </dependency>
@John java mail API is transitively required by eclipselink
Thank a lot. now I understood. For text data, it not call to third party libs. For binary data, it call to third party libs (javax.mail-api) to handle parser. Thank for your explanation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.