JAVA-EE7 / javax.ws.rs:在REST资源中注入EJB [英] JAVA-EE7/javax.ws.rs: Injection of EJB in REST-Resource

查看:143
本文介绍了JAVA-EE7 / javax.ws.rs:在REST资源中注入EJB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to inject a Stateless EJB into my JAX-RS webservice via Annotation @EJB. Unfortunately the EJB being injected is null and throws a NullPointerException when it is being called, see the class "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();
    }
}

我的Web服务注册为部分和通过javax.ws.rs.core.Application,请参阅RestConfiguration的代码:

My Webservice is registered as part and via javax.ws.rs.core.Application, see code of "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));
    }
}

因为注入的EJB实现了一个可能是事实上,这会阻止EJB被注入到webservice / resource中,正如我读到这篇文章所理解的那样:
http://netbeans.dzone.com/articles/how-to-combine-rest-and-ejb-31

As the injected EJB implements an interface that could be the fact that prevents the EJB being injected into the webservice/resource, as I understood reading this article: http://netbeans.dzone.com/articles/how-to-combine-rest-and-ejb-31

由于名为DbDao的EJB是一个实现接口的DAO,我想保留:

As the EJB with the name "DbDao" is a DAO which implements an interface I would like to keep:

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

在上面的文章中说明是不可能的注入实现接口的EJB。我无法想象是这样或者我想我忽略了什么。

In the above article it is stated that is not possible to inject an EJB which implements an interface.I cannot imagine that is so or I suppose I overlooked something.

这是堆栈跟踪:

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有这个:

Make sure your web.xml has this:

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

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

Finally, make sure you have beans.xml in your WEB-INF folder

<?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:在REST资源中注入EJB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