Jersey 2.26:在ResourceConfig中注册@Inject bindFactory无法将Factory转换为Supplier [英] Jersey 2.26: register @Inject in ResourceConfig bindFactory cannot convert Factory to Supplier

查看:159
本文介绍了Jersey 2.26:在ResourceConfig中注册@Inject bindFactory无法将Factory转换为Supplier的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在使用Jersey,我希望将一个 GeneralForm 映射到Resource类上下文中,该上下文接受所有 application / json multipart / form-data application / x-www-form-urlencoded 格式提交。



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





编译时:

 方法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 。供应商< 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>) )


解决方案

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



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

 公共类GeneralFormFactory实现Supplier< GeneralForm> {

私有最终HttpServletRequest请求;

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

@Override
public GeneralForm get(){
GeneralForm result = new GeneralForm();
返回结果;
}
}


I'm now using Jersey and I want to Inject a GeneralForm map into the Resource class context, which accepts all application/json, multipart/form-data and application/x-www-form-urlencoded format submits.

I follow the instructions specified under the Jersey documentation:

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

}

And then I register that Factory into my 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");

    }

}

But then I failed because the bindFactory method accepts a Supplier<T> argument which the Factory class did not implemented.

And when compile:

方法 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>))

解决方案

Jersey 2.26 made some changes to its DI support. First it removed HK2 as a hard dependency and added an abstraction layer. The new framework uses some of the names from HK2 but the packaging is different. For instance the AbstractBinder. You can see in your code, that there is no hk2 in the package name. This is the new abstraction layer Jersey uses.

The new layer makes a lot of use of Java 8. For instance with the bindFactory, it no longer uses the HK2 Factory, but instead uses the Java 8 Supplier. For for bindFactory you would now make your factory implement 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;
    }
}

这篇关于Jersey 2.26:在ResourceConfig中注册@Inject bindFactory无法将Factory转换为Supplier的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