98

How can I view the actual request that Jersey generates and sends to the server? I am having issues with a particular request and the fellow running the webserver asked to see the full request (with headers and the such).

Haphazard
  • 10,900
  • 6
  • 43
  • 55
  • 3
    for logging at the server side see the following post: [How to get jersey logs at server?][1] [1]: https://stackoverflow.com/questions/2332515/how-to-get-jersey-logs-at-server – eeezyy Jan 21 '14 at 15:54

5 Answers5

104

If you're just using Jersey Client API, LoggingFilter (client filter) should help you:

Client client = Client.create();
client.addFilter(new LoggingFilter(System.out));
WebResource webResource = client.resource("http://localhost:9998/");
ClientResponse response = webResource.accept(MediaType.APPLICATION_JSON)
                                         .get(ClientResponse.class);

Otherwise, you can again log both request and response on server using other LoggingFilter (container filter).

Jakub Bochenski
  • 3,113
  • 4
  • 33
  • 61
ivan.cikic
  • 1,827
  • 1
  • 14
  • 7
  • 6
    This `addFilter` method doesn't exist in Jersey 2.x. How do you use this now? – Daniel Kaplan Jan 07 '15 at 19:49
  • 2
    JAX-RS 2.x provides functionality that is equivalent to the Jersey 1.x proprietary client API. More details: https://jersey.java.net/documentation/latest/user-guide.html#mig-client-api – ivan.cikic Jan 13 '15 at 15:01
  • 1
    For people interested in customizing the log output, they can create their own LoggingFilter http://stackoverflow.com/questions/30187514/how-to-log-request-body-in-jax-rs-client – nacho4d Apr 24 '16 at 12:10
63

Since Jersey 2.23, there's a LoggingFeature you could use. The following is a bit simplified example, please note that you can register the feature on WebTarget as well.

Logger logger = Logger.getLogger(getClass().getName());

Feature feature = new LoggingFeature(logger, Level.INFO, null, null);

Client client = ClientBuilder.newBuilder()
        .register(feature)
        .build();

Response response = client.target("https://www.google.com")
        .queryParam("q", "Hello, World!")
        .request().get();

JavaDoc of LoggingFeature says that the request "and/or" the response is logged lol. On my machine, both are logged.

Martin Andersson
  • 18,072
  • 9
  • 87
  • 115
  • This works great for Jersey 2.25 but in 2.7 which I am using, the "logging" package is no longer located within org.glassfish.jersey.core:jersey-common. Do you know which package it was moved to in 2.7? – Tim Jan 26 '17 at 15:44
  • This does not print the body of the request or response. It only shows the headers – David Brossard Jan 03 '19 at 21:15
  • 3
    @DavidBrossard Use org.glassfish.jersey.logging.LoggingFeature.Verbosity.PAYLOAD_ANY as a constructor parameter to control this. – AxelW Jan 08 '19 at 20:58
52

@ivan.cikic's answer is for Jersey 1.x. Here's how you do it in Jersey 2.x:

import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.filter.LoggingFilter;
import org.json.JSONException;
import org.json.JSONObject;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Form;
import javax.ws.rs.core.MediaType;

...

        ClientConfig config = new ClientConfig();

        Client client = ClientBuilder.newClient(config);
        client.register(new LoggingFilter());

This is irrelevant but I just have to complain: The new LoggingFilter is really annoying because it forces you to use Java Util Logging. It would be better if it gave me control over the logger. Seems like a step backwards in design.

Daniel Kaplan
  • 62,768
  • 50
  • 234
  • 356
  • 3
    I know this is an old answer, but I have a question - do you know how to get the logger to print ALL of the information contained in a request? In particular, cookies. I've used the `LoggingFilter(Logger logger, boolean PrintEntity)` constructor, but even that doesn't print cookies. – bkaiser Nov 24 '15 at 15:52
  • 2
    LoggingFilter is now deprecated. You should use Martin's answer of the LoggingFeature. This also supports the Verbosity enum to print out a varying amount of detail. It should print out headers, which should include cookies. – Dan Hardiker Jan 03 '17 at 20:20
  • For some reason `LoggingFeature` doesn't print anything and `LoggingFilter` does print... ‍♂️ – Ferran Maylinch Jun 30 '20 at 21:35
3

All these answers are pretty close but they lack the setting to log the request and response body. At least with Jersey 2.30.1 this is how I accomplish logging the request and response including their respective bodies:

import javax.ws.rs.client.ClientBuilder;
import org.glassfish.jersey.logging.LoggingFeature;
import java.util.logging.Level;
import java.util.logging.Logger;

Logger logger = Logger.getLogger("LoggingFeature");
logger.setLevel(Level.ALL);
ClientBuilder.newClient()
  .target("https://www.example.com")
  .register(new LoggingFeature(
    logger,
    Level.ALL,
    LoggingFeature.Verbosity.PAYLOAD_ANY,
    8192))
  .request()
  .get();

Technically the Level.All and 8192 values could be null. I just provide them here to be concise.

james.lorenzen
  • 466
  • 1
  • 5
  • 13
0

For version 3.1.X it must be:

Logger logger = Logger.getLogger("test");
LoggingFeature loggingFeature = new LoggingFeature(logger, Level.INFO, Verbosity.PAYLOAD_ANY,null);
Client client = ClientBuilder.newClient();
client.register(loggingFeature);

Level.ALL is NOT Working

Jasper de Vries
  • 19,370
  • 6
  • 64
  • 102
boyi
  • 1
  • 1