Spring数据MongoDB存储库未被注入Jersey服务

时间:2018-02-15 20:35:57

标签: java rest dependency-injection jersey spring-data-mongodb

我正在使用Jersey和Sring Data MongoDB来构建Web服务,并且在服务中注入重复执行时我遇到了一些问题。

这是我的服务类:

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

}  

这是我的Repository界面:

public interface CustomTaskRepository {

    void createUser(User user);
}

这是我的存储库实现:

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

}  

这是依赖性活页夹:

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

ResouceConfig:

public class TodoApplication extends ResourceConfig{

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

当我在Tomcat上启动服务并向createUser发送Post请求时,我注意到taskRepository中的TaskService为空并且有以下三个例外:

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)  

我在哪里陷入困境?有更优雅的设计吗?

0 个答案:

没有答案