【SpringBoot】 Java中如何封裝Http請求,以及JSON多層嵌套解析

前言

本文中的內容其實嚴格來講不算springboot裏面的特性,屬於JAVA基礎,只是我在項目中遇到了,特概括總結一下。spring

 

HTTP請求封裝

目前JAVA對於HTTP封裝主要有三種方式:json

  1. JAVA原生封裝後端

  2. HttpClient 3.X /HttpClient4.X springboot

  3. Spring RestTemplate服務器

http請求過程以下:數據結構

GET:
1、建立遠程鏈接
2、設置鏈接方式(get、post、put。。。)
3、設置鏈接超時時間
4、設置響應讀取時間
5、發起請求
6、獲取請求數據
7、關閉鏈接

POST:
1、建立遠程鏈接
2、設置鏈接方式(get、post、put。。。)
3、設置鏈接超時時間
4、設置響應讀取時間
五、當向遠程服務器傳送數據/寫數據時,須要設置爲true(setDoOutput)
6、當前向遠程服務讀取數據時,設置爲true,該參數無關緊要(setDoInput)
7、設置傳入參數的格式:(setRequestProperty)
8、設置鑑權信息:Authorization:(setRequestProperty)
9、設置參數
10、發起請求
11、獲取請求數據
十二、關閉鏈接

 

JAVA原生:

