【问题标题】:calling method with Rest Template Builder使用 Rest Template Builder 调用方法
【发布时间】:2019-10-11 23:56:21
【问题描述】:

我使用休息模板生成器创建了这个休息模板,并设置了连接和读取超时。我需要从程序中的其他方法调用这个休息模板,但我不确定如何这样做。请帮忙,提前谢谢!

//create rest template with rest template builder and set connection and read timeouts        @Bean
public RestTemplate restTemplate(RestTemplateBuilder restTemplateBuilder) {



    return restTemplateBuilder
            .setConnectTimeout(Duration.ofMillis(connectTimeout))
            .setReadTimeout(Duration.ofMillis(readTimeout))
            .build();
}

// this is an example method that calls rest template, unsure what goes in the parameter section
@Bean
public example example() {
    return new restTemplate(what goes here)
    );
}

【问题讨论】:

  • 我想你可能想要RestTemplateCustomizer。看hereherehere
  • 所以我运行这个rest模板的应用程序在repo的各个部分调用这个rest模板。但在我集成 rest 模板构建器之前,repo 的各种方法只会调用默认的 rest 模板,restTemplate()。所以现在我已经集成了其余模板构建器,它需要一个参数。既然它是用 rest Temple builder 创建的,我该如何调用它?我希望这是有道理的。

标签: java spring maven spring-boot


【解决方案1】:

如果您已经创建了自定义的 RestTemplate,您可以将它自动装配到您想要调用它的任何类并使用它。如果您有超过 1 个 RestTemplates,您可以在 RestTemplate Bean 上方使用 @Qualifier 并在调用类中使用相同的。

【讨论】:

    【解决方案2】:

    RestTemplateBuilder 是 Spring boot 提供的一个 bean。您可以将其注入到任何 Spring bean 类中。

    然后您只想在创建 Spring bean 类时配置 restTemplate 并将其存储为字段。您可以执行以下操作(这不是唯一的方法)。

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.web.client.RestTemplateBuilder;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.client.RestTemplate;
    
    import java.time.Duration;
    
    @Configuration
    public class MyExampleRestClientConfiguration {
        private final RestTemplateBuilder restTemplateBuilder;
    
        @Autowired
        public MyExampleRestClient(RestTemplateBuilder restTemplateBuilder) {
            this.restTemplateBuilder = restTemplateBuilder;
        }
    
        @Bean
        public RestTemplate restTemplate() {
            return restTemplateBuilder
            .setConnectTimeout(Duration.ofMillis(connectTimeout))
            .setReadTimeout(Duration.ofMillis(readTimeout))
            .build();
        }
    }
    

    现在在其他一些 spring bean 类中,您可以简单地连接 restTemplate bean 并重新使用。

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    import org.springframework.web.client.RestTemplate;
    
    @Component
    public class MyExampleRestClient {
      private final RestTemplate restTemplate;
    
      @Autowired
      public MyExampleRestClient(RestTemplate restTemplate) {
          this.restTemplate = restTemplate;
      }
    
      //Now You can call restTemplate in any method
    }
    

    您可以参考this了解更多信息。

    【讨论】:

    • 您好,感谢您的帮助。我有点困惑如何实现这个,你能解释一下吗?
    • 我无法声明restTemplate = restTemplateBuilder,因为restTemplate 被声明为final。另外,在调用其余模板时,如果参数部分中有值,我仍然不明白
    • 所以我在我正在使用的 java 文件的主类中声明了最终的 rest 模板,然后我在上面由 @SupunWijerathne 指定的 rest 模板上方进行了 Autowired。当我确实使用它时,我不确定该方法的返回值是什么。创建其余模板时,我是否仅使用函数名称的值来调用其余模板?
    • 是的,我想使用在代码的不同区域调用的其余模板。
    • 感谢您的帮助。该代码有效,但奇怪的是,在设置超时后,我正在测试应用程序是否会生成连接超时但成功。该应用程序仍在正常运行。任何想法为什么连接没有超时?我已将连接超时设置为 1 秒。
    猜你喜欢
    • 1970-01-01
    • 2019-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-10
    相关资源
    最近更新 更多