Jersey 2.26:在ResourceConfig中注册@Inject bindFactory无法将Factory转换为供应商

时间:2018-02-07 04:10:52

标签: java json forms rest jersey-2.0

我现在正在使用Jersey,我希望将GeneralForm地图注入资源类上下文,该上下文接受所有application/jsonmultipart/form-dataapplication/x-www-form-urlencoded格式提交。

我按照Jersey文档中指定的说明进行操作:

https://jersey.github.io/documentation/latest/ioc.html#d0e17033

GeneralForm.java

package cn.easecloud.jrf.provider;
import java.util.HashMap;

public class GeneralForm extends HashMap<String, Object> {
}

GeneralFormFactory.java

package cn.easecloud.jrf.provider;

import org.glassfish.hk2.api.Factory;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;

public class GeneralFormFactory implements Factory<GeneralForm> {

    private final HttpServletRequest request;

    @Inject
    public GeneralFormFactory(HttpServletRequest request) {
        this.request = request;
    }

    @Override
    public GeneralForm provide() {
        GeneralForm result = new GeneralForm();
        return result;
    }

    @Override
    public void dispose(GeneralForm t) {
    }

}

然后我将该工厂注册到我的ResouceConfig:

package cn.cwhale.bowei;

import cn.easecloud.jrf.provider.AuthenticationFilter;
import cn.easecloud.jrf.provider.GeneralForm;
import cn.easecloud.jrf.provider.GeneralFormFactory;
import org.glassfish.jersey.internal.inject.AbstractBinder;
import org.glassfish.jersey.media.multipart.MultiPartFeature;
import org.glassfish.jersey.process.internal.RequestScoped;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.spring.SpringLifecycleListener;
import org.glassfish.jersey.server.spring.scope.RequestContextFilter;
import org.springframework.web.filter.CommonsRequestLoggingFilter;

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.container.ContainerRequestFilter;

@ApplicationPath("/api/*")
public class AppConfig extends ResourceConfig {

    public AppConfig() {

        packages("cn.cwhale.bowei");

        register(MultiPartFeature.class);
        register(SpringLifecycleListener.class);
        register(RequestContextFilter.class);
        register(ContainerRequestFilter.class);
        register(CommonsRequestLoggingFilter.class);
        register(AuthenticationFilter.class);

        register(new AbstractBinder() {
            @Override
            protected void configure() {
                bindFactory(GeneralFormFactory.class).to(GeneralForm.class)
                .proxy(true).proxyForSameScope(false).in(RequestScoped.class);
            }
        });

        property("encoding", "utf-8");

    }

}

但后来我失败了,因为bindFactory方法接受Supplier<T>类没有实现的Factory参数。

enter image description here

编译时:

方法 org.glassfish.jersey.internal.inject.AbstractBinder.<T>bindFactory(java.lang.Class<? extends java.util.function.Supplier<T>>,java.lang.Class<? extends java.lang.annotation.Annotation>)不适用
  (无法推断类型变量 T
    (实际参数列表和形式参数列表长度不同))
方法 org.glassfish.jersey.internal.inject.AbstractBinder.<T>bindFactory(java.lang.Class<? extends java.util.function.Supplier<T>>)不适用
  (无法推断类型变量 T
    (参数不匹配; java.lang.Class<cn.easecloud.jrf.provider.GeneralFormFactory>无法转换为java.lang.Class<? extends java.util.function.Supplier<T>>))
方法 org.glassfish.jersey.internal.inject.AbstractBinder.<T>bindFactory(java.util.function.Supplier<T>)不适用
  (无法推断类型变量 T
    (参数不匹配; java.lang.Class<cn.easecloud.jrf.provider.GeneralFormFactory>无法转换为java.util.function.Supplier<T>))

1 个答案:

答案 0 :(得分:4)

泽西岛2.26对其DI支持进行了一些修改。首先,它删除了HK2作为硬依赖,并添加了一个抽象层。新框架使用了HK2中的一些名称,但包装却不同。例如AbstractBinder。您可以在代码中看到包名称中没有hk2。这是Jersey使用的新抽象层。

新层大量使用Java 8.例如,对于bindFactory,它不再使用HK2 Factory,而是使用Java 8 Supplier。对于bindFactory,您现在可以使工厂实施Supplier

public class GeneralFormFactory implements Supplier<GeneralForm> {

    private final HttpServletRequest request;

    @Inject
    public GeneralFormFactory(HttpServletRequest request) {
        this.request = request;
    }

    @Override
    public GeneralForm get() {
        GeneralForm result = new GeneralForm();
        return result;
    }
}