JAX-RS Client / Jersey: HTTP Tracing

To log the HTTP traffic in a JAX-RS client (e.g. in a System Test) with Jersey, you will have to register an instance of LoggingFeature at the Client:


import org.glassfish.jersey.logging.LoggingFeature;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import org.junit.Before;
import org.junit.Test;

public class WorkshopsIT {

private Client client;
private WebTarget tut;

@Before
public void init() {
    this.client = ClientBuilder.newClient().register(logging());
    this.tut = this.client.target("http://localhost:8080/...");
}

LoggingFeature logging() {
    Logger logger = Logger.getLogger(this.getClass().getName());
    return new LoggingFeature(logger, Level.INFO, null, null);
}

@Test
public void request() {
    Response response = this.tut.request(MediaType.APPLICATION_JSON).get();
    assertThat(response.getStatus(), is(200));
    //...
}

The System Test yields:

Running com.airhacks.WorkshopsIT
Aug 09, 2019 8:55:20 AM com.airhacks.WorkshopsIT logging
Aug 09, 2019 8:55:20 AM org.glassfish.jersey.logging.LoggingInterceptor log
INFO: 1 * Sending client request on thread main
1 > GET http://localhost:8080/airhacks/resources/workshops
1 > Accept: application/json

Aug 09, 2019 8:55:20 AM org.glassfish.jersey.logging.LoggingInterceptor log
INFO: 1 * Client response received on thread main
1 < 200
1 < Connection: keep-alive
1 < Content-Length: 23
1 < Content-Type: application/json
1 < Date: Fri, 09 Aug 2019 06:55:20 GMT
{"airhacks":{"workshops":["PWAs","clouds","microservices"]}}

See you at Web, MicroProfile and Java EE Workshops at Munich Airport, Terminal 2 or Virtual Dedicated Workshops / consulting. Is Munich's airport too far? Learn from home: airhacks.io.

Comments:

Post a Comment:
  • HTML Syntax: NOT allowed
...the last 150 posts
...the last 10 comments
License