Spring Boot-休息模板和休息模板构建器 [英] Spring boot - rest template and rest template builder

查看:119
本文介绍了Spring Boot-休息模板和休息模板构建器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,RestTemplateBuilderRestTemplate的某种工厂.我对使用它有一些疑问:

As I know the RestTemplateBuilder is some kind of factory for RestTemplate. I have a few questions about using it:

  1. 在很多例子中,@Configuration类中经常有这样的东西:

  1. Very often in examples there is something like this in @Configuration class:

@Bean
public RestTemplate getRestClient() {
    RestTemplate restClient = new RestTemplate();
    ...
    return restClient;
}

不是每个@Service类都实例化RestTemplate吗?如果是这样,如何自定义它?

Shouldn't RestTemplate be instantiated per @Service class ? If so, how to customize it ?

Spring参考指出RestTemplateBuilder应该通过RestTemplateCustomizer自定义.如何使用一个构建器来管理来自多个IP地址的多个URI?

Spring reference says that RestTemplateBuilder should be customized via RestTemplateCustomizer. How to manage many URI's from many IP addresses with one builder ?

如何通过RestTemplateBuilder向所有RestTemplates添加BasicAuthentication全局变量,这是一个好习惯吗?

How to add BasicAuthentication globaly to all RestTemplates via RestTemplateBuilder, and is it a good practice?

感谢您的帮助.

更新:

我的应用程序从许多位于不同IP和URL的服务器上调用休息服务-因此,从逻辑上来说,当我有很多RestTemplates时就是这种情况.

My application calls rest services from many servers at different IP's and urls - so logically for me is the situation when I have many RestTemplates.

我正在尝试为每个服务器建立一个工厂(RestTemplateBuilder)-假设服务器A,B,C.我知道如何添加基本身份验证.但是,例如,当我想要对服务器A进行基本身份验证而不对服务器B进行基本身份验证时?

I'm trying to have a factory (RestTemplateBuilder) per server - let's say servers A, B, C. I know how to add a basic authentication. But what for example when I want a basic authentication for server A but not for server B ?

我考虑每个服务器有一个RestTemplateBuilder.我不想手动执行此操作-我更喜欢使用Spring机制.

I think about having one RestTemplateBuilder per server. I don't want to do this manually - I would prefer to use Spring mechanisms.

有帮助吗?

推荐答案

  1. 不,您不需要,通常,您将拥有rest模板实例,并且将传递不同的url,并每次都相应地请求参数.

  1. No, you don't need to, typically you will have on rest template instance, and you would pass different url, and request parameters accordingly every time.

String result = restTemplate.getForObject("http://example.com/hotels/{hotel}/bookings/{booking}", String.class, vars);

Foo foo = restTemplate.getForObject(fooResourceUrl + "/1", Foo.class);

  • 来自

  • A descriptive example from spring doc, you can add as many customizers to the builder

    public class ProxyCustomizer implements RestTemplateCustomizer {
    
        @Override
        public void customize(RestTemplate restTemplate) {
            HttpHost proxy = new HttpHost("proxy.example.com");
            HttpClient httpClient = HttpClientBuilder.create()
                    .setRoutePlanner(new DefaultProxyRoutePlanner(proxy) {
    
                        @Override
                        public HttpHost determineProxy(HttpHost target,
                                HttpRequest request, HttpContext context)
                                        throws HttpException {
                            if (target.getHostName().equals("192.168.0.5")) {
                                return null;
                            }
                            return super.determineProxy(target, request, context);
                        }
    
                    }).build();
            restTemplate.setRequestFactory(
                    new HttpComponentsClientHttpRequestFactory(httpClient));
        }
    
    }
    

  • 任何RestTemplateCustomizer Bean都会自动添加到 自动配置的RestTemplateBuilder.此外,新的 带有其他定制程序的RestTemplateBuilder可以通过以下方式创建 调用AdditionalCustomizers(RestTemplateCustomizer ...)

    Any RestTemplateCustomizer beans will be automatically added to the auto-configured RestTemplateBuilder. Furthermore, a new RestTemplateBuilder with additional customizers can be created by calling additionalCustomizers(RestTemplateCustomizer…​)

    @Bean
    public RestTemplateBuilder restTemplateBuilder() {
       return new RestTemplateBuilder()
            .rootUri(rootUri)
            .basicAuthorization(username, password);
    }
    

    这篇关于Spring Boot-休息模板和休息模板构建器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