尝试绑定泽西岛2.27中的工厂类的编译错误

时间:2018-07-06 10:17:12

标签: java generics compiler-errors jersey

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

我不明白为什么不编译。 bindFactory方法具有一个类型为Class<? extends Supplier<T>>的参数,我的工厂类正在实现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类的样子:

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

最后是LdapService类:

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

1 个答案:

答案 0 :(得分:0)

从工厂类中删除泛型类型。您应该要做的是将具体类型添加到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;