【问题标题】:Jersey factory tries to create @Context variable twice泽西工厂尝试两次创建 @Context 变量
【发布时间】:2018-03-23 16:16:12
【问题描述】:

我对 Jersey 2.26Spring Boot 2 有疑问。

我添加了一个工厂来将@Context 变量注入方法,但它做了两次,第一次是在方法之前,然后是在方法之后:

@GET
@Path("/user-entitlements")
public Set<String> getUserEntitlements(@Context User user) {
    return service.getUserEntitlements(user);
}

我有一个创建这些用户的工厂:

public class UserFactory implements Factory<User> {

    private final User user;

    @Inject
    public UserFactory(HttpServletRequest request, RequestIdentityService requestIdentityService) {
        String userName = requestIdentityService.getCurrentUser(request);
        user = new User(userName);
    }

    @Override
    public User provide() {
        return user;
    }

    @Override
    public void dispose(User instance) {
    }
}

在球衣配置中我注册了这个工厂:

    register(
        new AbstractBinder() {
            @Override
            public void configure() {
                bindFactory(UserFactory.class)
                    .to(User.class)
                    .in(RequestScoped.class);
            }
        }
    );

之前运行良好,但在我将应用程序升级到 jersey 2.26 和 spring boot 2 后,它停止工作并抛出异常:

java.lang.IllegalStateException: Proxiable context org.glassfish.jersey.inject.hk2.RequestContext@3a94c454 findOrCreate returned a null for descriptor SystemDescriptor(
    implementation=org.glassfish.jersey.inject.hk2.SupplierFactoryBridge
    contracts={javax.servlet.http.HttpServletRequest}
    scope=org.glassfish.jersey.process.internal.RequestScoped
    qualifiers={}
    descriptorType=PROVIDE_METHOD
    descriptorVisibility=NORMAL
    metadata=
    rank=0
    loader=org.glassfish.hk2.utilities.binding.AbstractBinder$2@464366a8
    proxiable=true
    proxyForSameScope=false
    analysisName=null
    id=20
    locatorId=0
    identityHashCode=1360647282
    reified=true) and handle ServiceHandle(SystemDescriptor(
    implementation=com.rest.UserFactory
    contracts={org.glassfish.hk2.api.Factory}
    scope=org.glassfish.hk2.api.PerLookup
    qualifiers={}
    descriptorType=CLASS
    descriptorVisibility=NORMAL
    metadata=
    rank=0
    loader=org.glassfish.hk2.utilities.binding.AbstractBinder$2@33335025
    proxiable=null
    proxyForSameScope=null
    analysisName=null
    id=172
    locatorId=0
    identityHashCode=790201459
    reified=true),1915928862)

出现异常是因为 UserFactory 被调用了两次。首先在调用 getUserEntitlements 之前调用它,以注入用户。 但方法返回后,又被调用(这次它得到一个不允许某些操作的 HttpRequest 实现)。

为什么会被调用两次? 好像我会要求它增强请求和响应。

【问题讨论】:

  • “为什么会被调用两次?”。出于某种原因,它无法识别RequestScoped,并回退到PerLookup 作为范围。您可以从错误的详细信息中看到它。不确定这是如何发生的,但如果它以前有效。尚未尝试相同的升级。

标签: java spring spring-boot jersey spring-jersey


【解决方案1】:

问题在于,在新泽西州,他们从 hk2 转移到其上方的抽象层,并使代码能够识别 Java 8(因为它是 Java EE 8 - https://jersey.github.io/release-notes/2.26.html 的实现)。

所以要修复它,我必须:

  1. 导入正确的AbstractBinder - org.glassfish.jersey.internal.inject.AbstractBinderNOT 前一个来自 HK2 - org.glassfish.hk2.utilities.binding.AbstractBinder)。

  2. 更新工厂以实现 Supplier&lt;User&gt; 而不是 Factory&lt;User&gt;

更改后一切都像以前一样工作,工厂只调用一次。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-17
    • 2014-05-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多