欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

基于springboot的RestTemplate、okhttp和HttpClient對比分析

 更新時間:2021年09月09日 16:56:09   作者:烏瑟爾  
這篇文章主要介紹了基于springboot的RestTemplate、okhttp和HttpClient對比分析,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

1、HttpClient:代碼復雜,還得操心資源回收等。代碼很復雜,冗余代碼多,不建議直接使用。

2、RestTemplate: 是 Spring 提供的用于訪問Rest服務的客戶端, RestTemplate 提供了多種便捷訪問遠程Http服務的方法,能夠大大提高客戶端的編寫效率。

引入jar包:

  <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

添加初始化配置(也可以不配,有默認的)--注意RestTemplate只有初始化配置,沒有什么連接池

package com.itunion.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;

@Configuration
public class ApiConfig {
    @Bean
    public RestTemplate restTemplate(ClientHttpRequestFactory factory) {
        return new RestTemplate(factory);
    }

    @Bean
    public ClientHttpRequestFactory simpleClientHttpRequestFactory() {
        SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();//默認的是JDK提供http連接,需要的話可以//通過setRequestFactory方法替換為例如Apache HttpComponents、Netty或//OkHttp等其它HTTP library。
        factory.setReadTimeout(5000);//單位為ms
        factory.setConnectTimeout(5000);//單位為ms
        return factory;
    }
}

  1)get請求(不帶參的即把參數(shù)取消即可)

// 1-getForObject()
User user1 = this.restTemplate.getForObject(uri, User.class);

// 2-getForEntity()
ResponseEntity<User> responseEntity1 = this.restTemplate.getForEntity(uri, User.class);
HttpStatus statusCode = responseEntity1.getStatusCode();
HttpHeaders header = responseEntity1.getHeaders();
User user2 = responseEntity1.getBody();
  
// 3-exchange()
RequestEntity requestEntity = RequestEntity.get(new URI(uri)).build();
ResponseEntity<User> responseEntity2 = this.restTemplate.exchange(requestEntity, User.class);
User user3 = responseEntity2.getBody();

    方式一:

Notice notice = restTemplate.getForObject("http://fantj.top/notice/list/{1}/{2}"
                , Notice.class,1,5);

    方式二:

Map<String,String> map = new HashMap();
        map.put("start","1");
        map.put("page","5");
        Notice notice = restTemplate.getForObject("http://fantj.top/notice/list/"
                , Notice.class,map);

  2)post請求:

// 1-postForObject()
User user1 = this.restTemplate.postForObject(uri, user, User.class);

// 2-postForEntity()
ResponseEntity<User> responseEntity1 = this.restTemplate.postForEntity(uri, user, User.class);

// 3-exchange()
RequestEntity<User> requestEntity = RequestEntity.post(new URI(uri)).body(user);
ResponseEntity<User> responseEntity2 = this.restTemplate.exchange(requestEntity, User.class);

  方式一:

String url = "http://demo/api/book/";
        HttpHeaders headers = new HttpHeaders();
        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
        headers.setContentType(type);
        String requestJson = "{...}";
        HttpEntity<String> entity = new HttpEntity<String>(requestJson,headers);
        String result = restTemplate.postForObject(url, entity, String.class);
        System.out.println(result);

  方式二:

@Test
public void rtPostObject(){
    RestTemplate restTemplate = new RestTemplate();
    String url = "http://47.xxx.xxx.96/register/checkEmail";
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
    MultiValueMap<String, String> map= new LinkedMultiValueMap<>();
    map.add("email", "844072586@qq.com");
 
    HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);
    ResponseEntity<String> response = restTemplate.postForEntity( url, request , String.class );
    System.out.println(response.getBody());
}

其它:還支持上傳和下載功能;

3、okhttp:OkHttp是一個高效的HTTP客戶端,允許所有同一個主機地址的請求共享同一個socket連接;連接池減少請求延時;透明的GZIP壓縮減少響應數(shù)據(jù)的大??;緩存響應內(nèi)容,避免一些完全重復的請求

