迁移到 Jersey 2.25 时替换 Jersey 1.19 中的 InjectableProvider

Replace InjectableProvider in Jersey 1.19 when migrating to Jersey 2.25

我正在从 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;
  }

}

使用 HK2 Factory。辅助摘要 class 是 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);
            }
        });
    }
}

另请参阅:

  • Dependency injection with Jersey 2.0

AbstractContainerRequestValueFactory class 自 Jersey 2.26 版本以来已被删除。或者,我们可以使用 org.glassfish.hk2.api.Factory 来注入可以使用 @Context

检索的自定义实例值

依赖关系

在class路径中添加jersey-hk2依赖依赖org.glassfish.jersey.inject:jersey-hk2

工厂class

定义工厂以构造要注入的值。

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**/}
}

注册工厂class

正在使用 RequestScoped 为类型 Locale 注册 LocaleFactory(对于每个请求,将调用 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);
            }
        });
    }
}