restloggingcxfcxf-client

How do you get logging from the CXF Rest Client?


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


Solution

  • 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());