Using @Context, @Provider and ContextResolver in JAX-RS

前端 未结 5 1322
感情败类
感情败类 2020-12-02 15:35

I\'m just getting acquainted with implementing REST web services in Java using JAX-RS and I ran into the following problem. One of my resource classes requires access to a s

5条回答
  •  时光说笑
    2020-12-02 16:24

    I don't think there's a JAX-RS specific way to do what you want. The closest would be to do:

    @Path("/something/")
    class MyResource {
        @Context
        javax.ws.rs.ext.Providers providers;
    
        @GET
        public Response get() {
            ContextResolver resolver = providers.getContextResolver(StorageEngine.class, MediaType.WILDCARD_TYPE);
            StorageEngine engine = resolver.get(StorageEngine.class);
            ...
        }
    }
    

    However, I think the @javax.ws.rs.core.Context annotation and javax.ws.rs.ext.ContextResolver is really for types related to JAX-RS and supporting JAX-RS providers.

    You may want to look for Java Context and Dependency Injection (JSR-299) implementations (which should be available in Java EE 6) or other dependency injection frameworks such as Google Guice to help you here.

提交回复
热议问题