My intent is to audit the data exchange through the endpoints of my application. I wanna retrieve the json exchanged throught the endpoints and store it in the database. For storing the json data I will use Javes (https://javers.org/) but I still need to figure out how to obtain the endpoint data.
Asked
Active
Viewed 900 times
1
-
1Write an interceptor may be to catch the request and response by implementing `ClientHttpRequestInterceptor` – Ubercool Jan 09 '19 at 12:54
1 Answers
3
You can write interceptor to do additional processing of requests and responses flowing through your application.
If you are using spring
it is very easy to implement. Here is an example:
@Component
public class RestInterceptor implements ClientHttpRequestInterceptor {
@Override
public ClientHttpResponse intercept(HttpRequest request, byte[] body,
ClientHttpRequestExecution execution) throws IOException {
traceRequest(request, body);
ClientHttpResponse response = execution.execute(request, body);
traceResponse(response);
return response;
}
private void traceRequest(HttpRequest request, byte[] body) throws IOException {
//track request here
}
private void traceResponse(ClientHttpResponse response) throws IOException {
//track response here
}
}
Now register it with your rest template
restTemplate.setInterceptors(Collections.singletonList(new RestInterceptor ()));

Ubercool
- 1,029
- 2
- 14
- 29
-
Where do I configure the following " Register ClientHttpRequestInterceptor with RestTemplate" in a short spring boot application. I am following an example here: howtodoinjava.com/spring-restful/clienthttprequestinterceptor – Rodolfo Jan 10 '19 at 03:21
-
Follow [this question](https://stackoverflow.com/q/33744875/3503187) and you can also check logback-access to log requests coming to your application. The example in the answer logs request and responses going from your application. – Ubercool Jan 10 '19 at 05:05