Home > front end >  How to convert XML to String with JAXB and spring-boot?
How to convert XML to String with JAXB and spring-boot?

Time:12-28

When I run a mvn spring-boot:run on the folder that has the pom.xml file the application starts and serializes a POJO into a XML correctly, but when I do it by going to the target folder and starting it by using java -jar in the jar file I get javax.xml.bind.JAXBException: Implementation of JAXB-API has not been found on module path or classpath caused by .java.lang.ClassNotFoundException: com.sun.xml.internal.bind.v2.ContextFactory.

In my maven I have the following JAXB dependencies:

<!-- JAXB API -->
<dependency>
    <groupId>jakarta.xml.bind</groupId>
    <artifactId>jakarta.xml.bind-api</artifactId>
</dependency>

<!-- JAXB Runtime -->
<dependency>
    <groupId>com.sun.xml.bind</groupId>
    <artifactId>jaxb-impl</artifactId>
    <version>2.3.5</version>
    <scope>runtime</scope>
</dependency>

Here's the code that serializes a POJO into XML:

private static final Pattern REMOVE_HEADER = Pattern.compile("\\<\\?xml(. ?)\\?\\>");

public static <T> String toXML(final T data) {
    try {
        final var jaxbMarshaller = JAXBContext.newInstance(data.getClass()).createMarshaller();
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        final var sw = new StringWriter();
        jaxbMarshaller.marshal(data, sw);
        return XMLUtils.REMOVE_HEADER.matcher(sw.toString()).replaceAll("").strip();
    } catch (final JAXBException e) {
        XMLUtils.LOGGER.error("Error while converting POJO to XML. ERROR: {}.", e.getMessage(), e);
    }

    return "";
}

Here's the log when I start the application with java -jar:

2021-12-26 21:19:14,526 [ForkJoinPool.commonPool-worker-11] ERROR com.enterprise.system.shared.util.XMLUtils - Error while converting POJO to XML. ERROR: Implementation of JAXB-API has not been found on module path or classpath..
javax.xml.bind.JAXBException: Implementation of JAXB-API has not been found on module path or classpath.
 - with linked exception:
[java.lang.ClassNotFoundException: com.sun.xml.bind.v2.ContextFactory]
        at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:232)
        at javax.xml.bind.ContextFinder.find(ContextFinder.java:375)
        at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:691)
        at javax.xml.bind.JAXBContext.newInstance(JAXBContext.java:632)
        at com.enterprise.system.shared.util.XMLUtils.toXML(XMLUtils.java:26)
        at java.base/java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:195)
        at java.base/java.util.AbstractList$RandomAccessSpliterator.forEachRemaining(AbstractList.java:720)
        at java.base/java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:484)
        at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:474)
        at java.base/java.util.stream.ReduceOps$ReduceTask.doLeaf(ReduceOps.java:952)
        at java.base/java.util.stream.ReduceOps$ReduceTask.doLeaf(ReduceOps.java:926)
        at java.base/java.util.stream.AbstractTask.compute(AbstractTask.java:327)
        at java.base/java.util.concurrent.CountedCompleter.exec(CountedCompleter.java:746)
        at java.base/java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:290)
        at java.base/java.util.concurrent.ForkJoinPool$WorkQueue.topLevelExec(ForkJoinPool.java:1020)
        at java.base/java.util.concurrent.ForkJoinPool.scan(ForkJoinPool.java:1656)
        at java.base/java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1594)
        at java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:183)
Caused by: java.lang.ClassNotFoundException: com.sun.xml.bind.v2.ContextFactory
        at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581)
        at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
        at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
        at javax.xml.bind.ServiceLoaderUtil.nullSafeLoadClass(ServiceLoaderUtil.java:92)
        at javax.xml.bind.ServiceLoaderUtil.safeLoadClass(ServiceLoaderUtil.java:125)
        at javax.xml.bind.ContextFinder.newInstance(ContextFinder.java:230)
        ... 17 more

Here's an image of the generated spring-boot fat jar with the JAXB dependencies: JAXB dependencies

