gpt4 book ai didi

java - "JAXBException: class org.example.entity.Foo nor any of its super class is known to this context."当对象在外部 .jar 中时

转载 作者:行者123 更新时间:2023-11-29 09:24:04 27 4
gpt4 key购买 nike

尝试从 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。

最佳答案

如果你有外部 jar使用@Entity 对象然后添加 <jar-file>relative/path/to/your/external.jar</jar-file>到你的 persistence.xml。

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

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

另请查看 external jar usage with @MappedSuperclass and @PersistenceContext injection on Github .

关于java - "JAXBException: class org.example.entity.Foo nor any of its super class is known to this context."当对象在外部 .jar 中时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4073531/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com