當網(wǎng)絡出現(xiàn)問題的時候OkHttp依然堅守自己的職責,它會自動恢復一般的連接問題,如果你的服務有多個IP地址,當?shù)谝粋€IP請求失敗時,OkHttp會交替嘗試你配置的其他IP,OkHttp使用現(xiàn)代TLS技術(SNI, ALPN)初始化新的連接,當握手失敗時會回退到TLS 1.0。

1)使用:它的請求/響應 API 使用構造器模式builders來設計,它支持阻塞式的同步請求和帶回調(diào)的異步請求。

引入jar包:

<dependency>
    <groupId>com.squareup.okhttp3</groupId>
    <artifactId>okhttp</artifactId>
    <version>3.10.0</version>
</dependency>

2)配置文件:

import okhttp3.ConnectionPool;
import okhttp3.OkHttpClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.concurrent.TimeUnit;

@Configuration
public class OkHttpConfiguration {

    @Bean
    public OkHttpClient okHttpClient() {
        return new OkHttpClient.Builder()
                //.sslSocketFactory(sslSocketFactory(), x509TrustManager())
                .retryOnConnectionFailure(false)
                .connectionPool(pool())
                .connectTimeout(30, TimeUnit.SECONDS)
                .readTimeout(30, TimeUnit.SECONDS)
                .writeTimeout(30,TimeUnit.SECONDS)
                .build();
    }

