Kotlin:未调用 RestTemplateCustomizer customize() 方法

Kotlin: RestTemplateCustomizer customize() method is not being called

我的代码:

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() 方法从未被打电话。

所以我的问题是:

注意:我按照 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创建