0

我的UserService样子

@Path("/login")
public class UserService {

    @GET
    public Response getCustomer() {
    return Response.ok("hello world!").build();
    }
}

并将此服务注册为

public class UserApplication extends Application {
    private Set<Object> emptySingletons = new HashSet<Object>();
    private Set<Class<?>> services = new HashSet<Class<?>>();

    public UserApplication() {
        services.add(new UserService().getClass());
    }

    public Set<Class<?>> getClasses() {
        return services;
    }

    public Set<Object> getSingletons() {
        return emptySingletons;
    }
}

当我部署此服务并对其进行测试时,一切都很好

$ curl http://localhost:8080/jboss-as-helloworld/login
hello world!

现在我有一个Manager查询该数据库并获得结果的查询,所以我将我的服务修改为

@GET
public Response getCustomer() {
    LoginManager loginManager = new LoginManager();
    Login user = loginManager.getUser("bird");
    if (user == null) {
        throw new WebApplicationException(Response.Status.NOT_FOUND);
    }
    return Response.ok("hello world!").build();
}

当我部署此更改时,我看到错误为

11:50:23,678 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/jboss-as-helloworld].[login]] (http--127.0.0.1-8080-5) Servlet.service() for servlet login threw exception: org.jboss.resteasy.spi.UnhandledException: java.lang.NullPointerException
    at org.jboss.resteasy.core.SynchronousDispatcher.handleApplicationException(SynchronousDispatcher.java:340) [resteasy-jaxrs-2.3.2.Final.jar:]
    at org.jboss.resteasy.core.SynchronousDispatcher.handleException(SynchronousDispatcher.java:214) [resteasy-jaxrs-2.3.2.Final.jar:]
    at org.jboss.resteasy.core.SynchronousDispatcher.handleInvokerException(SynchronousDispatcher.java:190) [resteasy-jaxrs-2.3.2.Final.jar:]
    at org.jboss.resteasy.core.SynchronousDispatcher.getResponse(SynchronousDispatcher.java:540) [resteasy-jaxrs-2.3.2.Final.jar:]
    at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:502) [resteasy-jaxrs-2.3.2.Final.jar:]
    at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:119) [resteasy-jaxrs-2.3.2.Final.jar:]
    at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:208) [resteasy-jaxrs-2.3.2.Final.jar:]
    at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:55) [resteasy-jaxrs-2.3.2.Final.jar:]
    at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:50) [resteasy-jaxrs-2.3.2.Final.jar:]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.13.Final.jar:]
    at org.jboss.weld.servlet.ConversationPropagationFilter.doFilter(ConversationPropagationFilter.java:62) [weld-core-1.1.5.AS71.Final.jar:2012-02-10 15:31]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [jbossweb-7.0.13.Final.jar:]
    at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [jbossweb-7.0.13.Final.jar:]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368) [jbossweb-7.0.13.Final.jar:]
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [jbossweb-7.0.13.Final.jar:]
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671) [jbossweb-7.0.13.Final.jar:]
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930) [jbossweb-7.0.13.Final.jar:]
    at java.lang.Thread.run(Thread.java:680) [classes.jar:1.6.0_29]
Caused by: java.lang.NullPointerException
    at com.mysema.query.jpa.impl.AbstractJPAQuery.createQuery(AbstractJPAQuery.java:134) [querydsl-jpa-2.9.0.jar:]
    at com.mysema.query.jpa.impl.AbstractJPAQuery.uniqueResult(AbstractJPAQuery.java:321) [querydsl-jpa-2.9.0.jar:]
    at com.mysema.query.jpa.impl.AbstractJPAQuery.uniqueResult(AbstractJPAQuery.java:308) [querydsl-jpa-2.9.0.jar:]
    at com.bb.service.LoginService.getLoginUser(LoginService.java:13) [classes:]
    at com.bb.business.LoginManager.getUser(LoginManager.java:13) [classes:]
    at com.bb.services.UserService.getCustomer(UserService.java:20) [classes:]
    at com.bb.services.UserService$Proxy$_$$_WeldClientProxy.getCustomer(UserService$Proxy$_$$_WeldClientProxy.java) [classes:]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [classes.jar:1.6.0_29]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) [classes.jar:1.6.0_29]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) [classes.jar:1.6.0_29]
    at java.lang.reflect.Method.invoke(Method.java:597) [classes.jar:1.6.0_29]
    at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:155) [resteasy-jaxrs-2.3.2.Final.jar:]
    at org.jboss.resteasy.core.ResourceMethod.invokeOnTarget(ResourceMethod.java:257) [resteasy-jaxrs-2.3.2.Final.jar:]
    at org.jboss.resteasy.core.ResourceMethod.invoke(ResourceMethod.java:222) [resteasy-jaxrs-2.3.2.Final.jar:]
    at org.jboss.resteasy.core.ResourceMethod.invoke(ResourceMethod.java:211) [resteasy-jaxrs-2.3.2.Final.jar:]
    at org.jboss.resteasy.core.SynchronousDispatcher.getResponse(SynchronousDispatcher.java:525) [resteasy-jaxrs-2.3.2.Final.jar:]
    ... 22 more

我确定我没有正确注入 Manager,但我该如何纠正呢?这是我第一次使用 JBoss。

谢谢

4

2 回答 2

1

您根本没有注入数据库管理器。您正在使用标准 Java 运算符对其进行实例化new,这意味着您的 bean 不由 CDI 容器管理,因此它所依赖的任何对象都不会被初始化。这就是导致您的空指针异常的原因。您应该使用 @Inject 注释:

@Path("/login")
public class UserService {
    @Inject
    private LoginManager loginManager;

    @GET
    public Response getCustomer() {
        Login user = loginManager.getUser("bird");
        return Response.ok(user).build();
    }
}

附带说明一下,您不需要在应用程序启动时注册您的用户服务,假设您beans.xml在其包含的存档(JAR 文件)中有一个有效的。

于 2013-01-01T18:11:59.953 回答
0

它应该类似于以下内容:

@Path("/login")
public class UserService {
    @In LoginManager loginManager;

    @GET
    public Response getCustomer() {
        ...
    }
}

还要确保你有(至少是空的)WEB-INF/beans.xml

此处提供了一个最小的应用程序示例:https ://github.com/jboss-developer/jboss-eap-quickstarts/tree/6.3.0.GA/helloworld-rs

于 2013-01-01T18:01:11.757 回答