I'm trying to build a RESTful web service. This component is an addition to an existing Tomcat 7 web application that contains several Servlets already. I'm using Jersey 2.25.
I need to instantiate a few components (objects) that are related to the existing system. I wish to do that once, while the application loads - and than read them from each Resource.
My current classes are:
@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);
}
}
This is the class that I want to use as a singleton:
public class MyService {
String name;
public MyService() {
name = "John";
}
public String getName() {
return name;
}
}
And a resource:
@Path("Person")
public class PersonResource {
@Inject
private MyService service;
@GET
public String test() {
String result = "Hello!";
return result;
}
}
I can't figure out why am I getting this exception:
javax.servlet.ServletException: A MultiException has 3 exceptions. They are: 1. org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at SystemInjecteeImpl(requiredType=MyService,parent=PersonResource,qualifiers={},position=-1,optional=false,self=false,unqualified=null,1688478701) 2. java.lang.IllegalArgumentException: While attempting to resolve the dependencies of plc_webservice.PersonResource errors were found 3. java.lang.IllegalStateException: Unable to perform operation: resolve on 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) root cause
A MultiException has 3 exceptions. They are: 1. org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at SystemInjecteeImpl(requiredType=MyService,parent=PersonResource,qualifiers={},position=-1,optional=false,self=false,unqualified=null,1688478701) 2. java.lang.IllegalArgumentException: While attempting to resolve the dependencies of plc_webservice.PersonResource errors were found 3. java.lang.IllegalStateException: Unable to perform operation: resolve on 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) root cause
org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at 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)
Thank you.