Jersey: Print the actual request

JavaJersey

Java Problem Overview


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).

Java Solutions


Solution 1 - Java

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).

Solution 2 - Java

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.

Solution 3 - Java

@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.

Solution 4 - Java

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionHaphazardView Question on Stackoverflow
Solution 1 - Javaivan.cikicView Answer on Stackoverflow
Solution 2 - JavaMartin AnderssonView Answer on Stackoverflow
Solution 3 - JavaDaniel KaplanView Answer on Stackoverflow
Solution 4 - Javajames.lorenzenView Answer on Stackoverflow