0

I am using Jersey and Sring Data MongoDB to build a web service and I am facing some issues when injecting the repeository implementation in the service.

Here is my service class :

@Path("/")
public class TaskService {

    @Inject
    CustomTaskRepository taskRepository;

    @POST
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    @Path("/create-user")
    public void createUser(@FormParam("email") String email,
                        @FormParam("phone") String phone) {

        this.taskRepository.createUser(new User(email, phone));
    }

}  

Here is my Repository interface :

public interface CustomTaskRepository {

    void createUser(User user);
}

Here is my Repository implementaion :

public class TaskRepositoryImpl implements CustomTaskRepository {

    private final MongoOperations operations;

    @Autowired
    public TaskRepositoryImpl(MongoOperations operations) {

        Assert.notNull(operations, "MongoOperations must not be null!");
        this.operations = operations;
    }

    @Override
    public void createUser(User user) {
        operations.insert(user);
    }

}  

Here is the depency binder :

public class DependencyBinder extends AbstractBinder {
    @Override
    protected void configure() {
        bind(TaskRepositoryImpl.class).to(CustomTaskRepository.class);
    }
}  

And the ResouceConfig :

public class TodoApplication extends ResourceConfig{

    public TodoApplication () {
        packages("org.mypackage");
        register(new DependencyBinder());
    }
}

When I launch the service on Tomcat and send a Post request to createUser I noticed that the taskRepository in TaskService is null and got the following three exceptions :

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=CustomTaskRepository,parent=TaskService,qualifiers={},position=-1,optional=false,self=false,unqualified=null,1528456797)
    2. java.lang.IllegalArgumentException: While attempting to resolve the dependencies of org.m9i.services.TaskService errors were found
    3. java.lang.IllegalStateException: Unable to perform operation: resolve on org.m9i.services.TaskService

    org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:419)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:381)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:344)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:221)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51) 

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=CustomTaskRepository,parent=TaskService,qualifiers={},position=-1,optional=false,self=false,unqualified=null,1528456797)
2. java.lang.IllegalArgumentException: While attempting to resolve the dependencies of org.m9i.services.TaskService errors were found
3. java.lang.IllegalStateException: Unable to perform operation: resolve on org.m9i.services.TaskService

    org.jvnet.hk2.internal.Collector.throwIfErrors(Collector.java:88)
    org.jvnet.hk2.internal.ClazzCreator.resolveAllDependencies(ClazzCreator.java:252)
    org.jvnet.hk2.internal.ClazzCreator.create(ClazzCreator.java:360)
    org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:471)
    org.glassfish.jersey.process.internal.RequestScope.findOrCreate(RequestScope.java:162)
    org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2064)
    org.jvnet.hk2.internal.ServiceLocatorImpl.internalGetService(ServiceLocatorImpl.java:711)
    org.jvnet.hk2.internal.ServiceLocatorImpl.getService(ServiceLocatorImpl.java:653)
    org.glassfish.jersey.internal.inject.Injections.getOrCreate(Injections.java:169)
    org.glassfish.jersey.server.model.MethodHandler$ClassBasedMethodHandler.getInstance(MethodHandler.java:185)
    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: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:295)
    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:286)
    org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1072)
    org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:399)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:381)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:344)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:221)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
root cause

org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at SystemInjecteeImpl(requiredType=CustomTaskRepository,parent=TaskService,qualifiers={},position=-1,optional=false,self=false,unqualified=null,1528456797)
    org.jvnet.hk2.internal.ThreeThirtyResolver.resolve(ThreeThirtyResolver.java:74)
    org.jvnet.hk2.internal.ClazzCreator.resolve(ClazzCreator.java:214)
    org.jvnet.hk2.internal.ClazzCreator.resolveAllDependencies(ClazzCreator.java:237)
    org.jvnet.hk2.internal.ClazzCreator.create(ClazzCreator.java:360)
    org.jvnet.hk2.internal.SystemDescriptor.create(SystemDescriptor.java:471)
    org.glassfish.jersey.process.internal.RequestScope.findOrCreate(RequestScope.java:162)
    org.jvnet.hk2.internal.Utilities.createService(Utilities.java:2064)
    org.jvnet.hk2.internal.ServiceLocatorImpl.internalGetService(ServiceLocatorImpl.java:711)
    org.jvnet.hk2.internal.ServiceLocatorImpl.getService(ServiceLocatorImpl.java:653)
    org.glassfish.jersey.internal.inject.Injections.getOrCreate(Injections.java:169)
    org.glassfish.jersey.server.model.MethodHandler$ClassBasedMethodHandler.getInstance(MethodHandler.java:185)
    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: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:295)
    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:286)
    org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1072)
    org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:399)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:381)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:344)
    org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:221)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)  

Where did I mess up ? Is there a more elegant design ?

Mehdi
  • 1,494
  • 5
  • 33
  • 53
  • I thought Spring injection only worked in classes annotated with @ Service or @ Component ? Try to add that to your TaskService controller? – RyanWilcox Feb 15 '18 at 21:16
  • I thought Jersey is using HK2 for DI hence the @Inject annotation. Actually I am confused as I am using Spring Data MongoDB and Jersey. – Mehdi Feb 15 '18 at 21:21
  • That's why I added my thing as a comment, not an answer - give it a shot and see ;) – RyanWilcox Feb 15 '18 at 21:38
  • Yep ! I tried using only Spring DI and annotations but still not working. I think it come from the injection of MongoOperations in the TaskRepositoryImpl. I think I am missing a Spring config somewhere – Mehdi Feb 15 '18 at 21:55
  • Check this out https://stackoverflow.com/q/32355845/2587435 – Paul Samsotha Feb 15 '18 at 22:43

0 Answers0