我正在使用AbstractDataProvider实现自定义参数注释。我已经通过一些链接,但仍然存在相同的问题。 https://psamsotha.github.io/jersey/2015/11/01/jersey-method-parameter-injection.html
Jersey custom method parameter injection with inbuild injection
我收到此错误
No injection source found for a parameter of type public gandalf.models.authz.Permission
@Singleton
@Provider
public class RequestInfoProviders extends AbstractValueFactoryProvider {
private static final Logger log = LoggerFactory.getLogger(RequestInfoProviders.class);
@Inject
public RequestInfoProviders(MultivaluedParameterExtractorProvider mpep, ServiceLocator locator) {
super(mpep, locator, new Source[]{Source.UNKNOWN});
}
protected AbstractContainerRequestValueFactory<?> createValueFactory(Parameter parameter) {
Class<?> classType = parameter.getRawType();
if (classType != null && classType.equals(UserAuthContext.class)) {
return new RequestInfoProviders.RequestInfoParamValueFactory();
} else {
log.warn("class {}", classType);
log.warn("RequestContext annotation was not placed on correct object type; Injection might not work correctly!");
return null;
}
}
private static final class RequestInfoParamValueFactory extends AbstractContainerRequestValueFactory<UserAuthenticationContext> {
@Context
private ResourceContext requestContext;
private RequestInfoParamValueFactory() {
}
public UserAuthenticationContext provide() {
return UserAuthenticationContext.builder()
.build();
}
}
@Singleton
public static final class InjectionResolver extends ParamInjectionResolver<UserAuthContext> {
public InjectionResolver() {
super(RequestInfoProviders.class);
}
}
}
这样我已经注册了
environment.jersey().register(new AbstractBinder() {
protected void configure() {
this.bind(RequestInfoProviders.class).to(ValueFactoryProvider.class).in(Singleton.class);
this.bind(RequestInfoProviders.InjectionResolver.class).to(new TypeLiteral<InjectionResolver<UserAuthContext>>() {
}).in(Singleton.class);
}
});
我的资源类
@POST
@Timed
@ExceptionMetered
@Authenticate
@ApiOperation("Create a Permission")
public Permission createPermission(@Valid PermissionRequest permissionRequest,
@HeaderParam(AUTHORIZATION) String authorisation,
@Context ContainerRequestContext context,
@UserAuthContext UserAuthenticationContext authenticationContext) throws Exception {
Permission permission = Permission.builder().permission(permissionRequest.getPermission())
.active(permissionRequest.isActive())
.createdBy(authenticationContext.getSession().getUserId())
.namespace(authenticationContext.getSession().getNamespace())
.build();
return permissionService.create(permission);
}
环形类
@Retention(RetentionPolicy.RUNTIME)
@Target({TYPE, PARAMETER, METHOD, FIELD })
public @interface UserAuthContext {
}