/** 
* http get請求
* @param httpUrl 連接
* @return 響應數據
*/
public
static String doGet(String httpUrl){ //連接 HttpURLConnection connection=null; InputStream is=null; BufferedReader br = null; StringBuffer result=new StringBuffer(); try { //建立鏈接 URL url=new URL(httpUrl); connection= (HttpURLConnection) url.openConnection(); //設置請求方式 connection.setRequestMethod("GET"); //設置鏈接超時時間 connection.setConnectTimeout(15000); //設置讀取超時時間 connection.setReadTimeout(15000); //開始鏈接 connection.connect(); //獲取響應數據 if(connection.getResponseCode()==200){ //獲取返回的數據 is=connection.getInputStream(); if(is!=null){ br=new BufferedReader(new InputStreamReader(is,"UTF-8")); String temp = null; while ((temp=br.readLine())!=null){ result.append(temp); } } } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { if(br!=null){ try { br.close(); } catch (IOException e) { e.printStackTrace(); } } if(is!=null){ try { is.close(); } catch (IOException e) { e.printStackTrace(); } } connection.disconnect();// 關閉遠程鏈接 } return result.toString(); } /** * post請求 * @param httpUrl 連接 * @param param 參數 * @return */ public static String doPost(String httpUrl, @Nullable String param) { StringBuffer result=new StringBuffer(); //鏈接 HttpURLConnection connection=null; OutputStream os=null; InputStream is=null; BufferedReader br=null; try { //建立鏈接對象 URL url=new URL(httpUrl); //建立鏈接 connection= (HttpURLConnection) url.openConnection(); //設置請求方法 connection.setRequestMethod("POST"); //設置鏈接超時時間 connection.setConnectTimeout(15000); //設置讀取超時時間 connection.setReadTimeout(15000); //設置是否可讀取 connection.setDoOutput(true); //設置響應是否可讀取 connection.setDoInput(true); //設置參數類型 connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); //拼裝參數 if(param!=null&&!param.equals("")){ //設置參數 os=connection.getOutputStream(); //拼裝參數 os.write(param.getBytes("UTF-8")); } //設置權限 //設置請求頭等 //開啓鏈接 //connection.connect(); //讀取響應 if(connection.getResponseCode()==200){ is=connection.getInputStream(); if(is!=null){ br=new BufferedReader(new InputStreamReader(is,"UTF-8")); String temp=null; if((temp=br.readLine())!=null){ result.append(temp); } } } //關閉鏈接 } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { if(br!=null){ try { br.close(); } catch (IOException e) { e.printStackTrace(); } } if(os!=null){ try { os.close(); } catch (IOException e) { e.printStackTrace(); } } if(is!=null){ try { is.close(); } catch (IOException e) { e.printStackTrace(); } } //關閉鏈接 connection.disconnect(); } return result.toString(); }

 

HttpCLient4.X 

httpclient有不少版本,目前最新的版本是4.X了,因此推薦使用4.x的方式進行封裝app

public static String doGet(String url) {
        CloseableHttpClient httpClient = null;
        CloseableHttpResponse response = null;
        String result = "";
        try {
            // 經過址默認配置建立一個httpClient實例
            httpClient = HttpClients.createDefault();
            // 建立httpGet遠程鏈接實例
            HttpGet httpGet = new HttpGet(url);
            // 設置請求頭信息,鑑權
            httpGet.setHeader("Authorization", "Bearer da3efcbf-0845-4fe3-8aba-ee040be542c0");
            // 設置配置請求參數
            RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(35000)// 鏈接主機服務超時時間
                    .setConnectionRequestTimeout(35000)// 請求超時時間
                    .setSocketTimeout(60000)// 數據讀取超時時間
                    .build();
            // 爲httpGet實例設置配置
            httpGet.setConfig(requestConfig);
            // 執行get請求獲得返回對象
            response = httpClient.execute(httpGet);
            // 經過返回對象獲取返回數據
            HttpEntity entity = response.getEntity();
            // 經過EntityUtils中的toString方法將結果轉換爲字符串
            result = EntityUtils.toString(entity);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 關閉資源
            if (null != response) {
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != httpClient) {
                try {
                    httpClient.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }

    public static String doPost(String url, Map<String, Object> paramMap) {
        CloseableHttpClient httpClient = null;
        CloseableHttpResponse httpResponse = null;
        String result = "";
        // 建立httpClient實例
        httpClient = HttpClients.createDefault();
        // 建立httpPost遠程鏈接實例
        HttpPost httpPost = new HttpPost(url);
        // 配置請求參數實例
        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(35000)// 設置鏈接主機服務超時時間
                .setConnectionRequestTimeout(35000)// 設置鏈接請求超時時間
                .setSocketTimeout(60000)// 設置讀取數據鏈接超時時間
                .build();
        // 爲httpPost實例設置配置
        httpPost.setConfig(requestConfig);
        // 設置請求頭
        httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded");
        // 封裝post請求參數
        if (null != paramMap && paramMap.size() > 0) {
            List<NameValuePair> nvps = new ArrayList<NameValuePair>();
            // 經過map集成entrySet方法獲取entity
            Set<Entry<String, Object>> entrySet = paramMap.entrySet();
            // 循環遍歷,獲取迭代器
            Iterator<Entry<String, Object>> iterator = entrySet.iterator();
            while (iterator.hasNext()) {
                Entry<String, Object> mapEntry = iterator.next();
                nvps.add(new BasicNameValuePair(mapEntry.getKey(), mapEntry.getValue().toString()));
            }

            // 爲httpPost設置封裝好的請求參數
            try {
                httpPost.setEntity(new UrlEncodedFormEntity(nvps, "UTF-8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        try {
            // httpClient對象執行post請求,並返回響應參數對象
            httpResponse = httpClient.execute(httpPost);
            // 從響應對象中獲取響應內容
            HttpEntity entity = httpResponse.getEntity();
            result = EntityUtils.toString(entity);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 關閉資源
            if (null != httpResponse) {
                try {
                    httpResponse.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != httpClient) {
                try {
                    httpClient.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }

 

Spring RestTemplate 封裝HTTP

spring自帶的一種封裝模式,方便簡潔,推薦使用post

public static String httpGet(String url){
        RestTemplate restTemplate=new RestTemplate();
        String result=restTemplate.exchange(url, HttpMethod.GET,null,String.class).getBody();
        return result;
    }

    public static String httpPost(String url,String name){
        RestTemplate restTemplate=new RestTemplate();
        return restTemplate.postForEntity(url,name,String.class).getBody();
    }

 

HTTPS請求的封裝

https請求只是在http請求的基礎上面添加了SSL驗證,經過下面的SLLClient 封裝便可,調用的時候:ui

httpClient = SSLClient.createSSLClientDefault();
public class SSLClient {
    public static CloseableHttpClient createSSLClientDefault(){
        try {
            SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
                //信任全部
                public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                    return true;
                }
            }).build();
            SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext);
            return HttpClients.custom().setSSLSocketFactory(sslsf).build();
        } catch (KeyManagementException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (KeyStoreException e) {
            e.printStackTrace();
        }
        return  HttpClients.createDefault();
    }

}

 

JSON多層嵌套

處理接口返回的時候,目前用的最多的方式是返回json格式,由於json格式比較成熟,包括取值,實例化等等。這裏介紹一種後端處理json返回多層嵌套的方式:url

1. 實例化json數據

2. 請求Http 接口獲取返回值

3. 實例化返回值並獲取你想要的key或者value,或者作驗證都行

優勢: 實例化了數據結構,想獲取任何數據都能使用實例化的方法。不用理會它有幾層,它的數據結構是什麼(List, array, string等等)

缺點: 須要返回的數據結構穩定,不能返回變更的數據結構, 另外實例化有必定的代碼量,比較繁瑣(可是有固定插件解決)

實操:

1. IDEA ---settings-----install plugins----GSON 插件

2. 新增一個entity實體類,在新建的實體類使用alt+S 快捷鍵打開GSON插件

3. 輸入你想要實例化的JSON數據,一直下一步便可完成實例化。

實例化截圖:

 

 

Service層調用實例化:

    //實例化的時候注意數據格式,List的話能夠循環獲取(下面的註釋部分);最終存儲到map裏面返回
     Gateway gatewaydata = JSON.parseObject(res, Gateway.class); Map<String,String> resMap=new HashMap<>(); String value=gatewaydata.getComponent().getMeasures().get(0).getPeriods().get(0).getValue(); String metric=gatewaydata.getComponent().getMeasures().get(0).getMetric(); //獲取各個區的名稱 // List<Country> clist=state.getData().get(0).getCity().get(0).getCounty(); // for(Country c:clist){ // System.out.println("cname:"+c.getName()); // } System.out.println(value+metric); resMap.put("metric",metric); resMap.put("value",value);
相關文章
相關標籤/搜索