我现在正在使用Jersey,我想将一个
我遵循了Jersey文档中指定的说明:
GeneralForm
映射注入到资源类上下文中,它接受所有的application/json
、multipart/form-data
和application/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
类没有实现它。
当编译时:
方法 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>))