-
-
Save jacqueminv/5a51b138f795353b8392c6807ea6fb04 to your computer and use it in GitHub Desktop.
Spring RestTemplate interceptor to optionnally log HTTP traffic.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import org.slf4j.Logger; | |
| import org.slf4j.LoggerFactory; | |
| import org.springframework.http.HttpMethod; | |
| import org.springframework.http.client.BufferingClientHttpRequestFactory; | |
| import org.springframework.http.client.ClientHttpRequestFactory; | |
| import java.net.URI; | |
| public class BufferingOptionallyClientHttpRequestFactory extends BufferingClientHttpRequestFactory { | |
| private static final Logger LOGGER = LoggerFactory.getLogger("restPayloads"); | |
| /** | |
| * Create a buffering wrapper for the given {@link ClientHttpRequestFactory}. | |
| * | |
| * @param requestFactory the target request factory to wrap | |
| */ | |
| public BufferingOptionallyClientHttpRequestFactory(ClientHttpRequestFactory requestFactory) { | |
| super(requestFactory); | |
| } | |
| @Override | |
| protected boolean shouldBuffer(URI uri, HttpMethod httpMethod) { | |
| return LOGGER.isDebugEnabled(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | |
| import java.io.ByteArrayOutputStream; | |
| import java.io.IOException; | |
| import java.io.InputStream; | |
| import java.nio.charset.Charset; | |
| import static java.nio.charset.StandardCharsets.UTF_8; | |
| public class LoggingInterceptor implements ClientHttpRequestInterceptor | |
| { | |
| private static final Logger LOGGER = LoggerFactory.getLogger("restPayloads"); | |
| @Override | |
| public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) | |
| throws IOException { | |
| if (LOGGER.isDebugEnabled()) { | |
| LOGGER.debug("HTTP {} request against {} with payload {}", request.getMethod(), request.getURI(), new String(body, UTF_8)); | |
| } | |
| ClientHttpResponse response = execution.execute(request, body); | |
| if (LOGGER.isDebugEnabled()) { | |
| ByteArrayOutputStream out = new ByteArrayOutputStream(); | |
| StreamUtils.copy(response.getBody(), out); | |
| LOGGER.debug("HTTP Response {} with payload {}", response.getStatusCode().value(), out.toString(UTF_8.name())); | |
| } | |
| return response; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import org.springframework.web.client.RestTemplate; | |
| public class Main { | |
| public static void main(String ... args) { | |
| RestTemplate restTemplate = new RestTemplate(); | |
| restTemplate.setRequestFactory(new BufferingOptionallyClientHttpRequestFactory(restTemplate.getRequestFactory())); | |
| //To make the interceptor do anything, set the "restPayloads" logger level to DEBUG | |
| restTemplate.getInterceptors().add(new RestHttpRequestLogger()); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment