将单例注入JAX-RS(Jersey)资源,作为Tomcat servlet

时间:2017-07-05 12:14:05

标签: rest servlets dependency-injection jax-rs jersey-2.0

我正在尝试构建RESTful Web服务。此组件是现有Tomcat 7 Web应用程序的补充,该应用程序已包含多个Servlet。我使用的是Jersey 2.25。

我需要实例化与现有系统相关的一些组件(对象)。我希望在应用程序加载时执行一次 - 而不是从每个资源中读取它们。

我目前的课程是:

@ApplicationPath("resources")
public class MyApplication extends ResourceConfig {

    public MyApplication() {
        packages("plc_webservice.PersonResource");

        register(new MyBinder());
    }
}

public class MyBinder extends AbstractBinder{

    @Override
    protected void configure() {
        bind(new MyService()).to(MyService.class);
    }
}

这是我想用作单身人士的课程:

public class MyService {
    String name;

    public MyService() {
        name = "John";
    }

    public String getName() {
        return name;
    } 
}

资源:

@Path("Person")
public class PersonResource {


    @Inject
    private MyService service;    

    @GET
    public String test() {
        String result = "Hello!";

        return result;
    }
}

我无法弄清楚为什么会出现这种异常:

  

javax.servlet.ServletException:MultiException有3个例外。   他们是:   1. org.glassfish.hk2.api.UnsatisfiedDependencyException:没有可用于注射的对象   SystemInjecteeImpl(requiredType =为MyService,父= PersonResource,限定符= {},位置= -1,可选=假,自我=假,不合格= NULL,1688478701)   2. java.lang.IllegalArgumentException:在尝试解析plc_webservice.PersonResource的依赖关系时发现错误   3. java.lang.IllegalStateException:无法执行操作:解析plc_webservice.PersonResource

     

org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:489)     org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:427)     org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:388)     org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:341)     org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:228)     org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)     org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)   根本原因

     

MultiException有3个例外。他们是:   1. org.glassfish.hk2.api.UnsatisfiedDependencyException:没有可用于注射的对象   SystemInjecteeImpl(requiredType =为MyService,父= PersonResource,限定符= {},位置= -1,可选=假,自我=假,不合格= NULL,1688478701)   2. java.lang.IllegalArgumentException:在尝试解析plc_webservice.PersonResource的依赖关系时发现错误   3. java.lang.IllegalStateException:无法执行操作:解析plc_webservice.PersonResource

     

org.jvnet.hk2.internal.Collector.throwIfErrors(Collector.java:89)     org.jvnet.hk2.internal.ClazzCreator.resolveAllDependencies(ClazzCreator.java:250)     org.jvnet.hk2.internal.ClazzCreator.create(ClazzCreator.java:358)     org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:487)     org.glassfish.jersey.process.internal.RequestScope.findOrCreate(RequestScope.java:162)     org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2022)     org.jvnet.hk2.internal.ServiceLocatorImpl.internalGetService(ServiceLocatorImpl.java:774)     org.jvnet.hk2.internal.ServiceLocatorImpl.internalGetService(ServiceLocatorImpl.java:737)     org.jvnet.hk2.internal.ServiceLocatorImpl.getService(ServiceLocatorImpl.java:707)     org.glassfish.jersey.internal.inject.Injections.getOrCreate(Injections.java:172)     org.glassfish.jersey.server.model.MethodHandler $ ClassBasedMethodHandler.getInstance(MethodHandler.java:284)     org.glassfish.jersey.server.internal.routing.PushMethodHandlerRouter.apply(PushMethodHandlerRouter.java:74)     org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:109)     org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:112)     org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:112)     org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:112)     org.glassfish.jersey.server.internal.routing.RoutingStage.apply(RoutingStage.java:92)     org.glassfish.jersey.server.internal.routing.RoutingStage.apply(RoutingStage.java:61)     org.glassfish.jersey.process.internal.Stages.process(Stages.java:197)     org.glassfish.jersey.server.ServerRuntime $ 2.run(ServerRuntime.java:318)     org.glassfish.jersey.internal.Errors $ 1.call(Errors.java:271)     org.glassfish.jersey.internal.Errors $ 1.call(Errors.java:267)     org.glassfish.jersey.internal.Errors.process(Errors.java:315)     org.glassfish.jersey.internal.Errors.process(Errors.java:297)     org.glassfish.jersey.internal.Errors.process(Errors.java:267)     org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)     org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:305)     org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1154)     org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:473)     org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:427)     org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:388)     org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:341)     org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:228)     org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)     org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)   根本原因

     

