Reputation: 2153
I have a RESTful service that runs as a Tomcat servlet as per the following specification in web.xml:
<servlet>
<servlet-name>User Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>demo.web.ix.users</param-value>
</init-param>
<init-param>
<param-name>jersey.config.server.provider.classnames</param-name>
<param-value>org.glassfish.jersey.filter.LoggingFilter;org.glassfish.jersey.media.multipart.MultiPartFeature</param-value>
</init-param>
<init-param>
<param-name>myCustomParam</param-name>
<param-value>some_value</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>User Service</servlet-name>
<url-pattern>/web/ix/users/*</url-pattern>
</servlet-mapping>
In the demo.web.ix.users package, there is class called UserService that implements the RESTful methods but does not extend any other class. It just features annotations (@GET, @POST, etc) which allow Jersey to pick it up.
I need to read the value of myCustomParam from the servlet code. I found out that calling servletConfig.getInitParameter("myCustomParam") would be the way to do it if the servlet extended any of the servlet classes (Generic, Http, etc).
I tried to change UserService so that it extends org.glassfish.jersey.servlet.ServletContainer but it doesn't help because the getServletConfig() method always returns null.
What am I missing?
Upvotes: 1
Views: 2653
Reputation: 208994
Just inject Configuration
. You can get the values from getProperty
@Context
private Configuration config;
...
String prop = (String) config.getProperty('customProp');
You could also inject ServletContext
and use its method getInitParameter
.
Configuration
is more portable, as it doesn't tie you to a servlet environment, for example you want to move to a grizzly container. Using Configuration
Jersey will just transfer all the init-params to the Configuration
when you are in a servlet environment. If you are not in a servlet environment then there are other ways to set up the property, aside from the init-param. See link below.
See also:
Upvotes: 3