Spring Boot如何使用httpcomponents實(shí)現(xiàn)http請(qǐng)求
基于org.apache.httpcomponents的httpclient實(shí)現(xiàn),其它的實(shí)現(xiàn)方式都行。
1. pom文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>registerTest</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>server</module>
<module>provider1</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.1.23</version>
</dependency>
</dependencies>
</project>2. HttpClient實(shí)現(xiàn)get和post方法
package http.client;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
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;
public class HttpClient {
private String url;
private String contentType;
private String encoding;
private JSONObject data;
public HttpClient(String url, String encoding, String contentType, JSONObject data) {
this.url = url;
this.data = data;
this.encoding = encoding == null ? "UTF-8" : encoding;
this.contentType = contentType == null ? "application/json" : contentType;
}
public String httpGet() throws Exception {
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
HttpGet httpGet = new HttpGet(url);
HttpResponse response = httpClient.execute(httpGet);
if (response.getStatusLine().getStatusCode() != 200) {
throw new Exception("調(diào)用服務(wù)端異常.");
}
HttpEntity res = response.getEntity();
String resultData = EntityUtils.toString(res);
System.out.println("從服務(wù)端返回結(jié)果: " + resultData);
httpClient.close();
return resultData;
}
public String httpPost() throws Exception {
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
String json = JSONObject.toJSONString(data);
StringEntity entity = new StringEntity(json);
entity.setContentEncoding(encoding);
entity.setContentType(contentType);
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost);
if (response.getStatusLine().getStatusCode() != 200) {
throw new Exception("調(diào)用服務(wù)端異常.");
}
HttpEntity res = response.getEntity();
String resultData = EntityUtils.toString(res);
System.out.println("從服務(wù)端返回結(jié)果: " + resultData);
httpClient.close();
return resultData;
}
}3. 創(chuàng)建springboot工程,提供接口訪問(wèn)
package register.control;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import register.list.RegisterList;
@RestController
@RequestMapping("/register")
public class Register {
@Autowired
RegisterList registerList;
@PostMapping("/register")
public String register(@RequestBody JSONObject data) {
//處理注冊(cè)邏輯
return registerList.add(data.getString("name"), data.getString("ip"));
}
@GetMapping("/list/{name}")
public Object getList(@PathVariable(value = "name", required = true) String name) {
//獲取注冊(cè)列表
return registerList.getByName(name);
}
@GetMapping("/list/all")
public Object getList() {
return registerList.getAll();
}
}4. 創(chuàng)建springboot工程,實(shí)現(xiàn)http請(qǐng)求
通過(guò)ApplicationRunner 實(shí)現(xiàn)啟動(dòng)自動(dòng)運(yùn)行。
package common.register;
import com.alibaba.fastjson.JSONObject;
import http.client.HttpClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
@Component
public class Register implements ApplicationRunner {
@Value("${model.name}")
String name;
@Value("${model.host}")
String host;
@Value("${model.port}")
String port;
@Value("${register.url}")
String url;
//發(fā)送注冊(cè)消息
@Override
public void run(ApplicationArguments args) throws Exception {
JSONObject data = new JSONObject() {{
put("name", name);
put("ip", "http://" + host + ":" + port);
}};
HttpClient client = new HttpClient(url, null, null, data);
client.httpPost();
}
}結(jié)果:成功返回

到此這篇關(guān)于Spring Boot使用httpcomponents實(shí)現(xiàn)http請(qǐng)求的文章就介紹到這了,更多相關(guān)Spring Boot使用httpcomponents內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java正則表達(dá)式處理特殊字符轉(zhuǎn)義的方法
由于正則表達(dá)式定了一些特殊字符,而有時(shí)候需要對(duì)這些特殊字符進(jìn)行匹配的話就需要進(jìn)行轉(zhuǎn)義了,下面這篇文章主要給大家介紹了Java正則表達(dá)式處理特殊字符轉(zhuǎn)義的方法,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-01-01
Jdbctemplate多數(shù)據(jù)源配置方法詳解
這篇文章主要介紹了Jdbctemplate多數(shù)據(jù)源配置方法詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06
Spark Maven項(xiàng)目打包后找不到主類問(wèn)題及解決
在使用IDEA、Maven創(chuàng)建Spark項(xiàng)目時(shí),遇到打包后Scala程序找不到主類的問(wèn)題,原因是Maven缺少scala-maven-plugin插件,通過(guò)添加該插件并重新構(gòu)建項(xiàng)目,問(wèn)題得到解決2026-02-02
Java8中Stream使用的一個(gè)注意事項(xiàng)
最近在工作中發(fā)現(xiàn)了對(duì)于集合操作轉(zhuǎn)換的神器,java8新特性 stream,但在使用中遇到了一個(gè)非常重要的注意點(diǎn),所以這篇文章主要給大家介紹了關(guān)于Java8中Stream使用過(guò)程中的一個(gè)注意事項(xiàng),需要的朋友可以參考借鑒,下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-11-11
SpringMvc+Angularjs 實(shí)現(xiàn)多文件批量上傳
本文通過(guò)實(shí)例代碼給大家講解了SpringMvc+Angularjs 實(shí)現(xiàn)多文件批量上傳功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友一起學(xué)習(xí)吧2017-03-03
SpringBoot2.x使用POI實(shí)現(xiàn)導(dǎo)入數(shù)據(jù)到Excel
這篇文章主要為大家詳細(xì)介紹了SpringBoot2.x如何使用POI實(shí)現(xiàn)導(dǎo)入數(shù)據(jù)到Excel,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2025-07-07
Java代理的幾種實(shí)現(xiàn)方式總結(jié)
本文將通過(guò)例子說(shuō)明java代理的幾種實(shí)現(xiàn)方式,并比較它們之間的差異,文中通過(guò)代碼示例給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的參考價(jià)值,需要的朋友可以參考下2023-12-12

