I am writing a Spring application using Kotlin that has:
Right now I'm using RestTemplate to call API, so my questions is what is the best pratices to modify RestTemplate so if (response is 401 and url is in the second API list) then auto call login?
Example of my code:
List API endpoints:
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.annotation.Configuration
@Configuration
class AppApiEnpoint(
@Autowired
private val appProperties: AppProperties
) {
var orderCreateUrl: String = appProperties.getUrl("/order/create.json").toString()
}
API call
fun create(contentRequest: OrderSendAgentDto, accessToken: String): ResponseEntity<OrderResponseDto> {
val requestUri = UrlBuilder(appApiEnpoint.orderCreateUrl).toString()
val headers = HttpHeaders()
headers.contentType = MediaType.APPLICATION_FORM_URLENCODED
val body = HttpUtils.convertObject2Map(contentRequest)
body.add("access_token", accessToken)
val request = HttpEntity(body, headers)
val restTemplate = RestTemplate()
val response = restTemplate.postForEntity(requestUri, request, OrderResponseDto::class.java)
return response
}
Note:
You can set ResponseErrorHandler
to the rest template that should do auto-login, or even use @Retry with retry handlers on API methods that should do auto-login