3

This took me quite a long time to figure out. I'm asking this question so I can answer it for others:

How do you get useful logging info from the CXF Rest Client? EG: The url, params, payload, response, etc.

Note: This question already exists but it's asking about CXF and Resteasy. I only want the answer for CXF: Logging in CXF and RestEasy clients

Community
  • 1
  • 1
Daniel Kaplan
  • 62,768
  • 50
  • 234
  • 356

3 Answers3

11

Here's how you do it with CXF:

import com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider;
import org.apache.cxf.interceptor.LoggingInInterceptor;
import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.jaxrs.client.ClientConfiguration;
import org.apache.cxf.jaxrs.client.WebClient;
import org.json.JSONException;
import org.json.JSONObject;
...
    WebClient client = WebClient.create(endPoint, providers).accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON);
    ClientConfiguration config = WebClient.getConfig(client);
    config.getInInterceptors().add(new LoggingInInterceptor());
    config.getOutInterceptors().add(new LoggingOutInterceptor());
Sridhar Sarnobat
  • 25,183
  • 12
  • 93
  • 106
Daniel Kaplan
  • 62,768
  • 50
  • 234
  • 356
4

Using the JAX-RS 2.0 client from CXF 3.1.x I am having some trouble getting the LoggingInInterceptor and LoggingOutInterceptor to work. I changed to use the LoggingFeature as described at http://cxf.apache.org/docs/message-logging.html and it worked first try:

Client client = ClientBuilder.newBuilder().register(LoggingFeature.class).build()

lmsurprenant
  • 1,723
  • 2
  • 14
  • 28
0

You can pass directly an instance of LoggingFeature to your client using the appropriate create() method.

Response response = WebClient
                .create(baseAddress,
                        Arrays.<Object>asList(Arrays
                                .<Object>asList(new JacksonJsonProvider())),
                        Arrays.<Feature>asList(new LoggingFeature()), null)
                .type(MediaType.APPLICATION_JSON)
                .accept(MediaType.APPLICATION_JSON).path(path).post(request);
Zeuzif
  • 318
  • 2
  • 14