    @Bean
    public X509TrustManager x509TrustManager() {
        return new X509TrustManager() {
            @Override
            public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
            }
            @Override
            public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
            }
            @Override
            public X509Certificate[] getAcceptedIssuers() {
                return new X509Certificate[0];
            }
        };
    }

    @Bean
    public SSLSocketFactory sslSocketFactory() {
        try {
            //信任任何鏈接
            SSLContext sslContext = SSLContext.getInstance("TLS");
            sslContext.init(null, new TrustManager[]{x509TrustManager()}, new SecureRandom());
            return sslContext.getSocketFactory();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (KeyManagementException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * Create a new connection pool with tuning parameters appropriate for a single-user application.
     * The tuning parameters in this pool are subject to change in future OkHttp releases. Currently
     */
    @Bean
    public ConnectionPool pool() {
        return new ConnectionPool(200, 5, TimeUnit.MINUTES);
    }
}

3)util工具:

import okhttp3.*;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.util.Iterator;
import java.util.Map;

public class OkHttpUtil{
    private static final Logger logger = LoggerFactory.getLogger(OkHttpUtil.class);
    
    private static OkHttpClient  okHttpClient;

    @Autowired
    public OkHttpUtil(OkHttpClient  okHttpClient) {
        OkHttpUtil.okHttpClient= okHttpClient;
    } 
    
    /**
     * get
     * @param url     請求的url
     * @param queries 請求的參數(shù),在瀏覽器?后面的數(shù)據(jù),沒有可以傳null
     * @return
     */
    public static  String get(String url, Map<String, String> queries) {
        String responseBody = "";
        StringBuffer sb = new StringBuffer(url);
        if (queries != null && queries.keySet().size() > 0) {
            boolean firstFlag = true;
            Iterator iterator = queries.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry entry = (Map.Entry<String, String>) iterator.next();
                if (firstFlag) {
                    sb.append("?" + entry.getKey() + "=" + entry.getValue());
                    firstFlag = false;
                } else {
                    sb.append("&" + entry.getKey() + "=" + entry.getValue());
                }
            }
        }
        Request request = new Request.Builder()
                .url(sb.toString())
                .build();
        Response response = null;
        try { 
            response = okHttpClient.newCall(request).execute();
            int status = response.code();
            if (response.isSuccessful()) {
                return response.body().string();
            }
        } catch (Exception e) {
            logger.error("okhttp3 put error >> ex = {}", ExceptionUtils.getStackTrace(e));
        } finally {
            if (response != null) {
                response.close();
            }
        }
        return responseBody;
    }

    /**
     * post
     *
     * @param url    請求的url
     * @param params post form 提交的參數(shù)
     * @return
     */
    public static String post(String url, Map<String, String> params) {
        String responseBody = "";
        FormBody.Builder builder = new FormBody.Builder();
        //添加參數(shù)
        if (params != null && params.keySet().size() > 0) {
            for (String key : params.keySet()) {
                builder.add(key, params.get(key));
            }
        }
        Request request = new Request.Builder()
                .url(url)
                .post(builder.build())
                .build();
        Response response = null;
        try { 
            response = okHttpClient.newCall(request).execute();
            int status = response.code();
            if (response.isSuccessful()) {
                return response.body().string();
            }
        } catch (Exception e) {
            logger.error("okhttp3 post error >> ex = {}", ExceptionUtils.getStackTrace(e));
        } finally {
            if (response != null) {
                response.close();
            }
        }
        return responseBody;
    }

    /**
     * get
     * @param url     請求的url
     * @param queries 請求的參數(shù),在瀏覽器?后面的數(shù)據(jù),沒有可以傳null
     * @return
     */
    public static String getForHeader(String url, Map<String, String> queries) {
        String responseBody = "";
        StringBuffer sb = new StringBuffer(url);
        if (queries != null && queries.keySet().size() > 0) {
            boolean firstFlag = true;
            Iterator iterator = queries.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry entry = (Map.Entry<String, String>) iterator.next();
                if (firstFlag) {
                    sb.append("?" + entry.getKey() + "=" + entry.getValue());
                    firstFlag = false;
                } else {
                    sb.append("&" + entry.getKey() + "=" + entry.getValue());
                }
            }
        }
        Request request = new Request.Builder()
                .addHeader("key", "value")
                .url(sb.toString())
                .build();
        Response response = null;
        try { 
            response = okHttpClient.newCall(request).execute();
            int status = response.code();
            if (response.isSuccessful()) {
                return response.body().string();
            }
        } catch (Exception e) {
            logger.error("okhttp3 put error >> ex = {}", ExceptionUtils.getStackTrace(e));
        } finally {
            if (response != null) {
                response.close();
            }
        }
        return responseBody;
    }

    /**
     * Post請求發(fā)送JSON數(shù)據(jù)....{"name":"zhangsan","pwd":"123456"}
     * 參數(shù)一:請求Url
     * 參數(shù)二:請求的JSON
     * 參數(shù)三:請求回調(diào)
     */
    public static String postJsonParams(String url, String jsonParams) {
        String responseBody = "";
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), jsonParams);
        Request request = new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();
        Response response = null;
        try { 
            response = okHttpClient.newCall(request).execute();
            int status = response.code();
            if (response.isSuccessful()) {
                return response.body().string();
            }
        } catch (Exception e) {
            logger.error("okhttp3 post error >> ex = {}", ExceptionUtils.getStackTrace(e));
        } finally {
            if (response != null) {
                response.close();
            }
        }
        return responseBody;
    }

    /**
     * Post請求發(fā)送xml數(shù)據(jù)....
     * 參數(shù)一:請求Url
     * 參數(shù)二:請求的xmlString
     * 參數(shù)三:請求回調(diào)
     */
    public static String postXmlParams(String url, String xml) {
        String responseBody = "";
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/xml; charset=utf-8"), xml);
        Request request = new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();
        Response response = null;
        try { 
            response = okHttpClient.newCall(request).execute();
            int status = response.code();
            if (response.isSuccessful()) {
                return response.body().string();
            }
        } catch (Exception e) {
            logger.error("okhttp3 post error >> ex = {}", ExceptionUtils.getStackTrace(e));
        } finally {
            if (response != null) {
                response.close();
            }
        }
        return responseBody;
    }
}

