3

I am trying to get ClientHttpRequestInterceptor working following, Baeldung's Spring RestTemplate Request/Response Logging. The problem is the ClientHttpRequestInterceptor never gets called.

I had a similar issue with a HandlerInterceptor and a HandlerInterceptorAdapter interceptors. The issue was I needed a add a listener, that 99% of the article I found did not mention.

@Configuration
public class ListenerConfig implements WebApplicationInitializer {
    @Override
    public void onStartup(ServletContext sc) throws ServletException {
        sc.addListener(new RequestContextListener());
    }
}

I am guessing something about Spring has changed and the listeners are not there by default.

Does anyone know the listener for ClientHttpRequestInterceptor?

The interceptor:

public class LoggingInterceptor implements ClientHttpRequestInterceptor {

    static Logger LOGGER = LoggerFactory.getLogger(LoggingInterceptor.class);

    @Override
    public ClientHttpResponse intercept(
            HttpRequest req, byte[] reqBody, ClientHttpRequestExecution ex) throws IOException {
        LOGGER.debug("Request body: {}", new String(reqBody, StandardCharsets.UTF_8));
        ClientHttpResponse response = ex.execute(req, reqBody);
        InputStreamReader isr = new InputStreamReader(
          response.getBody(), StandardCharsets.UTF_8);
        String body = new BufferedReader(isr).lines()
            .collect(Collectors.joining("\n"));
        LOGGER.debug("Response body: {}", body);
        return response;
    }
}

RestClientConfig:

@Configuration
public class RestClientConfig {

    @Bean
    public RestTemplate restTemplate() {
        RestTemplate restTemplate = new RestTemplate();

        List<ClientHttpRequestInterceptor> interceptors
          = restTemplate.getInterceptors();
        if (CollectionUtils.isEmpty(interceptors)) {
            interceptors = new ArrayList<>();
        }
        interceptors.add(new LoggingInterceptor());
        restTemplate.setInterceptors(interceptors);
        return restTemplate;
    }
}

This may be related to ClientHttpRequestInterceptor not called in springboot.

William Jones
  • 61
  • 1
  • 7
  • If your `RestTemplate` works fine but interceptor haven't bean added please put the portion of the code in which you use your `RestTemplate`. – Tashkhisi Oct 24 '20 at 08:57
  • This is a simple REST service. There is no `RestTemplate` code, only a controller with a `@RestController` annotation. That with `@RequestBody` in the mapping methods, should be calling the `RestTemplate`. The controller works fine. – William Jones Oct 25 '20 at 09:37
  • Have you `@Autowired` it in your Controller? – Tashkhisi Oct 25 '20 at 09:55
  • Yes an `@Autowired` for the service, the controller calls. The service builds a custom response model, which the controller returns and Spring turns into the JSON body of the response. – William Jones Oct 26 '20 at 10:30

1 Answers1

1

The Spring RestTemplate Interceptor, only works on the client(consumer) side or in test where the tests are acting as the client where you have a RestTemplate. I am trying to log on the server(producer) side. Thus, no need for a RestTemplate.

A Loredana with Bealdung.com was nice enough to email me and point out, I had a misconception.

Somewhere I had picked up Spring used the RestTemplate for the auto decoding and encoding of POJOs in Rest controllers. That is incorrect.

@Tashkhisi was also point to the lack a RestTempale in the commets.

William Jones
  • 61
  • 1
  • 7