Dependency injection with Jersey 2.0

后端 未结 8 1116
遇见更好的自我
遇见更好的自我 2020-11-22 01:18

Starting from scratch without any previous Jersey 1.x knowledge, I\'m having a hard time understanding how to setup dependency injection in my Jersey 2.0 project.

8条回答
  •  别那么骄傲
    2020-11-22 01:57

    Oracle recommends to add the @Path annotation to all types to be injected when combining JAX-RS with CDI: http://docs.oracle.com/javaee/7/tutorial/jaxrs-advanced004.htm Though this is far from perfect (e.g. you will get warning from Jersey on startup), I decided to take this route, which saves me from maintaining all supported types within a binder.

    Example:

    @Singleton
    @Path("singleton-configuration-service")
    public class ConfigurationService {
      .. 
    }
    
    @Path("my-path")
    class MyProvider {
      @Inject ConfigurationService _configuration;
    
      @GET
      public Object get() {..}
    }
    

提交回复
热议问题