简体   繁体   中英

How to inject DataBase Manager inside a web service using JAX-RS in JBoss 7.1?

My UserService looks like

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

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

and registered this service as

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;
    }
}

When I deploy this service and test it, everything is good

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

Now I have a Manager that queries that database and gets results, so I modify my Service to

@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();
}

When I deploy this change I see error as

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

I am sure I am not injecting Manager correctly, but how do I correct it? This is first time I am using JBoss.

Thank you

You're not injecting your database manager at all. You are instantiating it with the standard Java new operator, which means your bean isn't managed by the CDI container, and consequently none of the objects its relies on get initialized. This is what is causing your null pointer exception. You should be using the @Inject annotation:

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

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

On a side note, you do not need to register your user service in your application startup, assuming you have a valid beans.xml in its containing archive (JAR file).

It should be something like the following:

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

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

Also make sure you have (at least empty) WEB-INF/beans.xml

A minimal app example is available here: https://github.com/jboss-developer/jboss-eap-quickstarts/tree/6.3.0.GA/helloworld-rs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM