1. Overview
1.概述
In this short tutorial, we’ll take a look at how to send a request to a proxy using RestTemplate.
在这个简短的教程中,我们将看看如何使用proxy发送请求给RestTemplate。
2. Dependencies
2.依赖性
First, the RestTemplateCustomizer uses the HttpClient class to connect to the proxy.
首先,RestTemplateCustomizer使用HttpClient类来连接到代理。
To use the class, we need to add Apache’s httpcore dependency to our Maven pom.xml file:
要使用该类,我们需要将Apache的httpcore依赖项添加到我们的Mavenpom.xml文件中。
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.13</version>
</dependency>
Or to our Gradle build.gradle file:
或者到我们的Gradle build.gradle文件。
compile 'org.apache.httpcomponents:httpcore:4.4.13'
3. Using SimpleClientHttpRequestFactory
3.使用SimpleClientHttpRequestFactory
Sending a request to a proxy using RestTemplate is pretty simple. All we need to do is to call the setProxy(java.net.Proxy) from SimpleClientHttpRequestFactory before building the RestTemplate object.
使用RestTemplate向一个代理发送请求是非常简单的。我们需要做的就是在构建RestTemplate对象之前,从SimpleClientHttpRequestFactory调用setProxy(java.net.Proxy)。
First, we start by configuring the SimpleClientHttpRequestFactory:
首先,我们开始配置SimpleClientHttpRequestFactory。
Proxy proxy = new Proxy(Type.HTTP, new InetSocketAddress(PROXY_SERVER_HOST, PROXY_SERVER_PORT));
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
requestFactory.setProxy(proxy);
Then, we move forward to passing the request factory instance to the RestTemplate constructor:
然后,我们前进到将请求工厂实例传递给RestTemplate构造器。
RestTemplate restTemplate = new RestTemplate(requestFactory);
Finally, once we have built the RestTemplate, we can use it to make proxied requests:
最后,一旦我们建立了RestTemplate,我们就可以用它来进行代理请求。
ResponseEntity<String> responseEntity = restTemplate.getForEntity("http://httpbin.org/get", String.class);
assertThat(responseEntity.getStatusCode(), is(equalTo(HttpStatus.OK)));
4. Using RestTemplateCustomizer
4.使用RestTemplateCustomizer
Another approach is to use a RestTemplateCustomizer with RestTemplateBuilder to build a customized RestTemplate.
另一种方法是使用RestTemplateCustomizer与RestTemplateBuilder来构建一个定制的RestTemplate。
Let’s start defining a ProxyCustomizer:
让我们开始定义一个ProxyCustomizer。
class ProxyCustomizer implements RestTemplateCustomizer {
@Override
public void customize(RestTemplate restTemplate) {
HttpHost proxy = new HttpHost(PROXY_SERVER_HOST, PROXY_SERVER_PORT);
HttpClient httpClient = HttpClientBuilder.create()
.setRoutePlanner(new DefaultProxyRoutePlanner(proxy) {
@Override
public HttpHost determineProxy(HttpHost target, HttpRequest request, HttpContext context) throws HttpException {
return super.determineProxy(target, request, context);
}
})
.build();
restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory(httpClient));
}
}
After that, we build our customized RestTemplate:
之后,我们建立我们自定义的 RestTemplate。
RestTemplate restTemplate = new RestTemplateBuilder(new ProxyCustomizer()).build();
And finally, we use the RestTemplate to make requests that pass first through a proxy:
最后,我们使用RestTemplate来制作首先通过代理的请求。
ResponseEntity<String> responseEntity = restTemplate.getForEntity("http://httpbin.org/get", String.class);
assertThat(responseEntity.getStatusCode(), is(equalTo(HttpStatus.OK)));
5. Conclusion
5.总结
In this short tutorial, we’ve explored two different ways to send a request to a proxy using RestTemplate.
在这个简短的教程中,我们探讨了使用RestTemplate向代理发送请求的两种不同方式。
First, we learned how to send the request through a RestTemplate built using a SimpleClientHttpRequestFactory. Then we learned how to do the same using a RestTemplateCustomizer, this one being the recommended approach according to the documentation.
首先,我们学习了如何通过一个使用SimpleClientHttpRequestFactory构建的RestTemplate来发送请求。然后我们学习了如何使用RestTemplateCustomizer进行同样的操作,根据文档,这个是推荐的方法。
As always, the code samples are available over on GitHub.
像往常一样,代码样本可在GitHub上获得。