我对Jersey 2.26和Spring 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 实现)。
为什么它可以被调用两次?好像我会要求它增强请求和响应。