@EJB注入在我的resteasy Web服务中不起作用

时间:2017-08-21 11:21:41

标签: java ejb resteasy

我正在使用hibernate jpa框架和ejb实现创建一个resteasy web服务。

我创建了一个动态Web项目,我的项目结构如下:

enter image description here

WebApplication.java

public class WebApplication extends Application {

  private Set<Class<?>> classes = new HashSet<Class<?>>();
  private Set<Object> singletons = new HashSet<Object>();

  @EJB
  private PersonEndpoint personEndpoint;

  public WebApplication(){
    classes.add(PersonEndpointBean.class);
  }

  @Override
  public Set<Object> getSingletons(){
    return singletons;
  }

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

}

PersonEndpoint.java

public interface PersonEndpoint {

public Response creaatePersonEndpoint(Person person);
public Response updatePersonEndpoint(Person person);
public Response deletePersonEndpoint(int id);
public Response retrievePersonEndpoint(int id);
public Response retrieveAllPerson();

}

PersonEndpointBean.java

@Stateless
@Local(PersonEndpoint.class)
@Path("/Person")
@Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public class PersonEndpointBean implements PersonEndpoint{

@EJB
private PersonService service;

public PersonEndpointBean(PersonService service){
    this.service = service;
}

public PersonEndpointBean(){
    super();
}

@Override
@POST
@Path("/Create")
public Response creaatePersonEndpoint(Person person) {
    return service.createPerson(person);
}

@Override
@POST
@Path("/Update")
public Response updatePersonEndpoint(Person person) {
    return service.updatePerson(person);
}

@Override
@POST
@Path("/Delete")
public Response deletePersonEndpoint(int id) {
    return service.deletePerson(id);
}

@Override
@GET
@Path("/Search/{id}")
public Response retrievePersonEndpoint(@PathParam("id") int id) {
    return service.retrievePerson(id);
}

@Override
@GET
@Path("/Search")
public Response retrieveAllPerson() {
    System.out.println("==========");
    return service.retrieveAllPerson();
}

}

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

的web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://java.sun.com/xml/ns/j2ee" 
xmlns:web="http://xmlns.jcp.org/xml/ns/javaee" 
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd 
http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-
app_2_4.xsd" id="WebApp_ID" version="2.4">
<display-name>JPAandEJB</display-name>
 <context-param>
  <param-name>resteasy.async.job.service.base.path</param-name>
  <param-value>/asynch/jobs</param-value>
</context-param>
<servlet>
  <servlet-name>resteasy-servlet</servlet-name>
  <servlet-class>
        org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
            </servlet-class>
<init-param>
  <param-name>javax.ws.rs.Application</param-name>
  <param-value>com.ph.application.WebApplication</param-value>
</init-param>
</servlet>
<servlet-mapping>
  <servlet-name>resteasy-servlet</servlet-name>
  <url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>

PersonService.java

public interface PersonService {

public Response createPerson(Person person);
public Response updatePerson(Person person);
public Response deletePerson(int id);
public Response retrievePerson(int id);
public Response retrieveAllPerson();

}

PersonServiceBean.java

@Stateless
@Local(PersonService.class)
public class PersonServiceBean implements PersonService{

@EJB
private PersonDao dao;

public PersonServiceBean(PersonDao dao){
    this.dao = dao;
}

public PersonServiceBean(){
    super();
}

@Override
public Response createPerson(Person person) {
    try{
        if(person != null){
            dao.insertToTable(person);
            return Response.status(Response.Status.CREATED).build();
        } else{
            return Response.status(Response.Status.BAD_REQUEST).build();
        }
    } catch(Exception e){
        e.printStackTrace();
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
    }
}

@Override
public Response updatePerson(Person person) {
    try{
        if(person != null){
            dao.updateToTable(person);
            return Response.status(Response.Status.ACCEPTED).build();
        } else{
            return Response.status(Response.Status.BAD_REQUEST).build();
        }
    } catch(Exception e){
        e.printStackTrace();
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
    }
}

@Override
public Response deletePerson(int id) {
    try{
        Person person = dao.getRowToTable(id);
        dao.deleteToTable(person);
        return Response.status(Response.Status.ACCEPTED).build();
    } catch(Exception e){
        e.printStackTrace();
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
    }
}

@Override
public Response retrievePerson(int id) {
    try{
        Person person = dao.getRowToTable(id);
        return Response.status(Response.Status.ACCEPTED).entity(person).build();
    } catch(Exception e){
        e.printStackTrace();
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
    }
}

@Override
public Response retrieveAllPerson() {
    try{
        List<Person> list = dao.getListToTable();
        System.out.println("===========>" + list.size());
        return Response.status(Response.Status.ACCEPTED).entity(list).build();
    } catch(Exception e){
        e.printStackTrace();
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
    }
}

}

当我尝试使用邮递员调用GET方法时,我得到以下错误日志。

09:24:05,589 ERROR [io.undertow.request] (default task-2) UT005023: Exception handling request to /SERVICE/Person/Search: org.jboss.resteasy.spi.UnhandledException: java.lang.NullPointerException
at org.jboss.resteasy.core.ExceptionHandler.handleApplicationException(ExceptionHandler.java:77)
at org.jboss.resteasy.core.ExceptionHandler.handleException(ExceptionHandler.java:220)
at org.jboss.resteasy.core.SynchronousDispatcher.writeException(SynchronousDispatcher.java:175)
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:418)
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:209)
at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:221)
at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:56)
at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:51)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85)
at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131)
at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)
at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:60)
at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:77)
at io.undertow.security.handlers.NotificationReceiverHandler.handleRequest(NotificationReceiverHandler.java:50)
at io.undertow.security.handlers.AbstractSecurityContextAssociationHandler.handleRequest(AbstractSecurityContextAssociationHandler.java:43)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:292)
at io.undertow.servlet.handlers.ServletInitialHandler.access$100(ServletInitialHandler.java:81)
at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:138)
at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:135)
at io.undertow.servlet.core.ServletRequestContextThreadSetupAction$1.call(ServletRequestContextThreadSetupAction.java:48)
at io.undertow.servlet.core.ContextClassLoaderSetupAction$1.call(ContextClassLoaderSetupAction.java:43)
at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
at io.undertow.servlet.api.LegacyThreadSetupActionWrapper$1.call(LegacyThreadSetupActionWrapper.java:44)
at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:272)
at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:81)
at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:104)
at io.undertow.server.Connectors.executeRootHandler(Connectors.java:202)
at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:805)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException
at com.ph.endpoint.PersonEndpointBean.retrieveAllPerson(PersonEndpointBean.java:69)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:139)
at org.jboss.resteasy.core.ResourceMethodInvoker.invokeOnTarget(ResourceMethodInvoker.java:295)
at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:249)
at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:236)
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:402)
... 43 more

我搜索了很多论坛,但似乎没有工作.. TIA

0 个答案:

没有答案