Java Kotlin:未调用RestTemplateCustomizer customize()方法 我正在为应用程序中的所有RESTTemplateAPI调用编写一个定制ResponseErrorHandler。此应用程序是一个Spring引导应用程序,用Kotlin编写
我的代码: MposApiErrorHandler.classJava Kotlin:未调用RestTemplateCustomizer customize()方法 我正在为应用程序中的所有RESTTemplateAPI调用编写一个定制ResponseErrorHandler。此应用程序是一个Spring引导应用程序,用Kotlin编写,java,spring,spring-boot,kotlin,resttemplate,Java,Spring,Spring Boot,Kotlin,Resttemplate,我的代码: MposApiErrorHandler.class package jp.co.blayn.linkage.agent.utils.http.api.generic import jp.co.blayn.linkage.agent.config.MposApiEnpoint import jp.co.blayn.linkage.agent.service.LogService import jp.co.blayn.linkage.agent.service.agent.AgentA
package jp.co.blayn.linkage.agent.utils.http.api.generic
import jp.co.blayn.linkage.agent.config.MposApiEnpoint
import jp.co.blayn.linkage.agent.service.LogService
import jp.co.blayn.linkage.agent.service.agent.AgentAuthService
import jp.co.blayn.linkage.agent.service.implement.LogServiceImpl
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.http.HttpMethod
import org.springframework.http.HttpStatus
import org.springframework.http.client.ClientHttpResponse
import org.springframework.web.client.DefaultResponseErrorHandler
import org.springframework.web.client.ResponseErrorHandler
import java.net.URI
class MposApiErrorHandler(
@Autowired
val agentAuthService: AgentAuthService
) : DefaultResponseErrorHandler() {
companion object {
private val logger: LogService = LogServiceImpl(MposApiErrorHandler::class.java)
}
override fun hasError(response: ClientHttpResponse): Boolean {
return (response.statusCode.is4xxClientError || response.statusCode.is5xxServerError)
}
override fun handleError(response: ClientHttpResponse) {
logger.info("LOGIN MPOS AGAIN 1")
}
override fun handleError(url: URI, method: HttpMethod, response: ClientHttpResponse) {
if (response.statusCode == HttpStatus.UNAUTHORIZED && MposApiEnpoint.isMposApiEndpoint(url = url.toString())) {
logger.info("LOGIN MPOS AGAIN")
agentAuthService.callLogin();
}
}
}
CustomRestTemplateCustomizer类
package jp.co.blayn.linkage.agent.utils.http.api.generic
import jp.co.blayn.linkage.agent.service.agent.AgentAuthService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.web.client.RestTemplateCustomizer
import org.springframework.web.client.RestTemplate
class CustomRestTemplateCustomizer(
@Autowired
val agentAuthService: AgentAuthService
) : RestTemplateCustomizer {
override fun customize(restTemplate: RestTemplate) {
restTemplate.errorHandler = MposApiErrorHandler(agentAuthService = agentAuthService)
}
}
ClientHttpConfig类
package jp.co.blayn.linkage.agent.config
import jp.co.blayn.linkage.agent.service.agent.AgentAuthService
import jp.co.blayn.linkage.agent.utils.http.api.generic.CustomRestTemplateCustomizer
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
@Configuration
class ClientHttpConfig(
@Autowired
val agentAuthService: AgentAuthService
) {
@Bean
fun customRestTemplateCustomizer(): CustomRestTemplateCustomizer {
return CustomRestTemplateCustomizer(agentAuthService = agentAuthService);
}
}
问题是当我运行时,RestTemplate仍然使用DefaultResponseErrorHandler处理错误,调试后,我很快意识到,类CustomRestTemplateCustomizer中的customize()方法从未被调用。
所以我的问题是:
- customize()方法是否应该自动调用?如果没有,我该如何称呼它
- 我的代码可以实现CustomizeResponseErrorHandler吗
注意:我遵循Java教程:在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创建这不是我的问题的答案:(调试时,我已经按照您的建议进行了尝试,只需MposApiErrorHandler.class就可以正常工作,不需要其他类。但是我创建这些自定义类的主要原因是它可以为整个应用程序中的所有RestTemplate API调用进行配置。RestTemplateBuilder调用customize来创建RestTemplate,因此它无法工作通过这种方式,如果您自己创建其他RestTemplate实例,在本教程第3节中,它建议这是一种让应用程序中使用的每个RestTemplate都在Java中设置自定义errorHandler的方法。因此,您认为我在这里误解了什么,或者它在Kotlin中不起作用吗?如果您愿意,在Java中也是一样的使用new创建RestTemplate它不使用customizer…本教程在这一部分中有错误,或者只是缺少通过RestTemplateBuilder创建它的提示。只需在配置类中定义一个bean,并将创建的RestTemplate实例注入到要使用它的类中。或者注入RestTemplateBuilder并使用build()