title: Kotlin with Spring-Retry the @Recover not called
body: <p>I'm having a problem using spring boot and spring retry, the @Recover annotation method is not being called when making all the possible attempts.</p>
  
  <p>I'm using spring with kotlin.</p>
  
  <p>My Application servlet container:</p>
  
  <pre><code>class ServletInitializer : SpringBootServletInitializer() {
  
      override fun configure(application: SpringApplicationBuilder) : SpringApplicationBuilder {
          return application.sources(SecurityServicesApplication::class.java)
      }
  }
  </code></pre>
  
  <p>My Configuration</p>
  
  <p>import org.springframework.context.annotation.Configuration
  import org.springframework.retry.annotation.EnableRetry</p>
  
  <pre><code>@Configuration
  @EnableRetry
  class RetryConfig
  </code></pre>
  
  <p><strong>Updated</strong></p>
  
  <p>My Service</p>
  
  <pre><code>import security.commons.exception.SecurityException
  import org.apache.commons.lang3.exception.ExceptionUtils
  import org.slf4j.LoggerFactory
  import org.springframework.beans.factory.annotation.Value
  import org.springframework.retry.annotation.Backoff
  import org.springframework.retry.annotation.Recover
  import org.springframework.retry.annotation.Retryable
  import org.springframework.security.oauth2.client.OAuth2RestTemplate
  import org.springframework.security.oauth2.common.OAuth2AccessToken
  import org.springframework.stereotype.Service
  import java.net.ConnectException
  
  @Service
  class AuthorizationServerTokenRequester {
  
      val log = LoggerFactory.getLogger(Oauth2Service::class.java)!!
  
      @Value("\${accessTokenUri}")
      private val accessTokenUri: String? = null
  
  
      @Retryable(
              value = [SecurityException::class],
              maxAttemptsExpression = "\${server.oauthclient.retry.maxAttempts}",
              backoff = Backoff(delayExpression = "\${server.oauthclient.retry.delay}"))
      fun token(oauth2RestTemplate: OAuth2RestTemplate): OAuth2AccessToken? {
          try {
  
              return oauth2RestTemplate.accessToken
          } catch (ex: Exception) {
              if (ExceptionUtils.getRootCause(ex) is ConnectException) {
                  log.error("trying again....")
              }
              throw com.security.commons.exception.SecurityException("")
          }
      }
  
      @Recover
      fun recover(ex: SecurityException) {
          print("##############################################################################################sssss# SecurityException")
      }
  }
  </code></pre>
  
  <p><strong>My Error Log:</strong></p>
  
  <pre><code>2018-08-10 11:28:41.802 DEBUG 40168 --- [nio-8080-exec-1] o.s.w.s.m.m.a.HttpEntityMethodProcessor  : 
      Written [{timestamp=Fri Aug 10 11:28:41 BRT 2018, status=500, error=Internal Server Error, exception=org.springframework.retry.ExhaustedRetryException, 
                message=Cannot locate recovery method; nested exception is security.commons.exception.SecurityException: , 
                path=/security/api/v1/oauth2/token}] as "application/json" using [org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@21e2d8f6]
  </code></pre>
  
  <p><strong><em>Solution [with the help of @Gary Russell]</em></strong></p>
  
  <p><strong>The return of the @Recover method must be the same as the @Retryable method</strong></p>
  
  <pre><code>fun recover(ex: S@RecoverecurityException, oauth2RestTemplate: OAuth2RestTemplate ) : OAuth2AccessToken {
      print("##############################################################################################sssss# SecurityException")
      throw br.com.totvs.security.commons.exception.SecurityException("")
  }
  </code></pre>