【问题标题】:"JAXBException: class org.example.entity.Foo nor any of its super class is known to this context." when object is in external .jar“JAXBException:此上下文已知类 org.example.entity.Foo 或其任何超类。”当对象在外部 .jar 中时
【发布时间】:2011-05-03 16:16:56
【问题描述】:

我在尝试从 Java EE 6 Web 服务返回外部 .jar 中定义的对象时遇到问题。

Web 服务如下所示:

@Stateless
@Path("book")
@Produces({"application/json", "application/xml"})
@Consumes({"application/json", "application/xml"})
public class NewWebService {

    @PersistenceContext(unitName = "EnterpriseApplication3-warPU")
    private EntityManager em;

    @GET
    public List<Foo> getBookTitle() {
        Query query = em.createNamedQuery("Foo.findAll");
        List<Foo> foo = query.getResultList();
        return foo;
    }
}

当我在与 Web 服务相同的 .jar 文件中定义“Foo”类时,一切正常。但是,我想在它自己的 .jar 中定义“Foo”,因为“Foo”也是一个 JPA bean,并且应用程序的不同组件(打包为 .ear)需要能够访问“Foo ”。

在另一个 .jar 文件(即同一个 .ear 中的包)中定义“Foo”时,Glassfish 返回以下错误消息:

javax.ws.rs.WebApplicationException: javax.xml.bind.JAXBException: class org.example.entity.Foo nor any of its super class is known to this context.

关于如何解决此错误的任何提示? “Foo”是带有“@XmlRootElement”注解的标准 JPA bean。

【问题讨论】:

标签: java web-services jakarta-ee glassfish jax-ws


【解决方案1】:

如果您有带有@Entity 对象的外部jar,则将&lt;jar-file&gt;relative/path/to/your/external.jar&lt;/jar-file&gt; 添加到您的persistence.xml。

外部 jar 中的 @MappedSuperclass 对象不需要。

更多信息在这个答案中: Sharing a persistence unit across components in a .ear file

也可以看看external jar usage with @MappedSuperclass and @PersistenceContext injection on Github

【讨论】:

    猜你喜欢
    • 2014-08-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-13
    • 2021-05-27
    相关资源
    最近更新 更多