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

Java實(shí)現(xiàn)Http請(qǐng)求的常用方法詳解

 更新時(shí)間:2025年02月12日 11:21:02   作者:Alex_81D  
在實(shí)際開(kāi)發(fā)過(guò)程中,我們經(jīng)常需要調(diào)用對(duì)方提供的接口或測(cè)試自己寫(xiě)的接口是否合適,本文為大家整理了Java調(diào)用第三方http接口幾種方式,需要的可以參考下

背景

在實(shí)際開(kāi)發(fā)過(guò)程中,我們經(jīng)常需要調(diào)用對(duì)方提供的接口或測(cè)試自己寫(xiě)的接口是否合適。很多項(xiàng)目都會(huì)封裝規(guī)定好本身項(xiàng)目的接口規(guī)范,所以大多數(shù)需要去調(diào)用對(duì)方提供的接口或第三方接口(短信、天氣等)。若是普通java工程推薦使用OkHttpClient,若是spring工程推薦使用RestTemplate。,若是springcloud微服務(wù)可以用openFeign或Dubbo

在Java項(xiàng)目中調(diào)用第三方接口的方式有:

1、通過(guò)JDK網(wǎng)絡(luò)類Java.net.HttpURLConnection;

2、通過(guò)common封裝好的HttpClient;

3、通過(guò)Apache封裝好的CloseableHttpClient;

4、通過(guò)SpringBoot-RestTemplate;

5、通過(guò)Feign服務(wù)調(diào)用

一、Java調(diào)用第三方http接口幾種方式總結(jié)

1. 通過(guò)JDK網(wǎng)絡(luò)類Java.net.HttpURLConnection

比較原始的一種調(diào)用做法,這里把get請(qǐng)求和post請(qǐng)求都統(tǒng)一放在一個(gè)方法里面。

實(shí)現(xiàn)過(guò)程:

GET:

  • 1、創(chuàng)建遠(yuǎn)程連接
  • 2、設(shè)置連接方式(get、post、put。。。)
  • 3、設(shè)置連接超時(shí)時(shí)間
  • 4、設(shè)置響應(yīng)讀取時(shí)間
  • 5、發(fā)起請(qǐng)求
  • 6、獲取請(qǐng)求數(shù)據(jù)
  • 7、關(guān)閉連接

POST:

  • 1、創(chuàng)建遠(yuǎn)程連接
  • 2、設(shè)置連接方式(get、post、put。。。)
  • 3、設(shè)置連接超時(shí)時(shí)間
  • 4、設(shè)置響應(yīng)讀取時(shí)間
  • 5、當(dāng)向遠(yuǎn)程服務(wù)器傳送數(shù)據(jù)/寫(xiě)數(shù)據(jù)時(shí),需要設(shè)置為true(setDoOutput)
  • 6、當(dāng)前向遠(yuǎn)程服務(wù)讀取數(shù)據(jù)時(shí),設(shè)置為true,該參數(shù)可有可無(wú)(setDoInput)
  • 7、設(shè)置傳入?yún)?shù)的格式:(setRequestProperty)
  • 8、設(shè)置鑒權(quán)信息:Authorization:(setRequestProperty)
  • 9、設(shè)置參數(shù)
  • 10、發(fā)起請(qǐng)求
  • 11、獲取請(qǐng)求數(shù)據(jù)
  • 12、關(guān)閉連接

直接上代碼: 

import org.springframework.lang.Nullable;
 
import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
 
 
public class HttpURLConnectionUtil {
 
