1

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?

Alex G.
  • 2,113
  • 6
  • 25
  • 33
  • When you say you need value *"from the servlet code"*, do you actually mean that you need the value from class `UserService`, which is not actually a **servlet**, but a rest controller? – Andreas Mar 27 '17 at 02:01
  • Potential duplicate of "[Servlet init() method equivalent in JAX-RS](http://stackoverflow.com/q/16809379/5221149)" or "[How to get ServeletConfig in Jersey Provider?](http://stackoverflow.com/q/22265528/5221149)", or see Jersey documentation: [JAX-RS Application, Resources and Sub-Resources - 3.6. Use of @Context](https://jersey.java.net/documentation/latest/jaxrs-resources.html#d0e2822). – Andreas Mar 27 '17 at 02:12
  • @Andreas Indeed, my problem is similar to "How to get ServeletConfig in Jersey Provider?" but why do you have to be so downvote-happy? – Alex G. Mar 27 '17 at 04:12

1 Answers1

3

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:

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720