Spring Boot如何使用httpcomponents實現(xiàn)http請求
基于org.apache.httpcomponents的httpclient實現(xiàn),其它的實現(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實現(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)用服務端異常."); } HttpEntity res = response.getEntity(); String resultData = EntityUtils.toString(res); System.out.println("從服務端返回結(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)用服務端異常."); } HttpEntity res = response.getEntity(); String resultData = EntityUtils.toString(res); System.out.println("從服務端返回結(jié)果: " + resultData); httpClient.close(); return resultData; } }
3. 創(chuàng)建springboot工程,提供接口訪問
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) { //處理注冊邏輯 return registerList.add(data.getString("name"), data.getString("ip")); } @GetMapping("/list/{name}") public Object getList(@PathVariable(value = "name", required = true) String name) { //獲取注冊列表 return registerList.getByName(name); } @GetMapping("/list/all") public Object getList() { return registerList.getAll(); } }
4. 創(chuàng)建springboot工程,實現(xiàn)http請求
通過ApplicationRunner 實現(xià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ā)送注冊消息 @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實現(xiàn)http請求的文章就介紹到這了,更多相關(guān)Spring Boot使用httpcomponents內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring Boot兩種配置文件properties和yml區(qū)別
這篇文章主要為大家介紹了java面試中常見問到的Spring Boot兩種配置文件properties和yml區(qū)別解答,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-07-07Spring?Boot開發(fā)RESTful接口與http協(xié)議狀態(tài)表述
這篇文章主要為大家介紹了Spring?Boot開發(fā)RESTful接口與http協(xié)議狀態(tài)表述,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步2022-03-03SpringBoot讀取自定義配置文件方式(properties,yaml)
這篇文章主要介紹了SpringBoot讀取自定義配置文件方式(properties,yaml),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-07-07Mybatis MapperScannerConfigurer自動掃描Mapper接口生成代理注入到Spring的方法
這篇文章主要給大家介紹了關(guān)于Mybatis MapperScannerConfigurer自動掃描將Mapper接口生成代理注入到Spring的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下2019-03-03Spring Security之LogoutSuccessHandler注銷成功操作方式
這篇文章主要介紹了Spring Security之LogoutSuccessHandler注銷成功操作方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-08-08