    /**
     * Http get請(qǐng)求
     * @param httpUrl 連接
     * @return 響應(yīng)數(shù)據(jù)
     */
    public static String doGet(String httpUrl){
        //鏈接
        HttpURLConnection connection = null;
        InputStream is = null;
        BufferedReader br = null;
        StringBuffer result = new StringBuffer();
        try {
            //創(chuàng)建連接
            URL url = new URL(httpUrl);
            connection = (HttpURLConnection) url.openConnection();
            //設(shè)置請(qǐng)求方式
            connection.setRequestMethod("GET");
            //設(shè)置連接超時(shí)時(shí)間
            connection.setReadTimeout(15000);
            //開(kāi)始連接
            connection.connect();
            //獲取響應(yīng)數(shù)據(jù)
            if (connection.getResponseCode() == 200) {
                //獲取返回的數(shù)據(jù)
                is = connection.getInputStream();
                if (null != is) {
                    br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                    String temp = null;
                    while (null != (temp = br.readLine())) {
                        result.append(temp);
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != br) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != is) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            //關(guān)閉遠(yuǎn)程連接
            connection.disconnect();
        }
        return result.toString();
    }
 
    /**
     * Http post請(qǐng)求
     * @param httpUrl 連接
     * @param param 參數(shù)
     * @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 {
            //創(chuàng)建連接對(duì)象
            URL url = new URL(httpUrl);
            //創(chuàng)建連接
            connection = (HttpURLConnection) url.openConnection();
            //設(shè)置請(qǐng)求方法
            connection.setRequestMethod("POST");
            //設(shè)置連接超時(shí)時(shí)間
            connection.setConnectTimeout(15000);
            //設(shè)置讀取超時(shí)時(shí)間
            connection.setReadTimeout(15000);
            //DoOutput設(shè)置是否向httpUrlConnection輸出,DoInput設(shè)置是否從httpUrlConnection讀入,此外發(fā)送post請(qǐng)求必須設(shè)置這兩個(gè)
            //設(shè)置是否可讀取
            connection.setDoOutput(true);
            connection.setDoInput(true);
            //設(shè)置通用的請(qǐng)求屬性
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
            connection.setRequestProperty("Content-Type", "application/json;charset=utf-8");
 
            //拼裝參數(shù)
            if (null != param && param.equals("")) {
                //設(shè)置參數(shù)
                os = connection.getOutputStream();
                //拼裝參數(shù)
                os.write(param.getBytes("UTF-8"));
            }
            //設(shè)置權(quán)限
            //設(shè)置請(qǐng)求頭等
            //開(kāi)啟連接
            //connection.connect();
            //讀取響應(yīng)
            if (connection.getResponseCode() == 200) {
                is = connection.getInputStream();
                if (null != is) {
                    br = new BufferedReader(new InputStreamReader(is, "GBK"));
                    String temp = null;
                    while (null != (temp = br.readLine())) {
                        result.append(temp);
                        result.append("\r\n");
                    }
                }
            }
 
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //關(guān)閉連接
            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();
                }
            }
            //關(guān)閉連接
            connection.disconnect();
        }
        return result.toString();
    }
 
    public static void main(String[] args) {
        String message = doPost("https://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=13026194071", "");
        System.out.println(message);
    }
}

運(yùn)行結(jié)果: 

2. 通過(guò)apache common封裝好的HttpClient

httpClient的get或post請(qǐng)求方式步驟:

1.生成一個(gè)HttpClient對(duì)象并設(shè)置相應(yīng)的參數(shù);

2.生成一個(gè)GetMethod對(duì)象或PostMethod并設(shè)置響應(yīng)的參數(shù);

3.用HttpClient生成的對(duì)象來(lái)執(zhí)行GetMethod生成的Get方法;

4.處理響應(yīng)狀態(tài)碼;

5.若響應(yīng)正常,處理HTTP響應(yīng)內(nèi)容;

6.釋放連接。

<!--HttpClient-->
<dependency>
    <groupId>commons-httpclient</groupId>
    <artifactId>commons-httpclient</artifactId>
    <version>3.1</version>
</dependency>
 
<!--fastjson-->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.32</version>
</dependency>
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
 
import java.io.IOException;
 
/**
 * @Author allen
 * @Description TODO
 * @Date 2023-10-18 18:25
 * @Version 1.0
 */
public class HttpClientUtil {
    /**
     * httpClient的get請(qǐng)求方式
     * 使用GetMethod來(lái)訪問(wèn)一個(gè)URL對(duì)應(yīng)的網(wǎng)頁(yè)實(shí)現(xiàn)步驟:
     * 1.生成一個(gè)HttpClient對(duì)象并設(shè)置相應(yīng)的參數(shù);
     * 2.生成一個(gè)GetMethod對(duì)象并設(shè)置響應(yīng)的參數(shù);
     * 3.用HttpClient生成的對(duì)象來(lái)執(zhí)行GetMethod生成的Get方法;
     * 4.處理響應(yīng)狀態(tài)碼;
     * 5.若響應(yīng)正常,處理HTTP響應(yīng)內(nèi)容;
     * 6.釋放連接。
     *
     * @param url
     * @param charset
     * @return
     */
    public static String doGet(String url, String charset) {
        //1.生成HttpClient對(duì)象并設(shè)置參數(shù)
        HttpClient httpClient = new HttpClient();
        //設(shè)置Http連接超時(shí)為5秒
        httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
        //2.生成GetMethod對(duì)象并設(shè)置參數(shù)
        GetMethod getMethod = new GetMethod(url);
        //設(shè)置get請(qǐng)求超時(shí)為5秒
        getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 5000);
        //設(shè)置請(qǐng)求重試處理,用的是默認(rèn)的重試處理:請(qǐng)求三次
        getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());
        String response = "";
        //3.執(zhí)行HTTP GET 請(qǐng)求
        try {
            int statusCode = httpClient.executeMethod(getMethod);
            //4.判斷訪問(wèn)的狀態(tài)碼
            if (statusCode != HttpStatus.SC_OK) {
                System.err.println("請(qǐng)求出錯(cuò):" + getMethod.getStatusLine());
            }
            //5.處理HTTP響應(yīng)內(nèi)容
            //HTTP響應(yīng)頭部信息,這里簡(jiǎn)單打印
            Header[] headers = getMethod.getResponseHeaders();
            for (Header h : headers) {
                System.out.println(h.getName() + "---------------" + h.getValue());
            }
            //讀取HTTP響應(yīng)內(nèi)容,這里簡(jiǎn)單打印網(wǎng)頁(yè)內(nèi)容
            //讀取為字節(jié)數(shù)組
            byte[] responseBody = getMethod.getResponseBody();
            response = new String(responseBody, charset);
            System.out.println("-----------response:" + response);
            //讀取為InputStream,在網(wǎng)頁(yè)內(nèi)容數(shù)據(jù)量大時(shí)候推薦使用
            //InputStream response = getMethod.getResponseBodyAsStream();
        } catch (HttpException e) {
            //發(fā)生致命的異常,可能是協(xié)議不對(duì)或者返回的內(nèi)容有問(wèn)題
            System.out.println("請(qǐng)檢查輸入的URL!");
            e.printStackTrace();
        } catch (IOException e) {
            //發(fā)生網(wǎng)絡(luò)異常
            System.out.println("發(fā)生網(wǎng)絡(luò)異常!");
        } finally {
            //6.釋放連接
            getMethod.releaseConnection();
        }
        return response;
    }
 
