<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          SpringBoot系列之RestTemplate調(diào)https接口

          共 5413字,需瀏覽 11分鐘

           ·

          2020-12-05 23:46

          走過路過不要錯過

          點擊藍字關(guān)注我們


          業(yè)務(wù):本系統(tǒng)接口都是http的,調(diào)用第三方接口,因為做了安全性校驗,所以不能通過RestTemplate調(diào)用

          方法:重寫覆蓋SimpleClientHttpRequestFactory抽象類的prepareConnection方法

          package com.minstone.apprBase.common.utils.http.rest;

          import org.apache.http.conn.ssl.SSLContexts;
          import org.apache.http.conn.ssl.TrustStrategy;
          import org.springframework.http.client.SimpleClientHttpRequestFactory;

          import javax.net.ssl.HttpsURLConnection;
          import javax.net.ssl.SSLContext;
          import java.net.HttpURLConnection;
          import java.security.KeyStore;
          import java.security.cert.CertificateException;
          import java.security.cert.X509Certificate;

          /**
          * 兼容調(diào)Https接口
          * @Author mazq
          * @Date 2020/06/04 17:16
          * @Param
          * @return
          */

          public class HttpsClientRequestFactory extends SimpleClientHttpRequestFactory {

          @Override
          protected void prepareConnection(HttpURLConnection connection, String httpMethod) {
          try {
          if (!(connection instanceof HttpsURLConnection)) {// http協(xié)議
          //throw new RuntimeException("An instance of HttpsURLConnection is expected");
          super.prepareConnection(connection, httpMethod);
          }
          if (connection instanceof HttpsURLConnection) {// https協(xié)議,修改協(xié)議版本
          KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
          // 信任任何鏈接
          TrustStrategy anyTrustStrategy = new TrustStrategy() {
          @Override
          public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
          return true;
          }
          };
          SSLContext ctx = SSLContexts.custom().useTLS().loadTrustMaterial(trustStore, anyTrustStrategy).build();
          ((HttpsURLConnection) connection).setSSLSocketFactory(ctx.getSocketFactory());
          HttpsURLConnection httpsConnection = (HttpsURLConnection) connection;
          super.prepareConnection(httpsConnection, httpMethod);
          }
          } catch (Exception e) {
          e.printStackTrace();
          }
          }
          }

          關(guān)鍵代碼,new RestTemplate(new HttpsClientRequestFactory());,對應(yīng)工具類參考:

          package com.minstone.apprBase.common.utils.http.rest;

          import com.minstone.apprBase.common.utils.web.WebUtil;
          import org.springframework.http.*;
          import org.springframework.stereotype.Component;
          import org.springframework.web.client.RestTemplate;

          import java.util.Map;

          /**
          *

          * RestTemplate 遠程調(diào)用工具類
          *

          *
          *

          * @author mazq
          * 修改記錄
          * 修改后版本: 修改人:修改日期: 2020/06/01 11:38 修改內(nèi)容:
          *

          */

          @Component
          public class RestTemplateUtils {


          public static RestTemplate geTemplate(){
          return new RestTemplate(new HttpsClientRequestFactory());
          }

          /**
          * GET請求調(diào)用方式
          * @Author mazq
          * @Date 2020/06/01 13:47
          * @Param [url, responseType, uriVariables]
          * @return org.springframework.http.ResponseEntity
          */

          public static ResponseEntity getForEntity(String url, Class responseType, Map uriVariables) {
          return geTemplate().getForEntity(url, responseType, uriVariables);
          }

          /**
          * POST請求調(diào)用方式
          * @Author mazq
          * @Date 2020/06/01 13:47
          * @Param [url, headers, body, responseType]
          * @return org.springframework.http.ResponseEntity
          */

          public static ResponseEntity postForEntity(String url,HttpHeaders headers, Object requestBody , Class responseType ){

          HttpEntity requestEntity = new HttpEntity(requestBody, headers);
          return geTemplate().postForEntity(url, requestEntity, responseType);
          }

          /**
          * PUT請求調(diào)用方式
          * @Author mazq
          * @Date 2020/06/01 13:35
          * @param url 請求URL
          * @param headers 請求頭參數(shù)
          * @param requestBody 請求參數(shù)體
          * @param responseType 返回對象類型
          * @param uriVariables URL中的變量,與Map中的key對應(yīng)
          * @return ResponseEntity 響應(yīng)對象封裝類
          */

          public static ResponseEntity put(String url, HttpHeaders headers, Object requestBody, Class responseType, Map uriVariables) {
          HttpEntity requestEntity = new HttpEntity(requestBody, headers);
          return geTemplate().exchange(url, HttpMethod.PUT, requestEntity, responseType, uriVariables);
          }

          /**
          * DELETE請求調(diào)用方式
          * @Author mazq
          * @Date 2020/06/01 13:37
          * @param url 請求URL
          * @param headers 請求頭參數(shù)
          * @param requestBody 請求參數(shù)體
          * @param responseType 返回對象類型
          * @param uriVariables URL中的變量,按順序依次對應(yīng)
          * @return ResponseEntity 響應(yīng)對象封裝類
          */

          public static ResponseEntity delete(String url, HttpHeaders headers, Object requestBody, Class responseType, Object... uriVariables) {
          HttpEntity requestEntity = new HttpEntity(requestBody, headers);
          return geTemplate().exchange(url, HttpMethod.DELETE, requestEntity, responseType, uriVariables);
          }

          /**
          * 通用調(diào)用方式
          * @Author mazq
          * @Date 2020/06/01 13:37
          * @Param [url, method, requestEntity, responseType, uriVariables]
          * @return org.springframework.http.ResponseEntity
          */

          public static ResponseEntity exchange(String url, HttpMethod method, HttpEntity requestEntity, Class responseType, Map uriVariables) {
          return geTemplate().exchange(url, method, requestEntity, responseType, uriVariables);
          }
          }




          往期精彩推薦



          騰訊、阿里、滴滴后臺面試題匯總總結(jié) — (含答案)

          面試:史上最全多線程面試題 !

          最新阿里內(nèi)推Java后端面試題

          JVM難學?那是因為你沒認真看完這篇文章


          END


          關(guān)注作者微信公眾號 —《JAVA爛豬皮》


          了解更多java后端架構(gòu)知識以及最新面試寶典


          你點的每個好看,我都認真當成了


          看完本文記得給作者點贊+在看哦~~~大家的支持,是作者源源不斷出文的動力


          作者:SmileNicky

          出處:https://www.cnblogs.com/mzq123/p/13152885.html

          瀏覽 23
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                    <th id="afajh"><progress id="afajh"></progress></th>
                    一区二区在线视频 | 欧美成人无码片免费看A片秀色 | 乱系列视频在线观看 | 楪可怜Av一区二区三区 | 好逼天天操 |