Jersey 2.*. How to replace InjectableProvider and AbstractHttpContextInjectable of Jersey 1.*

前端 未结 2 789
借酒劲吻你
借酒劲吻你 2020-12-08 11:22

I would like to create a class whose objects can be injected using the @Context annotation (or better yet a custom annotation for cases where I need to pass an

2条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-08 11:29

    Providing an implementation of InjectionResolver only helps with injection, not when resolving values for the parameters of a resource method.

    At least with Jersey 2.11, you need to define a ValueFactoryProvider annotated with @Provider.

    @Provider
    public class MyValueFactoryProvider implements ValueFactoryProvider {
    
        @Inject
        private MyFactory factory;
    
        @Override
        public Factory getValueFactory(Parameter parameter) {
            if (parameter.getAnnotation(MyAnnotationParam.class) != null) {
                return factory;
            }
    
            return null;
        }
    
        @Override
        public PriorityType getPriority() {
            return Priority.NORMAL;
        }
    
    }
    

    If you also want to get the value injected in, e.g., members and constructor parameters, then InjectionResolver works well.

提交回复
热议问题