Java 用泛型绑定服务类

Java 用泛型绑定服务类,java,jersey,jersey-2.0,hk2,Java,Jersey,Jersey 2.0,Hk2,我有一个通用的接口 @FunctionalInterface public interface GenericInterface<T> { Optional<T> doSmth(long Id); } 在运行应用程序之后,假设我已经注册了ServiceBinder,就会出现这个错误 Caused by: org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object avail

我有一个通用的接口

@FunctionalInterface
public interface GenericInterface<T> {

    Optional<T> doSmth(long Id);
}
在运行应用程序之后,假设我已经注册了ServiceBinder,就会出现这个错误

Caused by: org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at SystemInjecteeImpl(requiredType=GenericInterface,parent=Class1 etc.)

如何正确绑定?

经过8个小时的搜索,我找到了解决方案

   bind(GenericInterfaceImpl.class).to(new TypeLiteral<GenericInterface<OwnClass>>() {});
   bind(GenericInterfaceImplImpl.class).to(new TypeLiteral<GenericInterface<OwnClass2>>(){});
bind(GenericInterfaceImpl.class).to(newTypeLiteral(){});
绑定(GenericInterfaceImpl.class).to(newTypeLiteral(){});
bind(GenericInterfaceImpl.class).to(GenericInterface.class);
bind(GenericInterfaceImplImpl.class).to(GenericInterface.class);
Caused by: org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at SystemInjecteeImpl(requiredType=GenericInterface,parent=Class1 etc.)
   bind(GenericInterfaceImpl.class).to(new TypeLiteral<GenericInterface<OwnClass>>() {});
   bind(GenericInterfaceImplImpl.class).to(new TypeLiteral<GenericInterface<OwnClass2>>(){});