Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get Jersey 2.2 (JAX-RS) to generate log output, including Json request bodies

Tags:

jersey

jetty

I'm running a Jersey 2.2 Servlet inside Jetty 9.0.4 in order to serve REST requests.

Mostly everything is good and requests get served, but I have never seen ANY log from Jersey classes. And I can't find any doco indicating what chickens I need to sacrifice to make that happen with Jersey 2.2

So my first question is - what do I need to do to get Jersey to generate some log.

When a request does run awry (eg because the Json request body can't be parsed) Jersey will throw a ContainerException with message like "Can not deserialize instance of java.util.ArrayList out of START_OBJECT token" etc. At that point it would be really lovely to have logged the incoming request body so I can inspect the Json. Again I can't find anything in the current doco outlining such a beast although I'm sure there is one. And in any case until I solve question 1 up above it's moot.

So my 2nd question is how do I log the incoming request body (without disrupting the request).

The Jersey Servlet config in web.xml looks like:

<servlet >
    <servlet-name>Jersey Servlet</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>javax.ws.rs.Application</param-name>
        <param-value>au.com.xandar.wirelesstiming.recorder.web.rest.JerseyApplication</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>

My JerseyApplication is:

public final class JerseyApplication extends ResourceConfig {

    public JerseyApplication() {
        super(
            //JacksonFeature.class   // Switching on Jackson
            // (My) JerseyLoggingFilter.class       // Log requests using Jersey ContainerRequestFilter 
            MyApplicationEventListener.class        // Log Requests using Jersey RequestEventListener
        );
        packages("au.com.xandar.wirelesstiming.recorder");

        // Enable LoggingFilter & output entity.
        // NB This does NOT generate any log.
        registerInstances(new LoggingFilter(Logger.getLogger(JerseyApplication.class.getName()), true));
    }
}
like image 850
William Avatar asked Aug 16 '13 08:08

William


People also ask

What is Jersey in REST API?

Jersey is Sun's production quality reference implementation for JSR 311: JAX-RS: The Java API for RESTful Web Services. Jersey implements support for the annotations defined in JSR-311, making it easy for developers to build RESTful web services with Java and the Java JVM.


1 Answers

Jersey doesn't log much by itself. For 2.x we're working on a development mode which would include tracing support (see Tracing in Jersey), default logging of incoming requests / outgoing responses (incl. entity bodies and tracing messages). This feature should be out soon.

Regarding your second question - you can register LoggingFilter which is able to log incoming requests / outgoing responses (method, headers, ..) as well as entity bodies (when configured). You can configure it via (3rd option illustrates how to turn-on entity logging):

web.xml (add it to the JAX-RS servlet definition):

<init-param>
    <param-name>jersey.config.server.provider.classnames</param-name>
    <param-value>org.glassfish.jersey.filter.LoggingFilter</param-value>
</init-param>

Application extension:

public class MyApplication extends Application {

    @Override
    public Set<Class<?>> getClasses() {
        return new HashSet<Class<?>>() {{
            // Add your resources.
            add(HelloWorldResource.class);

            // Add LoggingFilter.
            add(LoggingFilter.class);
        }};
    }
}

ResourceConfig instance (demonstrating also outputting the entity here):

public class MyApplication extends ResourceConfig {

    public MyApplication() {
        // Resources - add your package name here to enable package scanning.
        packages(...);

        // Enable LoggingFilter & output entity.     
        registerInstances(new LoggingFilter(Logger.getLogger(MyApplication.class.getName()), true));
    }
}
like image 190
Michal Gajdos Avatar answered Oct 14 '22 12:10

Michal Gajdos