Question

I am using Jersey 2.3.1, Tomcat 7.0, maven.

I have a simple jersey servlet:

@Path("/myresource")
public class JerseyResource {

  @GET
  @Produces(MediaType.APPLICATION_JSON)
  public List <SecurityControlDTO> getControls() throws HibernateException {
    SecurityControlDTOManager manager = new SecurityControlDTOManager();
    return manager.getControls();
  }
}

And want to add a LoggingFilter.

This is my web.xml:

<!-- Jersey Mapping -->
    <servlet>
    <servlet-name>jersey-servlet</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>com.rbs.wisexec.controlservice.rest;</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

    <servlet-mapping>
        <servlet-name>jersey-servlet</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>

    <filter>
        <filter-name>jersey-auth</filter-name>
        <filter-class>org.glassfish.jersey.servlet.ServletContainer</filter-class>
        <init-param>
            <param-name>javax.ws.rs.container.ContainerRequestFilter</param-name>
            <param-value>org.glassfish.jersey.filter.LoggingFilter</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>jersey-auth</filter-name>
        <url-pattern>/rest/*</url-pattern>
        <servlet-name>jersey-servlet</servlet-name>
    </filter-mapping>

When I run the tomcat server, the server starts up but then when I try to access the URL I get a "HTTP Status 404 - not found" error. I have debug breakpoints in filter class so I can see that it never gets invoked.

I have tried various different ways of configuring the loggingFilter in the web.xml i.e. putting the:

<init-param>
                <param-name>javax.ws.rs.container.ContainerRequestFilter</param-name>
                <param-value>org.glassfish.jersey.filter.LoggingFilter</param-value>
            </init-param>

Inside of the servlet tag - this runs the server and the resource is shown but again the filters are not invoked.

Was it helpful?

Solution

If you want to configure your application via web.xml and not by using subclass of javax.ws.rs.core.Application, you can modify your web descriptor in this way:

<!-- Jersey Mapping -->
<servlet>
    <servlet-name>jersey-servlet</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>com.rbs.wisexec.controlservice.rest;</param-value>
    </init-param>
    <init-param>
        <param-name>jersey.config.server.provider.classnames</param-name>
        <param-value>org.glassfish.jersey.filter.LoggingFilter</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>jersey-servlet</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

The important thing is the second init-param which states that LoggingFilter should be added to the list of JAX-RS providers in your application. Parameter is taken from ServerProperties class where you can find description of this parameter (ServerProperties.PROVIDER_CLASSNAMES) as well as other possibilities to configure your Jersey application.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top