Jersey 2.26: register @Inject in ResourceConfig bindFactory cannot convert Factory to Supplier

后端 未结 1 772
既然无缘
既然无缘 2020-12-11 23:28

I\'m now using Jersey and I want to Inject a GeneralForm map into the Resource class context, which accepts all application/json, multipart/f

1条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-12 00:02

    Jersey 2.26 made some changes to its DI support. First it removed HK2 as a hard dependency and added an abstraction layer. The new framework uses some of the names from HK2 but the packaging is different. For instance the AbstractBinder. You can see in your code, that there is no hk2 in the package name. This is the new abstraction layer Jersey uses.

    The new layer makes a lot of use of Java 8. For instance with the bindFactory, it no longer uses the HK2 Factory, but instead uses the Java 8 Supplier. For for bindFactory you would now make your factory implement Supplier

    public class GeneralFormFactory implements Supplier {
    
        private final HttpServletRequest request;
    
        @Inject
        public GeneralFormFactory(HttpServletRequest request) {
            this.request = request;
        }
    
        @Override
        public GeneralForm get() {
            GeneralForm result = new GeneralForm();
            return result;
        }
    }
    

    0 讨论(0)
提交回复
热议问题