我从Jersey 1.19迁移到Jersey 2.25。我找不到足够的文档来替换InjectableProvider
有人可以帮助我。
import java.lang.reflect.Type;
import java.util.List;
import java.util.Locale;
import javax.ws.rs.core.Context;
import javax.ws.rs.ext.Provider;
import org.glassfish.hk2.api.InjectionResolver;
import org.springframework.stereotype.Component;
import com.sun.jersey.api.core.HttpContext;
import com.sun.jersey.core.spi.component.ComponentContext;
import com.sun.jersey.core.spi.component.ComponentScope;
import com.sun.jersey.server.impl.inject.AbstractHttpContextInjectable;
import com.sun.jersey.spi.inject.Injectable;
import com.sun.jersey.spi.inject.InjectableProvider;
@Provider
@Component
public class LocaleProvider extends AbstractHttpContextInjectable<Locale>
implements InjectionResolver<Context, Type> {
private final Locale swedish = new Locale("sv", "", "");
@Override
public ComponentScope getScope() {
return ComponentScope.PerRequest;
}
@Override
public Injectable<Locale> getInjectable(ComponentContext ic, Context a, Type c) {
if (c.equals(Locale.class)) {
return this;
}
return null;
}
@Override
public Locale getValue(HttpContext c) {
final List<Locale> locales = c.getRequest().getAcceptableLanguages();
if (locales.isEmpty()) {
return Locale.US;
}
for (Locale locale : locales) {
if (locale.getLanguage().equals(swedish.getLanguage())) {
return swedish;
} else if (locale.getLanguage().equals(Locale.US.getLanguage())) {
return Locale.US;
}
}
// return english if no other supported language is found
return Locale.US;
}
}
答案 0 :(得分:1)
使用HK2 Factory
。辅助抽象类为AbstractContainerRequestValueFactory
,可让您轻松访问ContainerRequest
。您可以从
public class LocaleFactory
extends AbstractContainerRequestValueFactory<Local> {
@Override
public Locale provide() {
ContainerRequest cr = getContainerRequest();
}
}
然后你需要注册它。如果您使用的是ResourceConfig
,则可以
public AppConfig extends ResourceConfig {
public AppConfig() {
register(new AbstractBinder() {
@Override
public void configure() {
bindFactory(LocalFactory.class)
.to(Locale.class).in(RequestScoped.class);
}
});
}
}
另见:
答案 1 :(得分:0)
AbstractContainerRequestValueFactory
类已被删除,自 Jersey 2.26 版本以来。或者,我们可以使用 org.glassfish.hk2.api.Factory
注入可以使用 @Context
依赖
在类路径中添加 jersey-hk2 依赖项 org.glassfish.jersey.inject:jersey-hk2
工厂班
定义工厂以构造要注入的值。
import org.glassfish.hk2.api.Factory;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.core.Context;
import javax.ws.rs.ext.Provider;
import java.util.List;
import java.util.Locale;
@Provider
public class LocaleFactory implements Factory<Locale> {
private final Locale swedish = new Locale("sv", "", "");
@Context
private ContainerRequestContext ctx;
@Override
public Locale provide() {
final List<Locale> locales = ctx.getAcceptableLanguages();
if (locales.isEmpty()) {
return Locale.US;
}
for (Locale locale : locales) {
if (locale.getLanguage().equals(swedish.getLanguage())) {
return swedish;
} else if (locale.getLanguage().equals(Locale.US.getLanguage())) {
return Locale.US;
}
}
// return english if no other supported language is found
return Locale.US;
}
@Override
public void dispose(Locale instance) {/**Noop**/}
}
注册工厂类
使用 LocaleFactory
为类型 Locale
注册 RequestScoped
(对于每个请求,LocaleFactory#provide
方法都会被调用)
import org.glassfish.hk2.utilities.binding.AbstractBinder;
import org.glassfish.jersey.process.internal.RequestScoped;
import org.glassfish.jersey.server.ResourceConfig;
import javax.ws.rs.ext.Provider;
import java.util.Locale;
@Provider
class ApplicationConfig extends ResourceConfig {
public ApplicationConfig() {
register(new AbstractBinder() {
@Override
protected void configure() {
bindFactory(LocaleFactory.class)
.to(Locale.class)
.in(RequestScoped.class);
}
});
}
}