    /**
     * post請(qǐng)求
     *
     * @param url
     * @param json
     * @return
     */
    public static String doPost(String url, JSONObject json) {
        HttpClient httpClient = new HttpClient();
        PostMethod postMethod = new PostMethod(url);
 
        postMethod.addRequestHeader("accept", "*/*");
        postMethod.addRequestHeader("connection", "Keep-Alive");
        //設(shè)置json格式傳送
        postMethod.addRequestHeader("Content-Type", "application/json;charset=GBK");
        //必須設(shè)置下面這個(gè)Header
        postMethod.addRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36");
        //添加請(qǐng)求參數(shù)
        postMethod.addParameter("commentId", json.getString("commentId"));
 
        String res = "";
        try {
            int code = httpClient.executeMethod(postMethod);
            if (code == 200) {
                res = postMethod.getResponseBodyAsString();
                System.out.println(res);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return res;
    }
 
    public static void main(String[] args) {
        System.out.println(doGet("http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=13026194071", "GBK"));
        System.out.println("-----------分割線------------");
        System.out.println("-----------分割線------------");
        System.out.println("-----------分割線------------");
 
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("commentId", "13026194071");
        System.out.println(doPost("http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=13026194071", jsonObject));
    }
}

3. 通過(guò)Apache封裝好的CloseableHttpClient

CloseableHttpClient是在HttpClient的基礎(chǔ)上修改更新而來(lái)的,這里還涉及到請(qǐng)求頭token的設(shè)置(請(qǐng)求驗(yàn)證),利用fastjson轉(zhuǎn)換請(qǐng)求或返回結(jié)果字符串為json格式,當(dāng)然上面兩種方式也是可以設(shè)置請(qǐng)求頭token、json的,這里只在下面說(shuō)明。

導(dǎo)入如下jar包:

<!--CloseableHttpClient-->
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.2</version>
</dependency>
 
<!--fastjson-->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.32</version>
</dependency>
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
 
import java.io.IOException;
 
/**
 * @Author allen
 * @Description TODO
 * @Date 2023-10-18 18:31
 * @Version 1.0
 */
public class CloseableHttpClientUtil {
 
    private static String tokenString = "";
    private static String AUTH_TOKEN_EXPIRED = "AUTH_TOKEN_EXPIRED";
    private static CloseableHttpClient httpClient = null;
 
    /**
     * 以get方式調(diào)用第三方接口
     * @param url
     * @param token
     * @return
     */
    public static String doGet(String url, String token) {
        //創(chuàng)建HttpClient對(duì)象
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        HttpGet httpGet = new HttpGet(url);
        if (null != tokenString && !tokenString.equals("")) {
            tokenString = getToken();
        }
        //api_gateway_auth_token自定義header頭,用于token驗(yàn)證使用
        httpGet.addHeader("api_gateway_auth_token",tokenString);
        httpGet.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36");
        try {
            HttpResponse response = httpClient.execute(httpGet);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                //返回json格式
                String res = EntityUtils.toString(response.getEntity());
                return res;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
 
    /**
     * 以post方式調(diào)用第三方接口
     * @param url
     * @param json
     * @return
     */
    public static String doPost(String url, JSONObject json) {
        if (null == httpClient) {
            httpClient = HttpClientBuilder.create().build();
        }
        HttpPost httpPost = new HttpPost(url);
        if (null != tokenString && tokenString.equals("")) {
            tokenString = getToken();
        }
        //api_gateway_auth_token自定義header頭,用于token驗(yàn)證使用
        httpPost.addHeader("api_gateway_auth_token", tokenString);
        httpPost.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36");
        try {
            StringEntity se = new StringEntity(json.toString());
            se.setContentEncoding("UTF-8");
            //發(fā)送json數(shù)據(jù)需要設(shè)置contentType
            se.setContentType("application/x-www-form-urlencoded");
            //設(shè)置請(qǐng)求參數(shù)
            httpPost.setEntity(se);
            HttpResponse response = httpClient.execute(httpPost);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                //返回json格式
                String res = EntityUtils.toString(response.getEntity());
                return res;
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (httpClient != null){
                try {
                    httpClient.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }
 
    /**
     * 獲取第三方接口的token
     */
    public static String getToken() {
        String token = "";
        JSONObject object = new JSONObject();
        object.put("appid", "appid");
        object.put("secretkey", "secretkey");
        if (null == httpClient) {
            httpClient = HttpClientBuilder.create().build();
        }
        HttpPost httpPost = new HttpPost("http://localhost/login");
        httpPost.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36");
        try {
            StringEntity se = new StringEntity(object.toString());
            se.setContentEncoding("UTF-8");
            //發(fā)送json數(shù)據(jù)需要設(shè)置contentType
            se.setContentType("application/x-www-form-urlencoded");
            //設(shè)置請(qǐng)求參數(shù)
            httpPost.setEntity(se);
            HttpResponse response = httpClient.execute(httpPost);
            //這里可以把返回的結(jié)果按照自定義的返回?cái)?shù)據(jù)結(jié)果,把string轉(zhuǎn)換成自定義類
            //ResultTokenBO result = JSONObject.parseObject(response, ResultTokenBO.class);
            //把response轉(zhuǎn)為jsonObject
            JSONObject result = (JSONObject) JSONObject.parseObject(String.valueOf(response));
            if (result.containsKey("token")) {
                token = result.getString("token");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return token;
    }
 
    /**
     * 測(cè)試
     */
    public static void test(String telephone) {
 
        JSONObject object = new JSONObject();
        object.put("telephone", telephone);
 
        //首先獲取token
        tokenString = getToken();
        String response = doPost("http://localhost/searchUrl", object);
        //如果返回的結(jié)果是list形式的,需要使用JSONObject.parseArray轉(zhuǎn)換
        //List<Result> list = JSONObject.parseArray(response, Result.class);
        System.out.println(response);
    }
 
    public static void main(String[] args) {
        test("12345678910");
    }
}

4. 通過(guò)SpringBoot-RestTemplate

springBoot-RestTemple是上面三種方式的集大成者,代碼編寫(xiě)更加簡(jiǎn)單,目前可以采用的調(diào)用第三方接口有:

  • delete() 在特定的URL上對(duì)資源執(zhí)行HTTP DELETE操作
  • exchange() 在URL上執(zhí)行特定的HTTP方法,返回包含對(duì)象的ResponseEntity,這個(gè)對(duì)象是從響應(yīng)體中映射得到的
  • execute() 在URL上執(zhí)行特定的HTTP方法,返回一個(gè)從響應(yīng)體映射得到的對(duì)象
  • getForEntity() 發(fā)送一個(gè)HTTP GET請(qǐng)求,返回的ResponseEntity包含了響應(yīng)體所映射成的對(duì)象
  • getForObject() 發(fā)送一個(gè)HTTP GET請(qǐng)求,返回的請(qǐng)求體將映射為一個(gè)對(duì)象
  • postForEntity() POST 數(shù)據(jù)到一個(gè)URL,返回包含一個(gè)對(duì)象的ResponseEntity,這個(gè)對(duì)象是從響應(yīng)體中映射得到的
  • postForObject() POST 數(shù)據(jù)到一個(gè)URL,返回根據(jù)響應(yīng)體匹配形成的對(duì)象
  • headForHeaders() 發(fā)送HTTP HEAD請(qǐng)求,返回包含特定資源URL的HTTP頭
  • optionsForAllow() 發(fā)送HTTP OPTIONS請(qǐng)求,返回對(duì)特定URL的Allow頭信息
  • postForLocation() POST 數(shù)據(jù)到一個(gè)URL,返回新創(chuàng)建資源的URL
  • put() PUT 資源到特定的URL

首先導(dǎo)入springboot的web包

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.4.RELEASE</version>
    </parent>
 
    <dependencies>
        <!--CloseableHttpClient-->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.2</version>
        </dependency>
 
        <!--spring restTemplate-->
        <!-- @ConfigurationProperties annotation processing (metadata for IDEs)
                生成spring-configuration-metadata.json類,需要引入此類-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-tomcat</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jetty</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

在啟動(dòng)類同包下創(chuàng)建RestTemplateConfig.java類

import java.io.IOException;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.ResponseErrorHandler;
import org.springframework.web.client.RestTemplate;
 
import com.fasterxml.jackson.databind.ObjectMapper;
 
/**
 * @Author allen
 * @Description TODO
 * @Date 2023-10-18 18:41
 * @Version 1.0
 */
@Configuration
public class RestTemplateConfig {
 
    @Autowired
    private ObjectMapper objectMapper;
 
    @Bean
    public RestTemplate restTemplate() {
 
        RestTemplate template = new RestTemplate();
 
        // 設(shè)置 http 超時(shí)時(shí)間
        HttpComponentsClientHttpRequestFactory httpRequestFactory = new HttpComponentsClientHttpRequestFactory();
        // 獲取鏈接超時(shí)時(shí)間
        httpRequestFactory.setConnectionRequestTimeout(6000);
        // 鏈接超時(shí)時(shí)間 6S
        httpRequestFactory.setConnectTimeout(6000);
        // 讀取超時(shí) 6S
        httpRequestFactory.setReadTimeout(6000);
        template.setRequestFactory(httpRequestFactory);
 
//        template.getInterceptors().add((request,body,execution) -> {
//            final RequestAttributes requestAttributes=RequestContextHolder.getRequestAttributes();
//            if(requestAttributes!=null&&requestAttributes instanceof ServletRequestAttributes){
//                final HttpServletRequest servletRequest = ((ServletRequestAttributes) requestAttributes).getRequest();
//                request.getHeaders().add("token", servletRequest.getHeader("token"));
//                request.getHeaders().add("location", servletRequest.getHeader("location"));
//                request.getHeaders().add("appId", servletRequest.getHeader("appId"));
//            }
//            return execution.execute(request, body);
//        });
 
 
        template.setErrorHandler(new ResponseErrorHandler() {
            @Override
            public boolean hasError(ClientHttpResponse response) throws IOException {
                System.out.println("RestTemplateConfig------------------hasError");
                return response.getRawStatusCode() >= 400;
            }
 
            @Override
            public void handleError(ClientHttpResponse response) throws IOException {
                System.out.println("RestTemplateConfig----------handleError--------異常");
                String result = objectMapper.readValue(response.getBody(), String.class);
                throw new RuntimeException(result);
            }
        });
 
 
//        template.getMessageConverters().set(1, new StringHttpMessageConverter(StandardCharsets.UTF_8));
        return template;
    }
 
}

然后在Service類(RestTemplateToInterface )中注入使用

具體代碼如下:

import com.alibaba.fastjson.JSONObject;
import com.swordfall.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
 
 
@Service
public class RestTemplateToInterface {
 
    @Autowired
    private RestTemplate restTemplate;
 
    /**
     * 以get方式請(qǐng)求第三方http接口 getForEntity
     * @param url
     * @return
     */
    public User doGetWith1(String url){
        ResponseEntity<User> responseEntity = restTemplate.getForEntity(url, User.class);
        User user = responseEntity.getBody();
        return user;
    }
 
    /**
     * 以get方式請(qǐng)求第三方http接口 getForObject
     * 返回值返回的是響應(yīng)體,省去了我們?cè)偃etBody()
     * @param url
     * @return
     */
    public User doGetWith2(String url){
        User user  = restTemplate.getForObject(url, User.class);
        return user;
    }
 
    /**
     * 以post方式請(qǐng)求第三方http接口 postForEntity
     * @param url
     * @return
     */
    public String doPostWith1(String url){
        User user = new User("小白", 20);
        ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, user, String.class);
        String body = responseEntity.getBody();
        return body;
    }
 
    /**
     * 以post方式請(qǐng)求第三方http接口 postForEntity
     * @param url
     * @return
     */
    public String doPostWith2(String url){
        User user = new User("小白", 20);
        String body = restTemplate.postForObject(url, user, String.class);
        return body;
    }
 
    /**
     * exchange
     * @return
     */
    public String doExchange(String url, Integer age, String name){
        //header參數(shù)
        HttpHeaders headers = new HttpHeaders();
        String token = "asdfaf2322";
        headers.add("authorization", token);
        headers.setContentType(MediaType.APPLICATION_JSON);
 
        //放入body中的json參數(shù)
        JSONObject obj = new JSONObject();
        obj.put("age", age);
        obj.put("name", name);
 
        //組裝
        HttpEntity<JSONObject> request = new HttpEntity<>(obj, headers);
        ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.POST, request, String.class);
        String body = responseEntity.getBody();
        return body;
    }
}

5 通過(guò)okhttp

應(yīng)大家的響應(yīng),okhttp 現(xiàn)在也是蠻流行的,基于手機(jī)端很火,這里分享一下OkHttpClient客戶端,業(yè)務(wù)代碼get、post請(qǐng)求直接調(diào)用就好哈。

pom文件引入依賴包

<dependency>
    <groupId>com.squareup.okhttp3</groupId>
    <artifactId>okhttp</artifactId>
    <version>3.10.0</version>
</dependency>
@Slf4j
public class OkHttpClient {
    private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
 
    private volatile static okhttp3.OkHttpClient client;
 
    private static final int MAX_IDLE_CONNECTION = Integer
            .parseInt(ConfigManager.get("httpclient.max_idle_connection"));
 
    private static final long KEEP_ALIVE_DURATION = Long
            .parseLong(ConfigManager.get("httpclient.keep_alive_duration"));
 
    private static final long CONNECT_TIMEOUT = Long.parseLong(ConfigManager.get("httpclient.connectTimeout"));
 
    private static final long READ_TIMEOUT = Long.parseLong(ConfigManager.get("httpclient. "));
 
    /**
     * 單例模式(雙重檢查模式) 獲取類實(shí)例
     *
     * @return client
     */
    private static okhttp3.OkHttpClient getInstance() {
        if (client == null) {
            synchronized (okhttp3.OkHttpClient.class) {
                if (client == null) {
                    client = new okhttp3.OkHttpClient.Builder()
                            .connectTimeout(CONNECT_TIMEOUT, TimeUnit.SECONDS)
                            .readTimeout(READ_TIMEOUT, TimeUnit.SECONDS)
                            .connectionPool(new ConnectionPool(MAX_IDLE_CONNECTION, KEEP_ALIVE_DURATION,
                                    TimeUnit.MINUTES))
                            .build();
                }
            }
        }
        return client;
    }
 
    public static String syncPost(String url, String json) throws IOException {
        RequestBody body = RequestBody.create(JSON, json);
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();
        try {
            Response response = OkHttpClient.getInstance().newCall(request).execute();
            if (response.isSuccessful()) {
                String result = response.body().string();
                log.info("syncPost response = {}, responseBody= {}", response, result);
                return result;
            }
            String result = response.body().string();
            log.info("syncPost response = {}, responseBody= {}", response, result);
            throw new IOException("三方接口返回http狀態(tài)碼為" + response.code());
        } catch (Exception e) {
            log.error("syncPost() url:{} have a ecxeption {}", url, e);
            throw new RuntimeException("syncPost() have a ecxeption {}" + e.getMessage());
        }
    }
 
    public static String syncGet(String url, Map<String, Object> headParamsMap) throws IOException {
        Request request;
        final Request.Builder builder = new Request.Builder().url(url);
        try {
            if (!CollectionUtils.isEmpty(headParamsMap)) {
                final Iterator<Map.Entry<String, Object>> iterator = headParamsMap.entrySet()
                        .iterator();
                while (iterator.hasNext()) {
                    final Map.Entry<String, Object> entry = iterator.next();
                    builder.addHeader(entry.getKey(), (String) entry.getValue());
                }
            }
            request = builder.build();
            Response response = OkHttpClient.getInstance().newCall(request).execute();
            String result = response.body().string();
            log.info("syncGet response = {},responseBody= {}", response, result);
            if (!response.isSuccessful()) {
                throw new IOException("三方接口返回http狀態(tài)碼為" + response.code());
            }
            return result;
        } catch (Exception e) {
            log.error("remote interface url:{} have a ecxeption {}", url, e);
            throw new RuntimeException("三方接口返回異常");
        }
    }
 
}

參考:java實(shí)現(xiàn)調(diào)用http請(qǐng)求的五種常見(jiàn)方式

6 通過(guò)openFeign

可以參考博主的這篇文章

Feign實(shí)戰(zhàn)-Springboot集成OpenFeign Demo以及參數(shù)詳解

二、方案擴(kuò)展

我用的這個(gè):

HttpUtils如下:

import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpStatus;
import org.apache.http.HttpVersion;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.UnsupportedCharsetException;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
 
/**
 * Http tool class
 */
public class HttpUtils {
 
    public static String INTERFACE_CALL_ERROR = "接口調(diào)用錯(cuò)誤";
    private static Logger logger = LoggerUtil.getLogger();
 
    /**
     * "post" request to transfer "json" data
     *
     * @param url
     * @param json
     * @param timeOutMS (Millisecond)
     * @return
     */
    public static String doPostParmaMap(String url, Map<?, ?> json, Integer timeOutMS) {
        if (null == json) {
            return doPost(url, null, timeOutMS);
        }
        String formatJson = JsonUtils.toFormatJsonNoException(json);
        return doPost(url, formatJson, timeOutMS);
    }
 
    /**
     * "post" request to transfer "json" data
     *
     * @param url
     * @param json
     * @param timeOutMS (Millisecond)
     * @return
     */
    public static String doPost(String url, String json, Integer timeOutMS) {
        String result = "";
 
        // Create an "httpclient" object
        CloseableHttpClient httpClient = null;
        // Create a "post" mode request object
        HttpPost httpPost = null;
        try {
            // Create an "httpclient" object
            httpClient = HttpClients.createDefault();
            // Create a "post" mode request object
            httpPost = new HttpPost(url);
 
            logger.debug("afferent json param:" + json);
            // Set parameters to the request object
 
            if (StringUtils.isNotBlank(json)) {
                StringEntity stringEntity = new StringEntity(json.toString(), ContentType.APPLICATION_JSON);
                stringEntity.setContentEncoding("utf-8");
                httpPost.setEntity(stringEntity);
            }
            httpPost.setProtocolVersion(HttpVersion.HTTP_1_1);
            if (null != timeOutMS) {
                // Set timeout
                RequestConfig requestConfig = RequestConfig.custom()
                        .setConnectTimeout(5000).setConnectionRequestTimeout(1000)
                        .setSocketTimeout(timeOutMS).build();
                httpPost.setConfig(requestConfig);
            }
 
            logger.info("call '" + url + "' start");
            // Perform the request operation and get the result (synchronous blocking)
            CloseableHttpResponse response = httpClient.execute(httpPost);
            logger.info("call succeeded,return msg:" + response.toString());
            // Get result entity
            // Determine whether the network connection status code is normal (0--200 are normal)
            switch (response.getStatusLine().getStatusCode()) {
                case HttpStatus.SC_OK:
                    result = EntityUtils.toString(response.getEntity(), "utf-8");
                    logger.info("call succeeded,return msg:" + result);
                    break;
                case HttpStatus.SC_CREATED:
                    result = EntityUtils.toString(response.getEntity(), "utf-8");
                    logger.info("call succeeded,return msg:" + result);
                    break;
                default:
                    result = INTERFACE_CALL_ERROR + ":" + EntityUtils.toString(response.getEntity(), "utf-8");
                    logger.warn("call failed,return msg:" + result);
                    break;
            }
        } catch (UnsupportedCharsetException e) {
            logger.error(INTERFACE_CALL_ERROR, e);
            result = (INTERFACE_CALL_ERROR + ":UnsupportedCharsetException");
        } catch (ClientProtocolException e) {
            logger.error(INTERFACE_CALL_ERROR, e);
            result = (INTERFACE_CALL_ERROR + ":ClientProtocolException");
        } catch (ParseException e) {
            logger.error(INTERFACE_CALL_ERROR, e);
            result = (INTERFACE_CALL_ERROR + ":ParseException");
        } catch (IOException e) {
            logger.error(INTERFACE_CALL_ERROR, e);
            result = (INTERFACE_CALL_ERROR + ":IOException");
        } finally {
            // Close the connection and release the resource
            httpPost.releaseConnection();
        }
        return result;
    }
 
    /**
     * Get request to transfer data
     *
     * @param url
     * @param map       Request incoming parameters ("key" is the parameter name, "value" is the parameter value) "map" can be empty
     * @param timeOutMS (Millisecond)
     * @return
     */
    public static String doGet(String url, Map<String, String> map, Integer timeOutMS) {
        String result = "";
        if (StringUtils.isNotBlank(url)) {
            try {
                // Create an "httpclient" object
                CloseableHttpClient httpClient = HttpClients.createDefault();
 
                // Create a "get" mode request object
                HttpGet httpGet = null;
                // Determine whether to add parameters
                if (null != map && !map.isEmpty()) {
                    // Since the parameters of the "GET" request are all assembled behind the "URL" address, we have to build a "URL" with parameters.
 
                    URIBuilder uriBuilder = new URIBuilder(url);
 
                    List<NameValuePair> list = new LinkedList<>();
                    for (String key : map.keySet()) {
                        BasicNameValuePair param = new BasicNameValuePair(key, map.get(key));
                        list.add(param);
                    }
 
                    uriBuilder.setParameters(list);
                    // Construct a "GET" request object from a "URI" object with parameters
                    httpGet = new HttpGet(uriBuilder.build());
                } else {
                    httpGet = new HttpGet(url);
                }
 
                // Add request header information
                // Browser representation
                // httpGet.addHeader("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1;
                // en-US; rv:1.7.6)");
                // Type of transmission
                // httpGet.addHeader("Content-Type", "application/x-www-form-urlencoded");
 
                // Type of transmission
                httpGet.addHeader("Content-type", "application/json");
                if (null != timeOutMS) {
                    // Set timeout
                    RequestConfig requestConfig = RequestConfig.custom()
                            .setConnectTimeout(5000).setConnectionRequestTimeout(1000)
                            .setSocketTimeout(timeOutMS).build();
                    httpGet.setConfig(requestConfig);
                }
 
                logger.info("call '" + url + "' start");
                // Get the response object by requesting the object
                CloseableHttpResponse response = httpClient.execute(httpGet);
                //logger.info("call succeeded,return msg:" + response.toString());
                logger.info("call succeeded");
                // Get result entity
                // Determine whether the network connection status code is normal (0--200 are normal)
                switch (response.getStatusLine().getStatusCode()) {
                    case HttpStatus.SC_OK:
                        result = EntityUtils.toString(response.getEntity(), "utf-8");
                        //logger.info("call succeeded,return msg:" + result);
                        logger.info("call succeeded");
                        break;
                    case HttpStatus.SC_CREATED:
                        result = EntityUtils.toString(response.getEntity(), "utf-8");
                        logger.info("call succeeded,return msg:" + result);
                        break;
                    default:
                        result = INTERFACE_CALL_ERROR + ":" + EntityUtils.toString(response.getEntity(), "utf-8");
                        logger.warn("call failed,return msg:" + result);
                        break;
                }
                // Release link
                response.close();
            } catch (ClientProtocolException e) {
                logger.error(INTERFACE_CALL_ERROR, e);
                result = (INTERFACE_CALL_ERROR + ":ClientProtocolException");
            } catch (ParseException e) {
                logger.error(INTERFACE_CALL_ERROR, e);
                result = (INTERFACE_CALL_ERROR + ":ParseException");
            } catch (IOException e) {
                logger.error(INTERFACE_CALL_ERROR, e);
                result = (INTERFACE_CALL_ERROR + ":IOException");
            } catch (URISyntaxException e) {
                logger.error(INTERFACE_CALL_ERROR, e);
                result = (INTERFACE_CALL_ERROR + ":URISyntaxException");
            }
        }
        return result;
    }
 
    public static String getHtml(String urlStr) {
 
        // Define links to be accessed
        String url = urlStr;
        // Define a string to store web content
        String result = "";
        // Define a buffered character input stream
        BufferedReader in = null;
        try {
            // Convert string to url object
            URL realUrl = new URL(url);
            // Initialize a link to the "url" link
            URLConnection connection = realUrl.openConnection();
            // Start the actual connection
            connection.connect();
            // Initialize the "BufferedReader" input stream to read the response of the "URL"
            in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            // Used to temporarily store data for each fetched row
            String line;
            while ((line = in.readLine()) != null) {
                // Traverse each row that is fetched and store it in "result"
                result += line + "\n";
            }
        } catch (Exception e) {
            logger.error("send get request is abnormal!" + e);
            e.printStackTrace();
        } // Use "finally" to close the input stream
        finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        logger.debug("html info:" + result);
        return result;
    }
 
    /**
     * "post" request to transfer "json" data
     *
     * @param url
     * @param timeOutMS (Millisecond)
     * @return
     */
    public static String doDelete(String url, Integer timeOutMS) {
        String result = "";
 
        // Create an "httpclient" object
        CloseableHttpClient httpClient = null;
        // Create a "post" mode request object
        HttpDelete httpDelete = null;
        try {
            // Create an "httpclient" object
            httpClient = HttpClients.createDefault();
            // Create a "post" mode request object
            httpDelete = new HttpDelete(url);
            httpDelete.setProtocolVersion(HttpVersion.HTTP_1_1);
            if (null != timeOutMS) {
                // Set timeout
                RequestConfig requestConfig = RequestConfig.custom()
                        .setConnectTimeout(5000).setConnectionRequestTimeout(1000)
                        .setSocketTimeout(timeOutMS).build();
                httpDelete.setConfig(requestConfig);
            }
 
            logger.info("call '" + url + "' start");
            // Perform the request operation and get the result (synchronous blocking)
            CloseableHttpResponse response = httpClient.execute(httpDelete);
            logger.info("call succeeded,return msg:" + response.toString());
            // Get result entity
            // Determine whether the network connection status code is normal (0--200 are normal)
            switch (response.getStatusLine().getStatusCode()) {
                case HttpStatus.SC_OK:
                    result = EntityUtils.toString(response.getEntity(), "utf-8");
                    logger.info("call succeeded,return msg:" + result);
                    break;
                case HttpStatus.SC_CREATED:
                    result = EntityUtils.toString(response.getEntity(), "utf-8");
                    logger.info("call succeeded,return msg:" + result);
                    break;
                default:
                    result = INTERFACE_CALL_ERROR + ":" + EntityUtils.toString(response.getEntity(), "utf-8");
                    logger.warn("call failed,return msg:" + result);
                    break;
            }
        } catch (UnsupportedCharsetException e) {
            logger.error(INTERFACE_CALL_ERROR, e);
            result = (INTERFACE_CALL_ERROR + ":UnsupportedCharsetException");
        } catch (ClientProtocolException e) {
            logger.error(INTERFACE_CALL_ERROR, e);
            result = (INTERFACE_CALL_ERROR + ":ClientProtocolException");
        } catch (ParseException e) {
            logger.error(INTERFACE_CALL_ERROR, e);
            result = (INTERFACE_CALL_ERROR + ":ParseException");
        } catch (IOException e) {
            logger.error(INTERFACE_CALL_ERROR, e);
            result = (INTERFACE_CALL_ERROR + ":IOException");
        } finally {
            // Close the connection and release the resource
            httpDelete.releaseConnection();
        }
        return result;
    }
}

JsonUtils.java

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.*;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
 
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
 
/**
 * Json utils
 */
@SuppressWarnings("deprecation")
public class JsonUtils {
 
    private static final ObjectMapper objectMapper;
    private static Logger logger = LoggerUtil.getLogger();
 
    static {
        objectMapper = new ObjectMapper();
        // Remove the default timestamp format
        objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
        // Set to Shanghai time zone in China
        objectMapper.setTimeZone(TimeZone.getTimeZone("GMT+8"));
        objectMapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false);
        // Null value not serialized
        objectMapper.setSerializationInclusion(Include.NON_NULL);
        // Compatible processing when attributes are not present during deserialization
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        // Uniform format of dates when serializing
        objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
        // It is forbidden to deserialize "Enum" with "int" on behalf of "Enum"
        objectMapper.configure(DeserializationFeature.FAIL_ON_NUMBERS_FOR_ENUMS, true);
        objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
        // objectMapper.configure(DeserializationFeature.USE_JAVA_ARRAY_FOR_JSON_ARRAY,
        // true);
        objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
        // Single quote processing
        objectMapper.configure(com.fasterxml.jackson.core.JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
        // objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.OBJECT_AND_NON_CONCRETE);
    }
 
    public static <T> T toObjectNoException(String json, Class<T> clazz) {
        try {
            return objectMapper.readValue(json, clazz);
        } catch (JsonParseException e) {
            logger.error(e.getMessage(), e);
        } catch (JsonMappingException e) {
            logger.error(e.getMessage(), e);
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
        }
        return null;
    }
 
    public static <T> String toJsonNoException(T entity) {
        try {
            return objectMapper.writeValueAsString(entity);
        } catch (JsonGenerationException e) {
            logger.error(e.getMessage(), e);
        } catch (JsonMappingException e) {
            logger.error(e.getMessage(), e);
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
        }
        return null;
    }
 
    public static <T> String toFormatJsonNoException(T entity) {
        try {
            return objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(entity);
        } catch (JsonGenerationException e) {
            logger.error(e.getMessage(), e);
        } catch (JsonMappingException e) {
            logger.error(e.getMessage(), e);
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
        }
        return null;
    }
 
    public static <T> T toCollectionNoException(String json, TypeReference<T> typeReference) {
        try {
            return objectMapper.readValue(json, typeReference);
        } catch (JsonParseException e) {
            logger.error(e.getMessage(), e);
        } catch (JsonMappingException e) {
            logger.error(e.getMessage(), e);
        } catch (IOException e) {
            logger.error(e.getMessage(), e);
        }
        return null;
    }
 
    /**
     * Object to "json" string
     *
     * @param object
     * @return
     * @throws JsonProcessingException
     */
    public static String toString(Object object) throws JsonProcessingException {
        return objectMapper.writeValueAsString(object);
    }
 
    /**
     * "json" string to object
     *
     * @param jsonString
     * @param rspValueType
     * @return
     * @throws JsonParseException
     * @throws JsonMappingException
     * @throws IOException
     */
    public static <T> T toObject(String jsonString, Class<T> rspValueType)
            throws JsonParseException, JsonMappingException, IOException {
        return objectMapper.readValue(jsonString, rspValueType);
    }
 
    public static JsonNode readJsonNode(String jsonStr, String fieldName) {
        if (StringUtils.isEmpty(jsonStr)) {
            return null;
        }
        try {
            JsonNode root = objectMapper.readTree(jsonStr);
            return root.get(fieldName);
        } catch (IOException e) {
            logger.error("parse json string error:" + jsonStr, e);
            return null;
        }
    }
 
    @SuppressWarnings("unchecked")
    public static <T> T readJson(JsonNode node, Class<?> parametrized, Class<?>... parameterClasses) throws Exception {
        JavaType javaType = objectMapper.getTypeFactory().constructParametricType(parametrized, parameterClasses);
        return (T) objectMapper.readValue(toString(node), javaType);
    }
 
    /**
     * When converting "JSON" when using "Jackson", the date format setting "Jackson" two methods set the date format of the output
     * <p>
     * 1. Ordinary way: The default is to convert to "timestamps" form, you can cancel "timestamps" by the following way.
     * objectMapper.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS,
     * false); This will cause the time generation to use the so-called use a [ISO-8601]-compliant notation, which outputs a time similar to the following:
     * "1970-01-01T00:00:00.000+0000". Of course, you can also customize the output format:
     * objectMapper.getSerializationConfig().setDateFormat(myDateFormat);
     * The myDateFormat object is java.text.DateFormat, which uses the annotation method of checking java API 2.annotaion:
     * First define the format you need as follows
     * <p>
     * Then find the date get method on your POJO
     *
     * @JsonSerialize(using = CustomDateSerializer.class) public Date getCreateAt()
     * { return createAt; }
     * <p>
     * "java" date object converted to "JSON" date formatted custom class via "Jackson" library
     * @date 2010-5-3
     */
    public class CustomDateSerializer extends JsonSerializer<Date> {
        @Override
        public void serialize(Date value, JsonGenerator jgen, SerializerProvider provider)
                throws IOException, JsonProcessingException {
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            String formattedDate = formatter.format(value);
            jgen.writeString(formattedDate);
        }
    }
}

調(diào)用示例:

post請(qǐng)求

String doPost = HttpUtils.doPost("url", formatJson,  5 * 1000); //5秒
JSONObject obj = JSONObject.fromObject(doPost).getJSONObject("test");

get請(qǐng)求

Map<String, String> map = new HashMap<>();
map.put("parms", parms);
String doGet = HttpUtils.doGet("url", map, 10 * 1000); //10秒
String jsonResult = JSONObject.fromObject(doGet).getString("test");
JSONObject obj = JSONObject.fromObject(jsonResult);

以前工具僅供參考??! 

到此這篇關(guān)于Java實(shí)現(xiàn)Http請(qǐng)求的常用方法詳解的文章就介紹到這了,更多相關(guān)Java Http請(qǐng)求內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • SpringBoot+JPA?分頁(yè)查詢指定列并返回指定實(shí)體方式

    SpringBoot+JPA?分頁(yè)查詢指定列并返回指定實(shí)體方式

    這篇文章主要介紹了SpringBoot+JPA?分頁(yè)查詢指定列并返回指定實(shí)體方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • 基于log4j2.properties踩坑與填坑

    基于log4j2.properties踩坑與填坑

    這篇文章主要介紹了log4j2.properties踩坑與填坑方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • 一文總結(jié) Shiro 實(shí)戰(zhàn)教程

    一文總結(jié) Shiro 實(shí)戰(zhàn)教程

    shiro是apache的一個(gè)開(kāi)源框架,是一個(gè)權(quán)限管理的框架,實(shí)現(xiàn) 用戶認(rèn)證、用戶授權(quán),這篇文章詳細(xì)總結(jié)了shiro用法,感興趣的同學(xué)可以參考閱讀
    2023-04-04
  • SpringBoot之Helloword 快速搭建一個(gè)web項(xiàng)目(圖文)

    SpringBoot之Helloword 快速搭建一個(gè)web項(xiàng)目(圖文)

    這篇文章主要介紹了SpringBoot之Helloword 快速搭建一個(gè)web項(xiàng)目(圖文),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-12-12
  • Java編程基礎(chǔ)測(cè)試題分享

    Java編程基礎(chǔ)測(cè)試題分享

    這篇文章主要介紹了Java編程基礎(chǔ)測(cè)試題分享,具有一定參考價(jià)值,需要的朋友可以了解下。
    2017-10-10
  • SpringBoot使用token簡(jiǎn)單鑒權(quán)的具體實(shí)現(xiàn)方法

    SpringBoot使用token簡(jiǎn)單鑒權(quán)的具體實(shí)現(xiàn)方法

    這篇文章主要介紹了SpringBoot使用token簡(jiǎn)單鑒權(quán)的具體實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-11-11
  • 200行Java代碼如何實(shí)現(xiàn)依賴注入框架詳解

    200行Java代碼如何實(shí)現(xiàn)依賴注入框架詳解

    依賴注入對(duì)大家來(lái)說(shuō)應(yīng)該都不陌生,下面這篇文章主要給大家介紹了關(guān)于利用200行Java代碼如何實(shí)現(xiàn)依賴注入框架的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2018-05-05
  • 詳解SpringBoot+Lucene案例介紹

    詳解SpringBoot+Lucene案例介紹

    這篇文章主要介紹了詳解SpringBoot+Lucene案例介紹,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-06-06
  • kafka?消息隊(duì)列中點(diǎn)對(duì)點(diǎn)與發(fā)布訂閱的區(qū)別說(shuō)明

    kafka?消息隊(duì)列中點(diǎn)對(duì)點(diǎn)與發(fā)布訂閱的區(qū)別說(shuō)明

    這篇文章主要介紹了kafka?消息隊(duì)列中點(diǎn)對(duì)點(diǎn)與發(fā)布訂閱的區(qū)別說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • Java顯示程序包不存在的三種解決方法總結(jié)

    Java顯示程序包不存在的三種解決方法總結(jié)

    在Java開(kāi)發(fā)中,有時(shí)會(huì)遇到“程序包javax.servlet不存在”等錯(cuò)誤提示,這通常是因?yàn)槿鄙俦匾膸?kù)或依賴項(xiàng),這篇文章主要給大家介紹了關(guān)于Java顯示程序包不存在的三種解決方法,需要的朋友可以參考下
    2024-07-07

最新評(píng)論