当前位置: 首页 > news >正文

廊坊做网站企业教程企业管理咨询公司前景

廊坊做网站企业教程,企业管理咨询公司前景,多多鱼网页模板,wordpress 什么值得买主题 最新v在项目中,如果要调用第三方的http服务,就需要发起http请求,常用的请求方式:第一种,使用java原生发起http请求,这种方式不需要引入第三方库,但是连接不可复用,如果要实现连接复用&…

在项目中,如果要调用第三方的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两个库,如下图:
判断factory类型
默认支持的两种factory

虽然通过上面的方式注入的RestTemplate会自动选择创建httpclient的工厂,但是该工厂相关参数都是默认配置,有时候并不能满足我们系统的需求,比如要设置读写超时时间,默认的配置中是永远不超时,这样如果请求的第三方系统响应慢会导致整个系统崩溃。
不加任何配置的RestTemplate内容如下:
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的内容:
设置参数后的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.net.ssl.SSLException;
import java.io.InterruptedIOException;
import java.net.SocketException;
import java.net.SocketTimeoutException;
import java.net.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);
http://www.yayakq.cn/news/435584/

相关文章:

  • 怎么做一个电商网站延边网站建设
  • 做音乐网站的条件知名企业网站规划书
  • 做一个公司网站大概要多少钱建个网络平台多少钱
  • 遵化建设局网站全网营销型网站建站专家
  • 网站制作设计收费大钟寺网站建设
  • 爱站工具的功能卖手机网站开发的必要性
  • 建设网站怎么做wordpress默认的h1标签放在哪里
  • 怎么做系统网站北京建设大学
  • 如何做产品网站网页设计深圳设计公司深圳设计公司排名
  • 网站 需求文档今天的新闻头条
  • 吉林省工程信息网官网网络优化大师手机版
  • 冀州网站建设公司个人建站软件公司
  • 网站建设 小程序制作网站建设与管理试卷
  • 天天网站南宁网络营销策划推广公司
  • 做网站需要注册商标多少类代理网页 在线
  • 网站备案查询不到说明啥物联网工程主要学什么
  • 免费的空间网站数码设计网站
  • 网页微信版可以加入腾讯会议吗seo难不难
  • 建营销网站数字尾巴 wordpress
  • 网站开发 之cookie品牌包包都有哪些牌子
  • 电子商务网站怎么备案个人网站建设公司地址
  • 宜昌网站开发网站建设消费调查问卷
  • 江苏荣邦建设有限公司网站音乐外链生成网站怎么做
  • 淘客网站开发流程网络营销服务管理
  • 新网站建设一般多少钱网站建设外文文献翻译
  • 长春网站营销手机网站设计字体多大
  • 网站定制论文1500字左右小程序代理合同范本
  • 深圳企业官网网站建设哪家好怎么把网站黑了
  • 龙岗网站建设公司怎么样百度号码认证平台取消标记
  • 响应页手机网站源码2022互联网+创新创业项目