Get ServletContext in Application

前端 未结 6 689
长情又很酷
长情又很酷 2020-12-29 07:06

Could you possibly explain how I can get the ServletContext instance in my Application\'s sub-class? Is it possible? I have tried to do it like in

6条回答
  •  鱼传尺愫
    2020-12-29 07:30

    There is interesting statement in documentation for Jersey version 1.18 for class com.sun.jersey.spi.container.servlet.ServletContainer

    The servlet or filter may be configured to have an initialization parameter "com.sun.jersey.config.property.resourceConfigClass" or "javax.ws.rs.Application" and whose value is a fully qualified name of a class that implements ResourceConfig or Application. If the concrete class has a constructor that takes a single parameter of the type Map then the class is instantiated with that constructor and an instance of Map that contains all the initialization parameters is passed as the parameter.

    If my understanding is correct the following constructor must be invoced with "an instance of Map that contains all the initialization parameters"

    public class ExampleApplication extends Application {
        public ExampleApplication(Map initParams) {
        }
        ...
    }
    

    Here is appropriate part of web.xml:

    
      Jersey Web Application
      com.sun.jersey.spi.container.servlet.ServletContainer
        
           javax.ws.rs.Application
           experiment.service.ExampleApplication
        
    
    

    But somehow it failed for me with the following message:

    SEVERE: Missing dependency for constructor public experiment.service.ExampleApplication(java.util.Map) at parameter index 0

    And for current version of Jersey (2.5.1) there are no such statement in documentstion: https://jersey.java.net/apidocs/latest/jersey/org/glassfish/jersey/servlet/ServletContainer.html

提交回复
热议问题