Configure a RestTemplate with RestTemplateBuilder – 用RestTemplateBuilder配置一个RestTemplate

最后修改: 2018年 6月 7日

中文/混合/英文(键盘快捷键:t)

1. Introduction

1.介绍

In this quick tutorial, we’re going to look at how to configure a Spring RestTemplate bean.

在这个快速教程中,我们将看看如何配置Spring的RestTemplatebean.

Let’s start by discussing the three main configuration types:

让我们先讨论一下三种主要的配置类型。

  • using the default RestTemplateBuilder
  • using a RestTemplateCustomizer
  • creating our own RestTemplateBuilder

To be able to test this easily, please follow the guide on how to set up a simple Spring Boot application.

为了能够轻松地进行测试,请遵循如何设置一个简单的Spring Boot应用程序的指南。

2. Configuration Using the Default RestTemplateBuilder

2.使用默认的RestTemplateBuilder的配置

To configure a RestTemplate this way, we need to inject the default RestTemplateBuilder bean provided by Spring Boot into our classes:

要以这种方式配置RestTemplate,我们需要将Spring Boot提供的默认RestTemplateBuilderBean注入我们的类中。

private RestTemplate restTemplate;

@Autowired
public HelloController(RestTemplateBuilder builder) {
    this.restTemplate = builder.build();
}

The RestTemplate bean created with this method has its scope limited to the class in which we build it.

用这个方法创建的RestTemplatebean,其范围仅限于我们构建它的类

3. Configuration Using a RestTemplateCustomizer

3.使用RestTemplateCustomizer配置

With this approach, we can create an application-wide, additive customization.

通过这种方法,我们可以创建一个全应用的、可增加的定制。

This is a slightly more complicated approach. For this we need to create a class that implements RestTemplateCustomizer, and define it as a bean:

这是个稍微复杂的方法。为此我们需要创建一个实现RestTemplateCustomizer的类,并将其定义为一个bean。

public class CustomRestTemplateCustomizer implements RestTemplateCustomizer {
    @Override
    public void customize(RestTemplate restTemplate) {
        restTemplate.getInterceptors().add(new CustomClientHttpRequestInterceptor());
    }
}

The CustomClientHttpRequestInterceptor interceptor is doing basic logging of the request:

CustomClientHttpRequestInterceptor拦截器正在对请求做基本记录。

public class CustomClientHttpRequestInterceptor implements ClientHttpRequestInterceptor {
    private static Logger LOGGER = LoggerFactory
      .getLogger(CustomClientHttpRequestInterceptor.class);

    @Override
    public ClientHttpResponse intercept(
      HttpRequest request, byte[] body, 
      ClientHttpRequestExecution execution) throws IOException {
 
        logRequestDetails(request);
        return execution.execute(request, body);
    }
    private void logRequestDetails(HttpRequest request) {
        LOGGER.info("Headers: {}", request.getHeaders());
        LOGGER.info("Request Method: {}", request.getMethod());
        LOGGER.info("Request URI: {}", request.getURI());
    }
}

Now, we define CustomRestTemplateCustomizer as a bean in a configuration class or in our Spring Boot application class:

现在,我们将CustomRestTemplateCustomizer定义为配置类或Spring Boot应用类中的一个bean。

@Bean
public CustomRestTemplateCustomizer customRestTemplateCustomizer() {
    return new CustomRestTemplateCustomizer();
}

With this configuration, every RestTemplate that we’ll use in our application will have the custom interceptor set on it.

有了这个配置,我们将在应用程序中使用的每一个RestTemplate都将被设置为自定义拦截器。

4. Configuration by Creating Our Own RestTemplateBuilder

4.通过创建我们自己的RestTemplateBuilder进行配置

This is the most extreme approach to customizing a RestTemplate. It disables the default auto-configuration of RestTemplateBuilder, so we need to define it ourselves:

这是定制RestTemplate的最极端的方法。禁用了RestTemplateBuilder的默认自动配置所以我们需要自己定义它:

@Bean
@DependsOn(value = {"customRestTemplateCustomizer"})
public RestTemplateBuilder restTemplateBuilder() {
    return new RestTemplateBuilder(customRestTemplateCustomizer());
}

After this, we can inject the custom builder into our classes like we’d do with a default RestTemplateBuilder and create a RestTemplate as usual:

之后,我们可以像对待默认的RestTemplateBuilder那样,将自定义构建器注入我们的类中,并像往常一样创建一个RestTemplate

private RestTemplate restTemplate;

@Autowired
public HelloController(RestTemplateBuilder builder) {
    this.restTemplate = builder.build();
}

5. Conclusion

5.结论

We’ve seen how to configure a RestTemplate with the default RestTemplateBuilder, building our own RestTemplateBuilder, or using a RestTemplateCustomizer bean.

我们已经看到了如何用默认的RestTemplateBuilder配置RestTemplate,构建我们自己的RestTemplateBuilder,或者使用RestTemplateCustomizerbean。

As always, the full codebase for this example can be found in our GitHub repository.

一如既往,这个例子的完整代码库可以在我们的GitHub 仓库中找到。