尝试在 Jersey 2.27 中绑定工厂 class 时出现编译错误

Compilation error trying to bind factory class in Jersey 2.27

我正在使用 Jersey 2.27 构建 Java REST API,在我的测试中 class(如下所示)我收到以下编译错误:Cannot resolve method 'bindFactory(java.lang.Class<LdapServiceFactory>)' .

我不明白为什么这没有编译。 bindFactory方法有一个Class<? extends Supplier<T>>类型的参数,我的工厂class正在实现Supplier<LdapService>,如下所示。有人可以向我解释为什么这不会编译吗?

PS:这些只是完整代码的片段,以使事情更清楚。

import org.glassfish.jersey.internal.inject.AbstractBinder;

public class AppTest extends JerseyTest {
    @Override
    protected Application configure() {
        return new ResourceConfig()
                .packages(App.class.getPackage().getName())
                .register(new AbstractBinder() {
                    @Override
                    protected void configure() {
                        bindFactory(LdapServiceFactory.class).to(LdapService.class);
                    }
            });
    }
}

这是我的 LdapServiceFactory class 的样子:

public class LdapServiceFactory<T> implements Supplier<LdapService> {
    @Override
    public LdapService<T> get() {
        return createLdapService(DEFAULT_PROPERTIES_FILE);
    }
}

最后,LdapService class:

public interface LdapService<T> {
    List<T> request(String filter, String[] attributes, ResponseHandler<T> responseHandler) throws FilterException, ServerException;
}

从您的工厂中删除通用类型 class。您应该做的是将具体类型添加到 Supplier

public class LdapServiceFactory implements Supplier<LdapService<YourType>> {
    @Override
    public LdapService<YourType> get() {
        return createLdapService(DEFAULT_PROPERTIES_FILE);
    }
}

然后绑定的时候

bindFactory(LdapServiceFactory.class)
        .to(new GenericType<LdapService<YourType>>() {});

这样可以确保注入时的类型安全。

@Inject
private LdapService<YourType> service;