“JAXBException:类 org.example.entity.Foo 或其任何超类在此上下文中都是已知的。”当对象位于外部 .jar 中时
我在尝试从 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。
I have a problem when trying to return an object defined in an external .jar from a Java EE 6 Web service.
The Web service looks like the following:
@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;
}
}
When I define the "Foo" class in the same .jar File as the Web service everything works fine. However, I'd like to define "Foo" in an .jar of it's own, as "Foo" is also a JPA bean, and as different components of the application (packaged as .ear) need to be able to access "Foo".
When defining "Foo" in another .jar file (that is packages in the same .ear) Glassfish returns the following error message:
javax.ws.rs.WebApplicationException: javax.xml.bind.JAXBException: class org.example.entity.Foo nor any of its super class is known to this context.
Any hints on how I can workaround this error? "Foo" is a standard JPA bean with the "@XmlRootElement" annotation.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您有带有 @Entity 对象的外部
jar
,则将relative/path/to/your/external.jar
添加到您的持久性.xml。外部 jar 中的 @MappedSuperclass 对象不需要它。
更多信息在此答案中:
在组件中共享持久性单元.ear 文件
另外,请查看 Github 上的 外部 jar 使用 @MappedSuperclass 和 @PersistenceContext 注入。
If you have external
jar
with @Entity objects then add<jar-file>relative/path/to/your/external.jar</jar-file>
to your persistence.xml.It's not required for @MappedSuperclass objects in external jars.
More information is in this answer:
Sharing a persistence unit across components in a .ear file
ALso take a look at external jar usage with @MappedSuperclass and @PersistenceContext injection on Github.