RestTemplate配置和使用

编程入门 行业动态 更新时间:2024-10-22 21:34:42

<a href=https://www.elefans.com/category/jswz/34/1744999.html style=RestTemplate配置和使用"/>

RestTemplate配置和使用

在项目中,如果要调用第三方的http服务,就需要发起http请求,常用的请求方式:第一种,使用java原生发起http请求,这种方式不需要引入第三方库,但是连接不可复用,如果要实现连接复用,需要自己实现管理,相对来说比较麻烦;第二种就是使用第三方的库,比较常用的就是apache的httpclient和okhttp两个包,他们都对http请求进行了封装并且可以管理连接,对于重复使用的连接会有比较好的性能;第三种就是在springboot中使用RestTemplate进行请求,它是对http请求的封装,默认使用的java原生进行请求,也支持整合httpclient和okhttp库,提供很好的封装和扩展性。下面就介绍一下如何在项目中使用和相关配置:
要在项目中使用RestTemplate,首先需要定义一个Bean将它注入到系统中:

@Bean
public RestTemplate restTemplate() {return new RestTemplate();
}

这种方式注入的对象默认使用SimpleClientHttpRequestFactory工厂,它使用java原生方式发起http请求,性能并不好,还有一种注入方式是:

@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {return builder.build();
}

RestTemplateBuilder这种注入方式会根据系统中是否引入第三方的http库来灵活选择httpclient工厂,通过跟踪源码发现,目前支持httpclient和okhttp3两个库,如下图:

虽然通过上面的方式注入的RestTemplate会自动选择创建httpclient的工厂,但是该工厂相关参数都是默认配置,有时候并不能满足我们系统的需求,比如要设置读写超时时间,默认的配置中是永远不超时,这样如果请求的第三方系统响应慢会导致整个系统崩溃。
不加任何配置的RestTemplate内容如下:

要调整超时时间,就需要使用下面这种方式注入工厂,并配置相关参数:

@Bean
public ClientHttpRequestFactory simpleClientHttpRequestFactory() {SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();factory.setConnectTimeout(3000);factory.setReadTimeout(3000);return factory;
}@Bean
public RestTemplate restTemplate(ClientHttpRequestFactory factory) {return new RestTemplate(factory);
}

再次查看RestTemplate的内容:

通常情况下我们都不会使用java原生的方式发起http请求,因为每次发起http请求都要重新建立连接,这样会大大降低系统性能,在这个示例中我选择使用apache下的httpclient库扩展,首先需要在项目中引入相关依赖:

<dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpclient</artifactId><version>4.5.12</version>
</dependency>
<dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpmime</artifactId><version>4.5.12</version>
</dependency>
<dependency><groupId>org.apache.httpcomponents</groupId><artifactId>httpcore</artifactId><version>4.4.13</version>
</dependency>

注意包的版本要匹配,否则会有报错,接下来就是将httpclient注入到RestTemplate中,这里有两种方式,第一种是使用下面的配置

@Bean
public RestTemplate restTemplate(RestTemplateBuilder builder) {return builder.build();
}

它可以检测到系统中引入了httpclient依赖,会自动将原生的工厂类替换为apacheHttpClient的工厂:

但是这种方式使用的配置都是默认的,如连接池大小为20个,超时时间永不超时等。这种配置不能满足我们的需求,这时候就需要根据自己系统的要求调整配置参数了,下面的示例代码是我的一个简单配置:

import org.apache.http.HttpEntityEnclosingRequest;
import org.apache.http.HttpRequest;
import org.apache.http.NoHttpResponseException;
import org.apache.http.client.HttpRequestRetryHandler;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.conn.UnsupportedSchemeException;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;import javax.ssl.SSLException;
import java.io.InterruptedIOException;
import java.SocketException;
import java.SocketTimeoutException;
import java.UnknownHostException;
import java.util.concurrent.TimeUnit;@Configuration
public class RestTemplateConfig {@Beanpublic ClientHttpRequestFactory apacheHttpRequestFactory() {// 重试配置HttpRequestRetryHandler retryHandler = (e, count, context) -> {System.out.println("连接失败次数|" + count);e.printStackTrace();if (count >= 5) {       // 假设已经重试了5次,就放弃return false;}// 返回true需要重试if (e instanceof NoHttpResponseException            // 请求无响应就重试|| e instanceof ConnectTimeoutException     // 连接超时|| e instanceof SocketException             // 连接异常|| e instanceof SocketTimeoutException      // socket超时) {try {// 重试时间间隔:1sTimeUnit.MILLISECONDS.sleep(1000);} catch (InterruptedException ex) {e.printStackTrace();}return true;}// 返回false不需要重试if (e instanceof SSLException                       // SSL握手异常不要重试|| e instanceof InterruptedIOException      // 中断|| e instanceof UnknownHostException        // 目标server不可达|| e instanceof UnsupportedSchemeException  // 协议不支持) {return false;}HttpClientContext clientContext = HttpClientContext.adapt(context);HttpRequest request = clientContext.getRequest();// 假设请求是幂等的,就再次尝试return !(request instanceof HttpEntityEnclosingRequest);};Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.INSTANCE).register("https", SSLConnectionSocketFactory.getSocketFactory()).build();// 连接池配置PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(registry);// 最大连接数connManager.setMaxTotal(1000);// 每个路由最大连接数connManager.setDefaultMaxPerRoute(200);// 超时配置:都为5sRequestConfig reqConfig = RequestConfig.custom().setConnectionRequestTimeout(5000)      // 从连接池中获取连接超时时间.setConnectTimeout(5000)                // 连接建立超时时间,也就是三次握手完成时间.setSocketTimeout(5000)                 // 等待服务器响应超时时间.build();// 构建httpclientCloseableHttpClient client = HttpClients.custom().setConnectionManager(connManager).setDefaultRequestConfig(reqConfig).setRetryHandler(retryHandler).build();return new HttpComponentsClientHttpRequestFactory(client);}@Beanpublic RestTemplate restTemplate() {return new RestTemplate(apacheHttpRequestFactory());}
}

通过上面的配置就将自定义的连接参数注入到RestTemplate中了。在使用的位置,只需要引入restTemplate就可以正常使用了:

@Autowired
private RestTemplate restTemplate;

使用restTemplate发起http请求的方法封装为两种形式:一种是ForObject(),如getForObject()、postForObject(),这种只会返回数据;另一种是ForEntity(),如getForEntity()、postForEntity(),这种方式会返回数据和状态信息。
get请求比较简单,下面主要演示一下post请求中的提交表单数据和json数据的示例:

  1. 提交表单数据:
// 请求地址
String url = "http://localhost:8081/test/add";
// form表单数据
MultiValueMap<String, Object> data = new LinkedMultiValueMap<>();
data.add("name", "james");
data.add("age", 38);
// 请求头部
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
// 发起请求
ResponseEntity<String> result = restTemplate.postForEntity(url, new HttpEntity<>(data, headers), String.class);
  1. 提交json数据:
// 请求地址
String url = "http://localhost:8081/test/add";
// json数据
String data = "{\"name\":\"james\",\"age\":38}";
// 请求头部
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
// 发起请求
ResponseEntity<String> result = restTemplate.postForEntity(url, new HttpEntity<>(data, headers), String.class);

更多推荐

RestTemplate配置和使用

本文发布于:2023-11-15 21:44:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1606794.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:RestTemplate

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!