0

I have a Java Spring (non-Spring Boot) project that we recently upgraded from Java 8 to Java 17. The project uses Apache CXF for web services.

In this project, we rely on some model classes from a parent codebase, which is first built into a JAR and then included as a dependency in the current project.

However, when calling a REST endpoint that uses one of these model classes, I get the following error:

JAXBException: com.example.parent.webmodel.model this class nor any of its superclasses are recognized in the current context.

Debugging Steps Taken:

Checked if the class is available: It is present in the WEB-INF/lib folder inside the built WAR file, so the dependency is correctly included.

Verified the POM dependency reference: It is correctly declared and available.

Tried adding a ContextResolver: Registered a custom ContextResolver with the model class, but it did not help.

Moved the model class from the parent to the child project: When the model is directly in the child project, JAXB works fine, and the REST controller returns a response.

Question:

Why is JAXB unable to recognize the model class from the parent JAR when using Apache CXF?

Are there any additional configurations or dependencies needed for JAXB in Java 17?

Would appreciate any insights or solutions. Thanks!

Edit:

Minimal Reproducible Example: Model Class (CarCharge) from parent:

import java.math.BigDecimal;
import jakarta.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class CarCharge {
    private String description;
    private BigDecimal amount;

    public CarCharge() {}

    public CarCharge(String description, BigDecimal amount) {
        this.description = description;
        this.amount = amount;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public BigDecimal getAmount() {
        return amount;
    }

    public void setAmount(BigDecimal amount) {
        this.amount = amount;
    }
}

REST Controller Method (From Child):

import com.parent.webmodel.CarCharge;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;

@Path("/inv")
public class InventoryService {

    @GET
    @Path("/getcarcharges")
    @Produces({MediaType.APPLICATION_XML })
    public synchronized Response getCarCharges() {
        CarCharge charge = new CarCharge("Test Charge", new BigDecimal("100.00"));
        return Response.ok(charge).build();
    }
}

Additional Information:

    Java Version: 17
    Apache CXF Version: 4.1.0
    JAXB Dependencies:
    jakarta.xml.bind-api (4.0.2)
    jaxb-runtime (4.0.5)
    Application Server: Apache Tomcat 10
    Packaging: WAR
2
  • Version of Apache CXF? What Jakarta EE compliant app server do you use? Can you post a minimal reproducible example? Commented Apr 2 at 16:27
  • I have made edits to include examples and additional information @BasilBourque Commented Apr 3 at 8:42

0

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.