Kotlin:未调用 RestTemplateCustomizer customize() 方法
Kotlin: RestTemplateCustomizer customize() method is not being called
- 我正在为应用程序中的所有 RestTemplate api 调用编写自定义 ResponseErrorHandler。此应用程序是一个 Spring 启动应用程序,用 Kotlin
编写
我的代码:
ServiceApiErrorHandler.class
class ServiceApiErrorHandler(
@Autowired
val agentAuthService: AgentAuthService
) : DefaultResponseErrorHandler() {
companion object {
private val logger: LogService = LogServiceImpl(ServiceApiErrorHandler::class.java)
}
override fun hasError(response: ClientHttpResponse): Boolean {
return (response.statusCode.is4xxClientError || response.statusCode.is5xxServerError)
}
override fun handleError(response: ClientHttpResponse) {
logger.info("LOGIN AGAIN 1")
}
override fun handleError(url: URI, method: HttpMethod, response: ClientHttpResponse) {
if (response.statusCode == HttpStatus.UNAUTHORIZED && ServiceApiEnpoint.isServiceApiEndpoint(url = url.toString())) {
logger.info("LOGIN AGAIN")
agentAuthService.callLogin();
}
}
}
CustomRestTemplateCustomizer class
class CustomRestTemplateCustomizer(
@Autowired
val agentAuthService: AgentAuthService
) : RestTemplateCustomizer {
override fun customize(restTemplate: RestTemplate) {
restTemplate.errorHandler = ServiceApiErrorHandler(agentAuthService = agentAuthService)
}
}
ClientHttpConfig class
@Configuration
class ClientHttpConfig(
@Autowired
val agentAuthService: AgentAuthService
) {
@Bean
fun customRestTemplateCustomizer(): CustomRestTemplateCustomizer {
return CustomRestTemplateCustomizer(agentAuthService = agentAuthService);
}
}
问题是当我 运行 时,RestTemplate 仍然使用 DefaultResponseErrorHandler 处理错误,调试后,我很快意识到 class CustomRestTemplateCustomizer 中的 customize() 方法从未被打电话。
所以我的问题是:
- customize() 方法应该是自动调用的吗?如果不是,我应该怎么称呼它?
- 我的代码可以实现自定义 ResponseErrorHandler 吗?
注意:我按照 Java 中的教程:https://www.baeldung.com/spring-rest-template-builder 使用 Kotlin 编写此版本。
如何创建 RestTemplate 实例?
尝试添加一个 Bean,然后应该使用定制器
@Configuration
class ClientHttpConfig(
@Autowired
val agentAuthService: AgentAuthService,
@Autowired val builder: RestTemplateBuilder
) {
@Bean
fun customRestTemplateCustomizer(): CustomRestTemplateCustomizer {
return CustomRestTemplateCustomizer(agentAuthService = agentAuthService);
}
@Bean
fun customizedRestTemplate(): RestTemplate {
return builder.build();
}
}
编辑:需要通过RestTemplateBuilder创建
- 我正在为应用程序中的所有 RestTemplate api 调用编写自定义 ResponseErrorHandler。此应用程序是一个 Spring 启动应用程序,用 Kotlin 编写
我的代码:
ServiceApiErrorHandler.class
class ServiceApiErrorHandler(
@Autowired
val agentAuthService: AgentAuthService
) : DefaultResponseErrorHandler() {
companion object {
private val logger: LogService = LogServiceImpl(ServiceApiErrorHandler::class.java)
}
override fun hasError(response: ClientHttpResponse): Boolean {
return (response.statusCode.is4xxClientError || response.statusCode.is5xxServerError)
}
override fun handleError(response: ClientHttpResponse) {
logger.info("LOGIN AGAIN 1")
}
override fun handleError(url: URI, method: HttpMethod, response: ClientHttpResponse) {
if (response.statusCode == HttpStatus.UNAUTHORIZED && ServiceApiEnpoint.isServiceApiEndpoint(url = url.toString())) {
logger.info("LOGIN AGAIN")
agentAuthService.callLogin();
}
}
}
CustomRestTemplateCustomizer class
class CustomRestTemplateCustomizer(
@Autowired
val agentAuthService: AgentAuthService
) : RestTemplateCustomizer {
override fun customize(restTemplate: RestTemplate) {
restTemplate.errorHandler = ServiceApiErrorHandler(agentAuthService = agentAuthService)
}
}
ClientHttpConfig class
@Configuration
class ClientHttpConfig(
@Autowired
val agentAuthService: AgentAuthService
) {
@Bean
fun customRestTemplateCustomizer(): CustomRestTemplateCustomizer {
return CustomRestTemplateCustomizer(agentAuthService = agentAuthService);
}
}
问题是当我 运行 时,RestTemplate 仍然使用 DefaultResponseErrorHandler 处理错误,调试后,我很快意识到 class CustomRestTemplateCustomizer 中的 customize() 方法从未被打电话。
所以我的问题是:
- customize() 方法应该是自动调用的吗?如果不是,我应该怎么称呼它?
- 我的代码可以实现自定义 ResponseErrorHandler 吗?
注意:我按照 Java 中的教程:https://www.baeldung.com/spring-rest-template-builder 使用 Kotlin 编写此版本。
如何创建 RestTemplate 实例?
尝试添加一个 Bean,然后应该使用定制器
@Configuration
class ClientHttpConfig(
@Autowired
val agentAuthService: AgentAuthService,
@Autowired val builder: RestTemplateBuilder
) {
@Bean
fun customRestTemplateCustomizer(): CustomRestTemplateCustomizer {
return CustomRestTemplateCustomizer(agentAuthService = agentAuthService);
}
@Bean
fun customizedRestTemplate(): RestTemplate {
return builder.build();
}
}
编辑:需要通过RestTemplateBuilder创建