org.glassfish.hk2.api.UnsatisfiedDependencyException:没有   可用于注射的物体   SystemInjecteeImpl(requiredType = MyService,parent = PersonResource,qualifiers = {},position = -1,optional = false,self = false,unqualified = null,1688478701)org.jvnet.hk2.internal.ThreeThirtyResolver.resolve(ThreeThirtyResolver.java: 75)     org.jvnet.hk2.internal.ClazzCreator.resolve(ClazzCreator.java:212)     org.jvnet.hk2.internal.ClazzCreator.resolveAllDependencies(ClazzCreator.java:235)     org.jvnet.hk2.internal.ClazzCreator.create(ClazzCreator.java:358)     org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:487)     org.glassfish.jersey.process.internal.RequestScope.findOrCreate(RequestScope.java:162)     org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2022)     org.jvnet.hk2.internal.ServiceLocatorImpl.internalGetService(ServiceLocatorImpl.java:774)     org.jvnet.hk2.internal.ServiceLocatorImpl.internalGetService(ServiceLocatorImpl.java:737)     org.jvnet.hk2.internal.ServiceLocatorImpl.getService(ServiceLocatorImpl.java:707)     org.glassfish.jersey.internal.inject.Injections.getOrCreate(Injections.java:172)     org.glassfish.jersey.server.model.MethodHandler $ ClassBasedMethodHandler.getInstance(MethodHandler.java:284)     org.glassfish.jersey.server.internal.routing.PushMethodHandlerRouter.apply(PushMethodHandlerRouter.java:74)     org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:109)     org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:112)     org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:112)     org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:112)     org.glassfish.jersey.server.internal.routing.RoutingStage.apply(RoutingStage.java:92)     org.glassfish.jersey.server.internal.routing.RoutingStage.apply(RoutingStage.java:61)     org.glassfish.jersey.process.internal.Stages.process(Stages.java:197)     org.glassfish.jersey.server.ServerRuntime $ 2.run(ServerRuntime.java:318)     org.glassfish.jersey.internal.Errors $ 1.call(Errors.java:271)     org.glassfish.jersey.internal.Errors $ 1.call(Errors.java:267)     org.glassfish.jersey.internal.Errors.process(Errors.java:315)     org.glassfish.jersey.internal.Errors.process(Errors.java:297)     org.glassfish.jersey.internal.Errors.process(Errors.java:267)     org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317)     org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:305)     org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1154)     org.glassfish.jersey.servlet.WebComponent.serviceImpl(WebComponent.java:473)     org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:427)     org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:388)     org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:341)     org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:228)     org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)     org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)

谢谢。

2 个答案:

答案 0 :(得分:0)

我认为这是一个CDI问题。你有bean.xml定义了吗?另外,如果您希望MyService成为单身人士,我认为您希望使用@ApplicationScoped对其进行注释。

答案 1 :(得分:0)

首先注入的资源(bean)和Jersey Endpoint类(注入点)必须是CDI-Aware。必须由CDI检测。我们可以使用bean-discovery-mode =“all” - 然后CDI扫描所有类或 bean-discovery-mode =“annotated”并使用PROPER注释标记我们的类:从这里:Bean defining annotations。我更喜欢@Dependent或@RequestScoped

然后我们必须使用Jersey Extension

<dependency>
  <groupId>org.glassfish.jersey.ext.cdi</groupId>
  <artifactId>jersey-cdi1x-servlet</artifactId>
  <version>{version}</version>
  <scope>runtime</scope>
</dependency>

`

将CDI与HK2发现机制连接起来。 这是Official oracle Guideline