gpt4 book ai didi

JAVA-EE7/javax.ws.rs : Injection of EJB in REST-Resource

转载 作者:行者123 更新时间:2023-12-01 18:24:30 26 4
gpt4 key购买 nike

我正在尝试通过注释 @EJB 将无状态 EJB 注入(inject)到我的 JAX-RS Web 服务中。不幸的是,注入(inject)的 EJB 为 null,并且在调用时抛出 NullPointerException,请参阅类“RegistrationRest”:

@Path("/database")
@Stateless
public class RegistrationRest {
@EJB
private DbDao dbDao;

@Path("getInfo/{name}")
@GET
@Produces(MediaType.TEXT_PLAIN)
public String getInfoByName(@PathParam("name") String name {
TreeSet <String> ts = new TreeSet<String>();
JsonGenerator json = new JsonGenerator().writeStartArray();

// Lesen Info aus Datenbank
for(Persons person : dbDao.findInfoByName(name) {
ts.add(person.getName());
}
// Schreiben der gefundenen Staedte in ein JSON
for(String name : ts) {
json.write(name);
}
// Rueckgabe der Daten als JSON
return json.writeEnd().toString();
}
}

我的 Webservice 已通过 javax.ws.rs.core.Application 注册为一部分,请参阅“RestConfiguration”的代码:

package net.service.rest;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
// other imports

@ApplicationPath("/rest")
public class RestConfiguration extends Application {
@Override
public Set<Class<?>> getClasses() {
return new HashSet<Class<?>>(Arrays.asList(RegistrationRest.class));
}
}

由于注入(inject)的 EJB 实现了一个接口(interface),因此可能会阻止 EJB 被注入(inject)到 Web 服务/资源中,正如我在阅读本文时所理解的那样: http://netbeans.dzone.com/articles/how-to-combine-rest-and-ejb-31

由于名为“DbDao”的 EJB 是一个 DAO,它实现了我想保留的接口(interface):

@Stateless(name = "DbDao")
@Local
public class DbDaoImpl implements DbDao {
private final String ENTITY_MANAGER = "mouPU";

@PersistenceContext(unitName = ENTITY_MANAGER)
protected EntityManager em;

@Override
public List<Address> findInfoByName(final String name) {
@SuppressWarnings("unchecked")
List<Person> persons = em.createNamedQuery("findInfoByName").setParameter("name", name)).getResultList();
return persons;
}
}

在上面的文章中,指出不可能注入(inject)实现接口(interface)的 EJB。我无法想象事实如此,或者我想我忽略了一些东西。

这是堆栈跟踪:

16:28:14,831 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/].[net.Bplace.mou.ui.rest.RestConfiguration]] (http--0.0.0.0-80-6) Servlet.service() for servlet net.Bplace.mou.ui.rest.RestConfiguration 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 com.ocpsoft.pretty.PrettyFilter.doFilter(PrettyFilter.java:145) [prettyfaces-jsf2-3.3.3.jar:]
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.jpa.interceptor.WebNonTxEmCloserValve.invoke(WebNonTxEmCloserValve.java:50) [jboss-as-jpa-7.1.1.Final.jar:7.1.1.Final]
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(Unknown Source) [rt.jar:1.7.0_11]
Caused by: java.lang.NullPointerException
at net.Bplace.mou.ui.rest.RegistrationRest.getCityByCountryAndZipCode(RegistrationRest.java:100) [classes:]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.7.0_11]
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) [rt.jar:1.7.0_11]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) [rt.jar:1.7.0_11]
at java.lang.reflect.Method.invoke(Unknown Source) [rt.jar:1.7.0_11]
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:]
... 23 more

你能帮忙吗?

最佳答案

试试这个:

@ApplicationPath("/rest")
public class RestConfiguration extends Application {

private Set<Class<?>> resources = new HashSet<Class<?>>();

public RestConfiguration () {
resources.add(RegistrationRest.class);
}

@Override
public Set<Class<?>> getClasses() {
return resources;
}

}

确保您的 web.xml 包含以下内容:

<listener>
<listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
</listener>

最后,确保 WEB-INF 文件夹中有 beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
</beans>

关于JAVA-EE7/javax.ws.rs : Injection of EJB in REST-Resource,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26654634/

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