泽西岛工厂尝试两次创建@Context变量 [英] Jersey factory tries to create @Context variable twice

查看:147
本文介绍了泽西岛工厂尝试两次创建@Context变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到 Jersey 2.26 Spring Boot 2 .

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

I added a factory to inject @Context variable into methods, but it does it twice, first before the method and then again after the method:

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

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

I have a factory that creates those users:

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

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

And in jersey configuration I register this factory:

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

以前运行正常,但是在将应用程序升级到jersey 2.26和spring boot 2之后,它停止工作并引发异常:

It worked fine previously, but after I upgraded the application to jersey 2.26 and spring boot 2, it stopped working and throws exception:

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实现,该实现不允许执行某些操作).

The exception shows up because UserFactory is called twice. First it is called before the getUserEntitlements is called, to inject the User. But after the method returns, it is called again (and this time it gets a HttpRequest implementation that doesn't allow some operations).

为什么它可以被调用两次? 好像我要它同时满足请求和响应一样.

Why it could be called twice? As if I would asked it to enchance both the request and response.

推荐答案

问题是,在新泽西州,他们从hk2移到了它上面的抽象层,并使Java 8知道了代码(因为它是的实现). Java EE 8- https://jersey.github.io/release-notes/2.26. html ).

The problem is that in new Jersey they moved away from hk2 into an abstraction layer above it, and made the code Java 8 aware (because it is an implementation of Java EE 8 - https://jersey.github.io/release-notes/2.26.html).

所以要修复它,我必须:

So to fix it I had to:

  1. 导入正确的AbstractBinder-org.glassfish.jersey.internal.inject.AbstractBinder(从HK2-org.glassfish.hk2.utilities.binding.AbstractBinder导入上一个).

  1. Import the correct AbstractBinder - org.glassfish.jersey.internal.inject.AbstractBinder(NOT the previous one from HK2 - org.glassfish.hk2.utilities.binding.AbstractBinder).

更新工厂以实现Supplier<User>而不是Factory<User>.

Update the factory to implement Supplier<User> instead of Factory<User>.

更改之后,所有操作将像以前一样工作,只能调用一次factory.

After that changes everything works as previously, factory is called only once.

这篇关于泽西岛工厂尝试两次创建@Context变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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