Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jersey: Print the actual request

Tags:

java

jersey

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

like image 448
Haphazard Avatar asked Jul 28 '11 14:07

Haphazard


People also ask

What is Jersey client API?

Jersey ClientBuilder. JAX-RS Client API is a designed to allow fluent programming model. To create jersey client follow these steps – Use ClientBuilder. newClient() static method.

Is Jersey client thread safe?

Yes, the Jersey 2.1 client is thread safe and it should be thread safe even in the future Jersey version. You can create many WebTarget from one Client instance and invoke many requests on these WebTargets and even more requests on one WebTarget instance in the same time.


2 Answers

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

like image 130
ivan.cikic Avatar answered Sep 21 '22 11:09

ivan.cikic


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.

like image 23
Martin Andersson Avatar answered Sep 24 '22 11:09

Martin Andersson