到此這篇關于基于springboot的RestTemplate、okhttp和HttpClient對比分析的文章就介紹到這了,更多相關springboot的RestTemplate、okhttp和HttpClient內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • 一文搞懂設計模式中的單例模式

    一文搞懂設計模式中的單例模式

    這篇文章主要介紹了一文搞懂設計模式中的單例模式,單例模式是最簡單的設計模式之一,屬于創(chuàng)建型模式,它提供了一種創(chuàng)建對象的方式,確保只有單個對象被創(chuàng)建,需要的朋友可以參考下
    2023-08-08
  • java內(nèi)存溢出示例(堆溢出、棧溢出)

    java內(nèi)存溢出示例(堆溢出、棧溢出)

    這篇文章主要介紹了java內(nèi)存溢出示例(堆溢出、棧溢出),需要的朋友可以參考下
    2014-04-04
  • Java Stream map, Collectors(toMap, toList, toSet, groupingBy, collectingAndThen)使用案例

    Java Stream map, Collectors(toMap, toLis

    這篇文章主要介紹了Java Stream map, Collectors(toMap, toList, toSet, groupingBy, collectingAndThen)使用案例,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-09-09
  • SpringBoot實現(xiàn)返回值數(shù)據(jù)脫敏的步驟詳解

    SpringBoot實現(xiàn)返回值數(shù)據(jù)脫敏的步驟詳解

    這篇文章主要給大家介紹一下SpringBoot實現(xiàn)返回值數(shù)據(jù)脫敏的步驟,文章通過代碼示例介紹的非常詳細,具有一定的參考價值,需要的朋友可以參考下
    2023-07-07
  • Hutool?Java工具類庫_ExcelUtil的使用

    Hutool?Java工具類庫_ExcelUtil的使用

    這篇文章主要介紹了Hutool?Java工具類庫_ExcelUtil的使用,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • Spring?Boot?整合持久層之Spring Data JPA

    Spring?Boot?整合持久層之Spring Data JPA

    在介紹Spring Data JPA的時候,我們首先認識下Hibernate。Hibernate是數(shù)據(jù)訪問解決技術的絕對霸主,使用O/R映射技術實現(xiàn)數(shù)據(jù)訪問,O/R映射即將領域模型類和數(shù)據(jù)庫的表進行映射,通過程序操作對象而實現(xiàn)表數(shù)據(jù)操作的能力,讓數(shù)據(jù)訪問操作無須關注數(shù)據(jù)庫相關的技術
    2022-08-08
  • 簡單了解JAVA構造方法

    簡單了解JAVA構造方法

    構造方法作用就是對類進行初始化。 如果你沒有定議任何構造方法的形式,程式會為你取一個不帶任何參數(shù)的構造函數(shù),那么你產(chǎn)生類的對像時只能用不帶參數(shù)的方法.下面小編和大家來一起學習一下吧
    2019-06-06
  • 帶你重新認識MyBatis的foreach

    帶你重新認識MyBatis的foreach

    這篇文章主要介紹了重新認識MyBatis的foreach,本文提出了一種簡化<foreach>寫法的設想,更重要的是通過解決空集時生成的SQL語法問題,更深刻地理解MyBatis的foreach的生成機制,需要的朋友可以參考下
    2022-11-11
  • 動態(tài)更改Spring定時任務Cron表達式的優(yōu)雅方案實例詳解

    動態(tài)更改Spring定時任務Cron表達式的優(yōu)雅方案實例詳解

    spring定時器非常強大,但是有時候我們需要在不需要重啟應用就可以動態(tài)的改變Cron表達式的值,下面這篇文章主要給大家介紹了關于動態(tài)更改Spring定時任務Cron表達式的優(yōu)雅方案,需要的朋友可以參考下
    2022-12-12
  • SpringBoot使用Apache POI庫讀取Excel文件的操作詳解

    SpringBoot使用Apache POI庫讀取Excel文件的操作詳解

    在日常開發(fā)中,我們經(jīng)常需要處理Excel文件中的數(shù)據(jù),無論是從數(shù)據(jù)庫導入數(shù)據(jù)、處理數(shù)據(jù)報表,還是批量生成數(shù)據(jù),都可能會遇到需要讀取和操作Excel文件的場景,本文將詳細介紹如何使用Java中的Apache POI庫來讀取Excel文件,需要的朋友可以參考下
    2025-01-01

最新評論