【发布时间】:2019-12-02 18:03:40
【问题描述】:
我想配置多个 rest 模板客户端来访问不同的 API。两者都有不同的授权标头。我已经配置了一个,同样配置了其他的rest模板,但是会抛出错误bean 'restTemplate' defined in class path resource .class could not be registered.。
@Configuration
public class RestTemplateConfig {
@Autowired
private HeaderRequestInterceptor headerRequestInterceptor;
//constructor
public RestClientConfig() {}
@Bean
public RestTemplate restTemplate( RestTemplateBuilder builder ) {
RestTemplate restTemplate = builder.build();
restTemplate.setInterceptors(Collections.singletonList(headerRequestInterceptor));
return restTemplate;
}
}
HeaderRequestInterceptor 具有 base64 编码授权,因此无法在此处发布该代码。
另一个 RestTemplate:
@Configuration
public class AnotherRestClientConfig {
@Autowired
private AnotherHeaderRequestInterceptor anotherHeaderRequestInterceptor;
@Bean
public RestTemplate restTemplate( RestTemplateBuilder builder ) {
RestTemplate restTemplate = builder.build();
restTemplate.setInterceptors(Collections.singletonList(anotherHeaderRequestInterceptor));
return restTemplate;
}
}
谁能告诉我如何在一个应用程序中配置多个休息模板。
【问题讨论】:
-
是的,明白了。用名为 @Bean(name="") 的注释 2 个其余模板并使用 @Qualifier 自动装配它们。
标签: spring spring-boot resttemplate