title: Best pratice: How to make a customize RestTemplate auto retry it's failed api call?
body: <p>I have written an customize ResponseErrorHandler of RestTemplate as following code:</p>
  <pre><code>class ExternalApiErrorHandler(
      @Autowired
      val xxxAuthService: XXXAuthService
  ) : ResponseErrorHandler {
      companion object {
          private val logger: LogService = LogServiceImpl(ExternalApiErrorHandler::class.java)
      }
  
      override fun hasError(response: ClientHttpResponse): Boolean {
          return (response.statusCode.is4xxClientError || response.statusCode.is5xxServerError)
      }
  
      override fun handleError(url: URI, method: HttpMethod, response: ClientHttpResponse) {
          if (response.statusCode == HttpStatus.UNAUTHORIZED &amp;&amp; ExternalApiEnpoint.isExternalApiEndpoint(url = url.toString())) {
              xxxAuthService.callLogin();
      }
  }
  </code></pre>
  <p>}</p>
  <p>Right now, I only call auto login if the URL belong to the specific list ExternalApiEndpoint and the status code is 403. But I also want it to automatic retry it's failed action (like restTemplate.exchange, restTemplate.postForEntity,....). So my questions is: <strong>What is the best way to do this? Should I handle it also in the handleError() method or handle it separately with each API call? Any keyword for this problem would also be very helpful!!!</strong> Thanks in advance.</p>
  <p>Here is a code example of my service api:</p>
  <pre><code>@Service
  class OrderApi(
      @Autowired
      private val externalApiEnpoint: ExternalApiEnpoint,
      @Autowired
      private val restTemplate: RestTemplate
  ) {
  
      private var logService: LogService = LogServiceImpl(OrderApi::class.java)
  
      fun create(contentRequest: OrderSendXXXDto, accessToken: String): ResponseEntity&lt;OrderXXXResponseDto&gt; {
          val requestUri = UrlBuilder(ExternalApiEnpoint.orderCreateUrl)
              .toString()
  
          val headers = HttpHeaders()
          headers.contentType = MediaType.APPLICATION_FORM_URLENCODED
          val body = HttpUtils.convertObject2Map(contentRequest)
          val request = HttpEntity(body, headers)
          val response = restTemplate.postForEntity(requestUri, request, OrderXXXResponseDto::class.java)
  
          return response
      }
  }
  </code></pre>