最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

JAXBException: Class Not Recognized in Java 17 Spring (Non-Boot) with Apache CXF - Stack Overflow

programmeradmin2浏览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
发布评论

评论列表(0)

  1. 暂无评论