Here's my dependency tree:

--- maven-dependency-plugin:3.2.0:tree (default-cli) @ java-eleven-jaxb-hell ---
com.enterprise.system:java-eleven-jaxb-hell:jar:0.0.1-SNAPSHOT
 - org.slf4j:slf4j-log4j12:jar:1.7.32:compile (optional)
|   - org.slf4j:slf4j-api:jar:1.7.32:compile (optional)
|  \- log4j:log4j:jar:1.2.17:compile (optional)
 - org.springframework.boot:spring-boot-autoconfigure:jar:2.6.2:compile
|  \- org.springframework.boot:spring-boot:jar:2.6.2:compile
|      - org.springframework:spring-core:jar:5.3.14:compile
|     |  \- org.springframework:spring-jcl:jar:5.3.14:compile
|     \- org.springframework:spring-context:jar:5.3.14:compile
|         - org.springframework:spring-aop:jar:5.3.14:compile
|         - org.springframework:spring-beans:jar:5.3.14:compile
|        \- org.springframework:spring-expression:jar:5.3.14:compile
 - org.projectlombok:lombok:jar:1.18.22:provided
 - jakarta.xml.bind:jakarta.xml.bind-api:jar:2.3.3:compile
|  \- jakarta.activation:jakarta.activation-api:jar:1.2.2:compile
\- com.sun.xml.bind:jaxb-impl:jar:2.3.5:runtime
   \- com.sun.activation:jakarta.activation:jar:1.2.2:runtime

And finally, here are my model classes:

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@XmlRootElement(name = "finans")
@XmlAccessorType(XmlAccessType.FIELD)
@Data
@NoArgsConstructor
@AllArgsConstructor
public class FinanTokenDTO {

    @XmlAttribute
    private String pln;

    @XmlAttribute
    private String ope;

    @XmlAttribute
    private String mod;

    @XmlAttribute
    private String mis;

    @XmlAttribute
    private String val;

    @XmlAttribute
    private String car;

    @XmlAttribute
    private String dti;

    @XmlAttribute
    private String dtf;

    @XmlAttribute
    private String ota;

    @XmlElement
    private FinanDTO finan;

}

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@XmlRootElement(name = "finan")
@XmlAccessorType(XmlAccessType.FIELD)
@Data
@NoArgsConstructor
@AllArgsConstructor
public class FinanDTO {

    @XmlAttribute
    private String prd;

    @XmlAttribute
    private String pkg;

    @XmlAttribute
    private String val;

    @XmlAttribute
    private String fty;

}

I am using java 11 and I am aware that in java 11 JAXB was removed from the SE JDK because it is considered as a EE feature.

I am not able to execute it using mvn spring-boot:run in production environment because of the size of the docker image and security related issues using docker container.

Since Spring Boot generates a fat jar, shouldn't the application run with java -jar applied to the spring boot fat jar generated file the same way as it does with mvn spring-boot:run inside the folder with pom.xml?

EDIT:

After a lot of digging and testing I found that the problem occurs when we try to marshall several valid POJOs using parallel stream instead of stream on the POJOs list. I uploaded the code in java-eleven-jaxb-hell to better understanding, unfortunatelly I cannot change parallelStream to stream because of performance issues. Just to remember, for the problem to happen you have to run java -jar against spring-boot generated fat jar in target folder.

CodePudding user response:

Try adding the next dependency:

<dependency>
    <groupId>jakarta.xml.bind</groupId>
    <artifactId>jakarta.xml.bind-api</artifactId>
    <version>2.3.1</version>
</dependency>

CodePudding user response:

I suggest you to replace the JAXB-impl from Sun by the one from Glassfish:

<dependency>
    <groupId>org.glassfish.jaxb</groupId>
    <artifactId>jaxb-runtime</artifactId>
    <version>2.3.5</version>
</dependency>

Generally speaking, Sun does not exists anymore since years, and the package might be there only for compatibility. The recommended JAXB implementation today comes from Glassfish.

  • Related