gpt4 book ai didi

java - 如何在 JBoss 7.1 中使用 JAX-RS 将数据库管理器注入(inject)到 Web 服务中?

转载 作者:行者123 更新时间:2023-12-01 14:58:11 25 4
gpt4 key购买 nike

我的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

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

谢谢

最佳答案

您根本没有注入(inject)数据库管理器。您使用标准 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();
}
}

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

关于java - 如何在 JBoss 7.1 中使用 JAX-RS 将数据库管理器注入(inject)到 Web 服务中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14074965/

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