Java調(diào)用第三方http接口的常用方式總結(jié)
1.概述
在實(shí)際開(kāi)發(fā)過(guò)程中,我們經(jīng)常需要調(diào)用對(duì)方提供的接口或測(cè)試自己寫的接口是否合適。很多項(xiàng)目都會(huì)封裝規(guī)定好本身項(xiàng)目的接口規(guī)范,所以大多數(shù)需要去調(diào)用對(duì)方提供的接口或第三方接口(短信、天氣等)。
在Java項(xiàng)目中調(diào)用第三方接口的常用方式有
- ①通過(guò)JDK網(wǎng)絡(luò)類Java.net.HttpURLConnection;
- ②通過(guò)Apache common封裝好的HttpClient;
- ③通過(guò)Apache封裝好的CloseableHttpClient;
- ④通過(guò)OkHttp;
- ⑤通過(guò)Spring的RestTemplate;
- ⑥通過(guò)hutool的HttpUtil。
2.Java調(diào)用第三方http接口的方式
2.1 通過(guò)JDK網(wǎng)絡(luò)類Java.net.HttpURLConnection
簡(jiǎn)介:java.net包下的原生java api提供的http請(qǐng)求。
使用步驟:
1、通過(guò)統(tǒng)一資源定位器(java.net.URL)獲取連接器(java.net.URLConnection)。
2、設(shè)置請(qǐng)求的參數(shù)。
3、發(fā)送請(qǐng)求。
4、以輸入流的形式獲取返回內(nèi)容。
5、關(guān)閉輸入流。
比較原始的一種調(diào)用做法,這里把get請(qǐng)求和post請(qǐng)求都統(tǒng)一放在一個(gè)方法里面,直接上代碼:
import com.alibaba.fastjson.JSON;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* @ClassName : HttpUrlConnectionToInterface
* @Description : jdk類HttpURLConnection調(diào)用第三方http接口
* @Author : THQ
* @Date: 2021-09-02 14:09
* @Version V1.0
*/
public class HttpUrlConnectionToInterface {
/**
* 以post方式調(diào)用對(duì)方接口方法
* @param pathUrl
*/
public static void doPost(String pathUrl, String data){
OutputStreamWriter out = null;
BufferedReader br = null;
String result = "";
try {
URL url = new URL(pathUrl);
//打開(kāi)和url之間的連接
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//設(shè)定請(qǐng)求的方法為"POST",默認(rèn)是GET
//post與get的不同之處在于post的參數(shù)不是放在URL字串里面,而是放在http請(qǐng)求的正文內(nèi)。
conn.setRequestMethod("POST");
//設(shè)置30秒連接超時(shí)
conn.setConnectTimeout(30000);
//設(shè)置30秒讀取超時(shí)
conn.setReadTimeout(30000);
// 設(shè)置是否向httpUrlConnection輸出,因?yàn)檫@個(gè)是post請(qǐng)求,參數(shù)要放在http正文內(nèi),因此需要設(shè)為true, 默認(rèn)情況下是false;
conn.setDoOutput(true);
// 設(shè)置是否從httpUrlConnection讀入,默認(rèn)情況下是true;
conn.setDoInput(true);
// Post請(qǐng)求不能使用緩存
conn.setUseCaches(false);
//設(shè)置通用的請(qǐng)求屬性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive"); //維持長(zhǎng)鏈接
conn.setRequestProperty("Content-Type", "application/json;charset=utf-8");
//連接,從上述url.openConnection()至此的配置必須要在connect之前完成,
conn.connect();
/**
* 下面的三句代碼,就是調(diào)用第三方http接口
*/
//獲取URLConnection對(duì)象對(duì)應(yīng)的輸出流
//此處getOutputStream會(huì)隱含的進(jìn)行connect(即:如同調(diào)用上面的connect()方法,所以在開(kāi)發(fā)中不調(diào)用上述的connect()也可以)。
out = new OutputStreamWriter(conn.getOutputStream(), "UTF-8");
//發(fā)送請(qǐng)求參數(shù)即數(shù)據(jù)
out.write(data);
//flush輸出流的緩沖
out.flush();
/**
* 下面的代碼相當(dāng)于,獲取調(diào)用第三方http接口后返回的結(jié)果
*/
//獲取URLConnection對(duì)象對(duì)應(yīng)的輸入流
InputStream is = conn.getInputStream();
//構(gòu)造一個(gè)字符流緩存
br = new BufferedReader(new InputStreamReader(is));
String str = "";
while ((str = br.readLine()) != null){
result += str;
}
System.out.println(result);
//關(guān)閉流
is.close();
//斷開(kāi)連接,disconnect是在底層tcp socket鏈接空閑時(shí)才切斷,如果正在被其他線程使用就不切斷。
conn.disconnect();
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if (out != null){
out.close();
}
if (br != null){
br.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 以get方式調(diào)用對(duì)方接口方法
* @param pathUrl
*/
public static void doGet(String pathUrl){
BufferedReader br = null;
String result = "";
try {
URL url = new URL(pathUrl);
//打開(kāi)和url之間的連接
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//設(shè)定請(qǐng)求的方法為"GET",默認(rèn)是GET
//post與get的不同之處在于post的參數(shù)不是放在URL字串里面,而是放在http請(qǐng)求的正文內(nèi)。
conn.setRequestMethod("GET");
//設(shè)置30秒連接超時(shí)
conn.setConnectTimeout(30000);
//設(shè)置30秒讀取超時(shí)
conn.setReadTimeout(30000);
// 設(shè)置是否向httpUrlConnection輸出,因?yàn)檫@個(gè)是post請(qǐng)求,參數(shù)要放在http正文內(nèi),因此需要設(shè)為true, 默認(rèn)情況下是false;
conn.setDoOutput(true);
// 設(shè)置是否從httpUrlConnection讀入,默認(rèn)情況下是true;
conn.setDoInput(true);
// Post請(qǐng)求不能使用緩存(get可以不使用)
conn.setUseCaches(false);
//設(shè)置通用的請(qǐng)求屬性
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive"); //維持長(zhǎng)鏈接
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
//連接,從上述url.openConnection()至此的配置必須要在connect之前完成,
conn.connect();
/**
* 下面的代碼相當(dāng)于,獲取調(diào)用第三方http接口后返回的結(jié)果
*/
//獲取URLConnection對(duì)象對(duì)應(yīng)的輸入流
InputStream is = conn.getInputStream();
//構(gòu)造一個(gè)字符流緩存
br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
String str = "";
while ((str = br.readLine()) != null){
result += str;
}
System.out.println(result);
//關(guān)閉流
is.close();
//斷開(kāi)連接,disconnect是在底層tcp socket鏈接空閑時(shí)才切斷,如果正在被其他線程使用就不切斷。
conn.disconnect();
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if (br != null){
br.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
//post請(qǐng)求一般都是把實(shí)體對(duì)象轉(zhuǎn)為Json字符串
LocationPrintDTO locationPrintDTO = new LocationPrintDTO();
String s = JSON.toJSONString(locationPrintDTO);
doPost("http://127.0.0.1:9090/ZebraPrinter/locationPrint", s);
doGet("https://weather.cma.cn/api/climate?stationid=57516");
}
}2.2 通過(guò)apache common封裝好的HttpClient
簡(jiǎn)介:http client到目前為止最新是5.1版,官網(wǎng)地址:http://hc.apache.org/ 。Http client專為推展而設(shè)計(jì),同時(shí)為基本http協(xié)議提供強(qiáng)大支持,盡管java.net包提供了通過(guò)http訪問(wèn)的基本功能,但是未提供許多應(yīng)用程序所需要功能。
使用步驟:
- 生成一個(gè)HttpClient對(duì)象并設(shè)置相應(yīng)的參數(shù);
- 生成一個(gè)GetMethod對(duì)象或PostMethod并設(shè)置響應(yīng)的參數(shù);
- 用HttpClient生成的對(duì)象來(lái)執(zhí)行GetMethod生成的Get方法;
- 處理響應(yīng)狀態(tài)碼;
- 若響應(yīng)正常,處理HTTP響應(yīng)內(nèi)容;
- 釋放連接。無(wú)論執(zhí)行方法是否成功,都必須釋放連接。
導(dǎo)入如下jar包:
<!--HttpClient-->
<dependency>
<groupId>commons-httpclient</groupId>
<artifactId>commons-httpclient</artifactId>
<version>3.1</version>
</dependency>代碼如下:
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
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;
/**
* @ClassName : HttpClientToInterface
* @Description : HttpClient模擬get、post請(qǐng)求并發(fā)送請(qǐng)求參數(shù)(json等)
* @Author : THQ
* @Date: 2021-09-02 16:50
* @Version V1.0
*/
public class HttpClientToInterface {
/**
* 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=utf-8");
//必須設(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) {
doGet("http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=13026194071", "UTF-8");
System.out.println("-----------分割線------------");
System.out.println("-----------分割線------------");
System.out.println("-----------分割線------------");
JSONObject jsonObject = new JSONObject();
jsonObject.put("commentId", "13026194071");
doPost("http://tcc.taobao.com/cc/json/mobile_tel_segment.htm", jsonObject);
}
}2.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>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.28</version>
</dependency>代碼如下:
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
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;
/**
* @ClassName : CloseableHttpClientToInterface
* @Description : Apache封裝好的CloseableHttpClient
* @Author : THQ
* @Date: 2021-09-02 17:16
* @Version V1.0
*/
public class CloseableHttpClientToInterface {
private static String tokenString = "";
private static String AUTH_TOKEN_EXPIRED = "AUTH_TOKEN_EXPIRED";
private static CloseableHttpClient httpClient = null;
/**
* 以get方式調(diào)用第三方接口
* @param url
* @return
*/
public static String doGet(String url, String token){
//創(chuàng)建HttpClient對(duì)象
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
HttpGet get = new HttpGet(url);
try {
if (tokenString != null && !tokenString.equals("")){
tokenString = getToken();
}
//api_gateway_auth_token自定義header頭,用于token驗(yàn)證使用
get.addHeader("api_gateway_auth_token", tokenString);
get.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");
HttpResponse response = httpClient.execute(get);
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){
try {
if (httpClient == null){
httpClient = HttpClientBuilder.create().build();
}
HttpPost post = new HttpPost(url);
if (tokenString != null && !tokenString.equals("")){
tokenString = getToken();
}
//api_gateway_auth_token自定義header頭,用于token驗(yàn)證使用
post.addHeader("api_gateway_auth_token", tokenString);
post.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");
StringEntity s = new StringEntity(json.toString());
s.setContentEncoding("UTF-8");
//發(fā)送json數(shù)據(jù)需要設(shè)置contentType
s.setContentType("application/x-www-form-urlencoded");
//設(shè)置請(qǐng)求參數(shù)
post.setEntity(s);
HttpResponse response = httpClient.execute(post);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
//返回json格式
String res = EntityUtils.toString(response.getEntity());
return res;
}
} catch (Exception 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");
try {
if (httpClient == null){
httpClient = HttpClientBuilder.create().build();
}
HttpPost post = new HttpPost("http://localhost/login");
post.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");
StringEntity s = new StringEntity(object.toString());
s.setContentEncoding("UTF-8");
//發(fā)送json數(shù)據(jù)需要設(shè)置contentType
s.setContentType("application/x-www-form-urlencoded");
//設(shè)置請(qǐng)求參數(shù)
post.setEntity(s);
HttpResponse response = httpClient.execute(post);
//這里可以把返回的結(jié)果按照自定義的返回?cái)?shù)據(jù)結(jié)果,把string轉(zhuǎn)換成自定義類
//ResultTokenBO result = JSONObject.parseObject(response, ResultTokenBO.class);
//把response轉(zhuǎn)為jsonObject
JSONObject result = JSONObject.parseObject(String.valueOf(response));
if (result.containsKey("token")){
token = result.getString("token");
}
} catch (Exception e) {
e.printStackTrace();
}
return token;
}
/**
* 測(cè)試
*/
public static void test(String telephone){
JSONObject object = new JSONObject();
object.put("telephone", telephone);
try {
//首先獲取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);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
test("12345678910");
}2.4 通過(guò)OkHttp
簡(jiǎn)介:OkHttp是一個(gè)默認(rèn)有效的HTTP客戶端,有效地執(zhí)行HTTP可以加快您的負(fù)載并節(jié)省帶寬,如果您的服務(wù)有多個(gè)IP地址,如果第一次連接失敗,OkHttp將嘗試備用地址。這對(duì)于IPv4 + IPv6和冗余數(shù)據(jù)中心中托管的服務(wù)是必需的。OkHttp啟動(dòng)具有現(xiàn)代TLS功能(SNI,ALPN)的新連接,并在握手失敗時(shí)回退到TLS 1.0,OkHttp支持Android 2.3及更高版本。對(duì)于Java,最低要求是1.7。
操作步驟:
1、創(chuàng)建OkhttpClient。
2、mClient執(zhí)行newCall將Request轉(zhuǎn)化成一個(gè)Call。
3、最后call執(zhí)行excute同步執(zhí)行,enqueue異步執(zhí)行。
4、Request主要通過(guò)Request.Builder來(lái)構(gòu)建。
5、緩存。
6、取消請(qǐng)求。
導(dǎo)入如下jar包:
<!--okhttp3-->
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.9.1</version>
</dependency>代碼如下:
import okhttp3.*;
import java.io.IOException;
/**
* @ClassName : OkHttpToInterface
* @Description :
* @Author : THQ
* @Date: 2021-09-02 17:36
* @Version V1.0
*/
public class OkHttpToInterface {
public static final MediaType JSON = MediaType.get("application/json; charset=utf-8");
/**
* 以get方式調(diào)用第三方接口
* @param url
*/
public static void doGet(String url) {
OkHttpClient okHttpClient = new OkHttpClient();
final Request request = new Request.Builder()
.url(url)
.get()//默認(rèn)就是GET請(qǐng)求,可以不寫
.build();
Call call = okHttpClient.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
System.out.println( "onFailure: ");
}
@Override
public void onResponse(Call call, Response response) throws IOException {
System.out.println("onResponse: " + response.body().string());
}
});
}
/**
* post請(qǐng)求
* @param url
* @param json
*/
public static void doPost(String url, String json){
MediaType mediaType = MediaType.parse("text/x-markdown; charset=utf-8");
String requestBody = json;
Request request = new Request.Builder()
.url(url)
.post(RequestBody.create(mediaType, requestBody))
.build();
OkHttpClient okHttpClient = new OkHttpClient();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
System.out.println("onFailure: " + e.getMessage());
}
@Override
public void onResponse(Call call, Response response) throws IOException {
System.out.println(response.protocol() + " " +response.code() + " " + response.message());
Headers headers = response.headers();
for (int i = 0; i < headers.size(); i++) {
System.out.println(headers.name(i) + ":" + headers.value(i));
}
System.out.println("onResponse: " + response.body().string());
}
});
}
public static void main(String[] args) {
doGet("http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=13026194071");
doPost("https://api.github.com/markdown/raw","I am Jdqm.");
}
}2.5 通過(guò)Spring的RestTemplate
RestTemple是前三種方式的集大成者,代碼編寫更加簡(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)建資源的URLput()PUT 資源到特定的URL
注意:目前標(biāo)紅的為常用的
操作步驟:
1、使用默認(rèn)構(gòu)造方法new一個(gè)實(shí)例new RestTemplate()。
2、RestTemplate 內(nèi)部通過(guò)調(diào)用 doExecute 方法,首先就是獲取 ClientHttpRequest。
3、RestTemplate 實(shí)現(xiàn)了抽象類 HttpAccessor ,所以可以調(diào)用父類的 createRequest。
4、SimpleClientHttpRequestFactory 實(shí)現(xiàn)了 ClientHttpRequest,同時(shí)實(shí)現(xiàn)方法。
5、執(zhí)行 requestCallback.doWithRequest(request)。
6、執(zhí)行 response = request.execute()。
7、最后解析response。
首先導(dǎo)入springboot的web包
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</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 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;
/**
* @ClassName : RestTemplateConfig
* @Description :
* @Author : THQ
* @Date: 2021-09-02 20:00
* @Version V1.0
*/
@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate(ClientHttpRequestFactory factory){
return new RestTemplate(factory);
}
@Bean
public ClientHttpRequestFactory simpleClientHttpRequestFactory(){
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
factory.setConnectTimeout(15000);
factory.setReadTimeout(5000);
return factory;
}
}然后在Service類(RestTemplateToInterface )中注入使用
具體代碼如下:
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;
/**
* @ClassName : RestTemplateToInterface
* @Description :
* @Author : THQ
* @Date: 2021-09-02 20:03
* @Version V1.0
*/
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;
}
}2.6通過(guò)hutool的HttpUtil
簡(jiǎn)介:關(guān)于Hutool工具類之HttpUtil如何使用可以參考官方文檔Hutool之HttpUtil。
導(dǎo)入如下jar包:
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.7.1</version>
</dependency>代碼如下:
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.http.HttpUtil;
import java.util.HashMap;
/**
* @ClassName : HttpUtilToInterface
* @Description :
* @Author : THQ
* @Date: 2021-09-02 20:15
* @Version V1.0
*/
public class HttpUtilToInterface {
/**
* get請(qǐng)求示例
*/
public static void doGet() {
// 最簡(jiǎn)單的HTTP請(qǐng)求,可以自動(dòng)通過(guò)header等信息判斷編碼,不區(qū)分HTTP和HTTPS
String result1 = HttpUtil.get("https://www.baidu.com");
// 當(dāng)無(wú)法識(shí)別頁(yè)面編碼的時(shí)候,可以自定義請(qǐng)求頁(yè)面的編碼
String result2 = HttpUtil.get("https://www.baidu.com", CharsetUtil.CHARSET_UTF_8);
//可以單獨(dú)傳入http參數(shù),這樣參數(shù)會(huì)自動(dòng)做URL編碼,拼接在URL中
HashMap<String, Object> paramMap = new HashMap<>();
paramMap.put("city", "北京");
String result3 = HttpUtil.get("https://www.baidu.com", paramMap);
}
/**
* post請(qǐng)求示例
*/
public static void doPost() {
//post普通請(qǐng)求示例
HashMap<String, Object> paramMap = new HashMap<>();
paramMap.put("city", "北京");
String result= HttpUtil.post("https://www.baidu.com", paramMap);
//文件上傳示例
HashMap<String, Object> paramMap1 = new HashMap<>();
//文件上傳只需將參數(shù)中的鍵指定(默認(rèn)file),值設(shè)為文件對(duì)象即可,對(duì)于使用者來(lái)說(shuō),文件上傳與普通表單提交并無(wú)區(qū)別
paramMap1.put("file", FileUtil.file("D:\\face.jpg"));
String result1= HttpUtil.post("https://www.baidu.com", paramMap1);
//下載文件(很少用)
String fileUrl = "http://mirrors.sohu.com/centos/8.4.2105/isos/x86_64/CentOS-8.4.2105-x86_64-dvd1.iso";
//將文件下載后保存在E盤,返回結(jié)果為下載文件大小
long size = HttpUtil.downloadFile(fileUrl, FileUtil.file("e:/"));
System.out.println("Download size: " + size);
}
}3.總結(jié)
日常開(kāi)發(fā)中,我們一般使用spring的resttemplate和hutool的HttpUtil偏多,特別是hutool,非常推薦,里面有很多省心的工具類。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Java如何基于okhttp請(qǐng)求SSE接口流式返回詳解
- Java請(qǐng)求Http接口OkHttp超詳細(xì)講解(附帶工具類)
- java+Okhttp3調(diào)用接口的實(shí)例
- Java中的HttpServletRequest接口詳細(xì)解讀
- Java調(diào)用HTTPS接口實(shí)現(xiàn)繞過(guò)SSL認(rèn)證
- Java調(diào)用第三方http接口的四種方式總結(jié)
- Java發(fā)送http請(qǐng)求調(diào)用第三方接口獲取token方式
- Java實(shí)現(xiàn)調(diào)用對(duì)方http接口得到返回?cái)?shù)據(jù)
- Java 調(diào)用 HTTP 接口的 7 種方式示例代碼(全網(wǎng)最全指南)
相關(guān)文章
Springmvc工程跳轉(zhuǎn)controller無(wú)效的解決
這篇文章主要介紹了Springmvc工程跳轉(zhuǎn)controller無(wú)效的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09
Spring定時(shí)任務(wù)無(wú)故停止又不報(bào)錯(cuò)的解決
這篇文章主要介紹了Spring定時(shí)任務(wù)無(wú)故停止又不報(bào)錯(cuò)的解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
Java實(shí)現(xiàn)簡(jiǎn)單酒店管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)簡(jiǎn)單酒店管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05
使用RedisAtomicLong優(yōu)化性能問(wèn)題
這篇文章主要介紹了使用RedisAtomicLong優(yōu)化性能問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11
Java類加載機(jī)制實(shí)現(xiàn)流程及原理詳解
這篇文章主要介紹了Java類加載機(jī)制實(shí)現(xiàn)流程及原理詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06
解決Feign切換client到okhttp無(wú)法生效的坑(出現(xiàn)原因說(shuō)明)
這篇文章主要介紹了解決Feign切換client到okhttp無(wú)法生效的坑(出現(xiàn)原因說(shuō)明),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-02-02

