问题描述
我一直在寻找解决问题的方法,但我无法让它发挥作用。我想将我的Weld托管服务注入ConstraintValidator,该ConstraintValidator用于验证发布到我的JAX-RS Rest-Service的User-Object。一切都部署到glassfish 4.1服务器。
I've been searching for hours to find a solution for my problem but I can't get it to work. I want to inject my Weld-managed service into a ConstraintValidator that is used to validate a User-Object that is posted to my JAX-RS Rest-Service. Everything is deployed to a glassfish 4.1 server.
我有这样的服务
@ApplicationScoped
public class UserService {
}
and I want to inject it into a ConstraintValidator like this
public class UniqueUserNameValidator implements ConstraintValidator<UniqueUserName, ApiUser> {
@Inject
private UserService service;
@Override
public void initialize(UniqueUserName constraintAnnotation) {
}
@Override
public boolean isValid(ApiUser value, ConstraintValidatorContext context) {
return service.getByUserName(value.getUserName()) == null;
}
}
REST资源看起来像这样
the REST resource looks like this
@Path("users")
@Produces(MediaType.APPLICATION_JSON)
public class UserResource {
@Inject
UserService userService;
@POST
public Response createUser(@Valid ApiUser apiUser) {
ApiRepresentation created = userService.create(apiUser);
return Response.created(createURL(created)).build();
}
}
当我发布一个json用户对象时,我得到以下内容例外:
When I Post a json user object i get the following exception:
org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at SystemInjecteeImpl(requiredType=UserService,parent=UniqueUserNameValidator,qualifiers={},position=-1,optional=false,self=false,unqualified=null,173822971)
at org.jvnet.hk2.internal.ThreeThirtyResolver.resolve(ThreeThirtyResolver.java:74)
at org.jvnet.hk2.internal.Utilities.justInject(Utilities.java:947)
at org.jvnet.hk2.internal.ServiceLocatorImpl.inject(ServiceLocatorImpl.java:902)
at org.jvnet.hk2.internal.ServiceLocatorImpl.createAndInitialize(ServiceLocatorImpl.java:977)
at org.jvnet.hk2.internal.ServiceLocatorImpl.createAndInitialize(ServiceLocatorImpl.java:968)
at org.glassfish.jersey.internal.inject.Injections.getOrCreate(Injections.java:173)
我是意识到球衣使用hk2作为DI提供程序和ConstraintValidator使用,它反过来使用ResourceContext。由于HK2对我的WELD容器托管bean一无所知,因此在创建ConstraintValidator时无法注入正确的服务。
I'm aware that jersey uses hk2 as DI provider and the ConstraintValidator is created using the InjectingConstraintValidatorFactory which in return uses the ResourceContext. Since HK2 doe know nothing about my WELD container managed beans it can not inject the proper service when creating the ConstraintValidator.
要解决此问题,我正在搜索
To fix this I am searching for
a)使用自定义ConstraintValidatorFactory提供JAX-RS(最好是纯粹的JAX-RS方式而不依赖于jersey)的方法来创建验证器。
a) A way to provide JAX-RS (preferable a pure JAX-RS way without a dependency to jersey) with a custom ConstraintValidatorFactory to create the validator.
或
b)强制球衣使用WELD作为DI提供者或告诉hk2拾取所有容器管理bean而不手动将每个bean添加到hk2的方法。
我不知道如何使用建议的桥梁。
感谢您的帮助。
干杯
推荐答案
我还遇到了Jersey 2.25.x,Weld 2.4.x和Tomcat 8.x的这个问题,并且没有找到适合<$的解决方案c $ c> @Inject 。
I also faced this issue with Jersey 2.25.x, Weld 2.4.x and Tomcat 8.x and haven't found a proper solution with @Inject
.
作为一种解决方法,我使用以下方式以编程方式查找bean实例:
As a workaround, I programmatically looked up for the bean instance using:
SomeSortOfBean bean = CDI.current().select(SomeSortOfBean.class).get();
这篇关于Jersey Inject Weld管理bean进入ConstraintValidator的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!