All Articles

How to Log Rest Requests and Responses With Spring REST Template

Throughout my career, I have integrated with a bunch of different services. Most have been SOAP services. Recently I have had to integrate more with REST services. In general, when integrating with a service it can be incredibly hard to debug issues when you cannot see what is being sent and received. REST is not special in this regard.

For the one fairly complex integration I was running into marshalling issues due to values not being in the response or being in a different JSON path. This can be debugged by using Postman with the same request, but this gets tedious very quickly.

After doing a bit of Googling I came across this answer. While I did not end up using the proposed solution, it set me on the right track. My solution consists of the following parts:

  • A REST Interceptor
  • A properties file to control when this interceptor is turned on/off (this check happens when the client is initialized on startup)
  • Use of the RestTemplateBuilder in the REST client to bring all this together

    • It is very important to use a BufferingClientHttpRequestFactory otherwise the stream will be empty/null after intercepting it (i.e. the consumer of the response will get back null)

The REST Interceptor

The terminology may sound all hardcore. But in reality, this is simply a piece of code that runs before or/and after every request and response.

Below I have my implementation of the interceptor which should work with basically any rest service. The only thing you may want to tweak is the log level as well as how and when items are logged. To skip logging simply pass the request/response straight through the implemented interface methods.

import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.http.HttpRequest
import org.springframework.http.client.ClientHttpRequestExecution
import org.springframework.http.client.ClientHttpRequestInterceptor
import org.springframework.http.client.ClientHttpResponse

class RESTInterceptor : ClientHttpRequestInterceptor {
    override fun intercept(request: HttpRequest, body: ByteArray, execution: ClientHttpRequestExecution): ClientHttpResponse {
        traceRequest(request, body)
        val response = execution.execute(request, body)
        return response

    private fun traceRequest(request: HttpRequest, body: ByteArray) {"===========================request begin================================================")"URI         : {}", request.uri)"Method      : {}", request.method)"Headers     : {}", request.headers)"Request body: {}", String(body, Charsets.UTF_8))"==========================request end================================================")

    private fun traceResponse(response: ClientHttpResponse) {
        val inputStringBuilder = StringBuilder()
        val bufferedReader = BufferedReader(InputStreamReader(response.body, "UTF-8"))
        var line = bufferedReader.readLine()
        while (line != null) {
            line = bufferedReader.readLine()
        }"============================response begin==========================================")"Status code  : {}", response.statusCode)"Status text  : {}", response.statusText)"Headers      : {}", response.headers)"Response body: {}", inputStringBuilder.toString())"=======================response end=================================================")

    companion object {
        val log: Logger = LoggerFactory.getLogger(

The Properties File

You can name the property however you want. I normally just use the name of the service so that I can use this with different services independently. If you are using this in a sort of manual unit test you can simply construct your client with the bean of this property configured as you see fit.

The REST Client

The below client is fairly standard except for the interceptor being conditionally added based on our property and authentication being added to every request header (this is only needed if the service you are integrating with needs authentication)

class MyRestClient(theRestClientsProperties: MyRestClientProperties) {

private val restTemplate: RestTemplate = buildRestTemplate(theRestClientsProperties)

private fun buildRestTemplate(theRestClientsProperties: MyRestClientProperties): RestTemplate {

    if (theRestClientsProperties.debug.toLowerCase() == "yes") {
        return RestTemplateBuilder()
                .requestFactory { BufferingClientHttpRequestFactory(HttpComponentsClientHttpRequestFactory()) }
                .basicAuthentication(theRestClientsProperties.user, theRestClientsProperties.password)
    return RestTemplateBuilder()
            .requestFactory { HttpComponentsClientHttpRequestFactory() }
            .basicAuthentication(theRestClientsProperties.user, theRestClientsProperties.password)