RESTful Web 服务:java.lang.NullPointerException service.AbstractFacade.findAll

发布于 2024-12-22 19:02:57 字数 3542 浏览 7 评论 0原文

我使用 NetBeans 7 的“来自数据库的 RESTful Web 服务...”向导创建了一个简单的 XML Web 服务。此时,我想从关联的 mySQL 数据库发布用户列表。

当我尝试通过其 URL (http://localhost:8080/database/resources/users) 访问该服务时,收到一条错误消息“java.lang.NullPointerException”。堆栈跟踪:

service.AbstractFacade.findAll(AbstractFacade.java:41)
service.UserFacade.findAll(UserFacade.java:51)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$TypeOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:165)
com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:67)
com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:276)
com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:83)
com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:133)
com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:71
com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1171)  com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1103)  com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1053)
com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1043)
com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:406)
com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:477)
com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:662)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

用户实体:

package entities;
...
@Entity
@Table(name="users")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Users.findAll", query = "SELECT u FROM Users u"), 
...

我还将命名查询更改为 User.findAll,以防名称需要与实体的名称对齐。这并没有解决问题。

我不确定它是否“正常”,但向导创建了一个相当稀疏的 UserFacade 类;在研究该主题后,我添加了缺少的方法。此外, javax.ejb.Stateless 包似乎丢失了(可能不在我工作站的 CLASSPATH 上);这就是 @Stateless 注释被禁用的原因。

UserFacade 类:

//@Stateless
@Path("users")
public class UserFacade extends AbstractFacade<User> {

    @PersistenceContext(unitName="databasePU") 
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public UserFacade() {
        super(User.class);
    } 

    @GET
    @Path("{id}")
    @Produces({"application/xml", "application/json"})
    public User find(@PathParam("id") BigDecimal id) {
        return super.find(id);
    }

    @GET
    @Override
    @Produces({"application/xml", "application/json"})
    public List<User> findAll() {
        return super.findAll();
    } 

}

AbstractFacade 的 findAll 方法的第一行抛出异常:

public List<T> findAll() {  

  javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
  ...
}

问题:

  • 该功能是否需要 @Stateless 注释?
  • 此模式是否需要 J2EE 6 而不是 J2SE 6(这是我的 OS X 工作站上安装的)? “javax.ejb”命名空间似乎暗示了企业 Java bean。

** 编辑 **

  • Java SE 6 (1.6.0_29-b11-402)

I created a simple XML web service using NetBeans 7's "RESTful Web Services from Database..." wizard. At this point, I want to publish a list of users from the associated mySQL database.

When I attempt to access the service via its URL (http://localhost:8080/database/resources/users), I get an error that reads "java.lang.NullPointerException". The stack trace:

service.AbstractFacade.findAll(AbstractFacade.java:41)
service.UserFacade.findAll(UserFacade.java:51)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$TypeOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:165)
com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:67)
com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:276)
com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:83)
com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:133)
com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:71
com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1171)  com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1103)  com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1053)
com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1043)
com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:406)
com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:477)
com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:662)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

User entity:

package entities;
...
@Entity
@Table(name="users")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Users.findAll", query = "SELECT u FROM Users u"), 
...

I've also changed the named query to User.findAll in case the names needs to align with the entity's name. This did not solve the problem.

I'm not certain if it is 'normal' or not, but the wizard created a fairly sparse UserFacade class; I added the missing methods after researching the topic. Furthermore, the javax.ejb.Stateless package seems to be missing (perhaps not on my workstation's CLASSPATH); this is the reason that the @Stateless annotation is disabled.

UserFacade class:

//@Stateless
@Path("users")
public class UserFacade extends AbstractFacade<User> {

    @PersistenceContext(unitName="databasePU") 
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public UserFacade() {
        super(User.class);
    } 

    @GET
    @Path("{id}")
    @Produces({"application/xml", "application/json"})
    public User find(@PathParam("id") BigDecimal id) {
        return super.find(id);
    }

    @GET
    @Override
    @Produces({"application/xml", "application/json"})
    public List<User> findAll() {
        return super.findAll();
    } 

}

Exception is thrown at the first line in the AbstractFacade's findAll method:

public List<T> findAll() {  

  javax.persistence.criteria.CriteriaQuery cq = getEntityManager().getCriteriaBuilder().createQuery();
  ...
}

Questions:

  • Is the @Stateless annotation required for this to function?
  • Does this pattern require J2EE 6 rather than J2SE 6 (which is what is installed on my OS X workstation)? The 'javax.ejb' namespace seems to suggest enterprise java beans.

** edit **

  • Java SE 6 (1.6.0_29-b11-402)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

智商已欠费 2024-12-29 19:02:57

自动生成的查询“SELECT u FROM Users u”运行没有任何问题。根据评论表明“u”可能是错误的,因为它不代表列,该建议不正确,因为这里“u”是表用户的别名。

我将进一步调试 findAll() 以检查某些内容是否为空,即 EntityManager。

UserFacade 中的 @Stateless 注释是必要的,删除它可能会导致 EntityManager 为空(请注意,我写了“删除”,因为 NetBeans 为您放置了 if ,如果您使用“来自数据库的 RestFul Web 服务”向导)。请参阅此处类似的问题。

关于您的最新编辑:是的,这些功能需要使用 Java 构建平台企业版。特别是,RESTFul Web 服务利用 Java API for RESTful Web Services (JAX-RS),该 API 包含在 Java EE 6 平台中,如下所述 此处

GlassFish Server 开源版是 Java EE 6 平台规范的第一个兼容实现:我建议使用此应用程序服务器并遵循上面链接的教程。

The auto-generated query "SELECT u FROM Users u" works without any problems. As per the comment suggesting that "u" might be wrong because it doesn't represent a column, that suggestion is not correct because here "u" is an alias for the table users.

I would debug further the findAll() to check if something is null, i.e. the EntityManager.

The @Stateless annotation in the UserFacade is necessary, and removing it would probably cause the EntityManager to be null (note that I wrote "removing" because NetBeans places if for you, if you use "RestFul Web Services from Database" wizard). See here a similar question.

Regarding your latest edit: yes, these features need to be built using the Java Platform, Enterprise Edition. In particular, RESTFul web services make use of the Java API for RESTful Web Services (JAX-RS) which is included in the Java EE 6 platform as explained here.

GlassFish Server Open Source Edition is the first compatible implementation of the Java EE 6 platform specification: I suggest using this Application Server and following the tutorials linked above.

山田美奈子 2024-12-29 19:02:57

我认为@ori 就是答案。您的表 Users 可能没有名为 u 的列,因此当它尝试将列 u 与数据库匹配时,您会收到异常。

更改为 u.* ,它应该可以正常工作。

I think @ori is on the the answer. Your table Users probably don't have a column named u so you get an exception when it tries to match the column u to the database.

Change to u.* and it should work fine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文