anij anij - 3 months ago 10x
Java Question

Is there any way to log every web service call coming to an Application in Jersey

I'm using Jersey 1.8 in My Application.
Now, there are lots of service classes.
Is there any way to log all the request coming to different services.
That means, is it possible to create something like a generic filter via which all request will go, from where I can track and log all incoming requests.


Yes, it's quite easy:

Create a provider, e.g.

public final class GeneralAccessHandler implements ContainerRequestFilter, ContainerResponseFilter {

    /** The incoming request we obtained. */
    private final HttpServletRequest mHttpServletRequest;

    /** Some useful informations about the accesspoint in our service. */
    private final UriInfo mUriInfo;

    public ContainerRequest filter(final ContainerRequest request) {
        if (null != mHttpServletRequest) {
            System.out.println("Method: " + mHttpServletRequest.getMethod());
            System.out.println("Session: " + mHttpServletRequest.getSession().getId());
            System.out.println("User Agent: " + mHttpServletRequest.getHeader("user-agent"));
            final String addr = mHttpServletRequest.getRemoteAddr();
            final int port = mHttpServletRequest.getRemotePort();
            System.out.println("Client: " + addr + ":" + port);
        if (null != mUriInfo) {
            System.out.println("URI: " + mUriInfo.getRequestUri().getPath());
        return request;

    public ContainerResponse filter(final ContainerRequest pReq, final ContainerResponse pResp) {
        System.out.println("Outgoing Response")
        return pResp;


And in your web.xml you can add two init-params to your Servlet, pointing to this AccessHandler like this:


This class will now be the first and last place for each Request. You can do whatever you want here. Log it with your logger, use MDC from logback, etc pp.

Hope this helps!