最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring Boot如何使用httpcomponents實現(xiàn)http請求

 更新時間:2023年07月17日 10:24:08   作者:青銅愛碼士  
這篇文章主要介紹了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("調用服務端異常.");
        }
        HttpEntity res = response.getEntity();
        String resultData = EntityUtils.toString(res);
        System.out.println("從服務端返回結果: " + 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("調用服務端異常.");
        }
        HttpEntity res = response.getEntity();
        String resultData = EntityUtils.toString(res);
        System.out.println("從服務端返回結果: " + 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();
    }
}

結果:成功返回

在這里插入圖片描述

到此這篇關于Spring Boot使用httpcomponents實現(xiàn)http請求的文章就介紹到這了,更多相關Spring Boot使用httpcomponents內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Java正則表達式處理特殊字符轉義的方法

    Java正則表達式處理特殊字符轉義的方法

    由于正則表達式定了一些特殊字符,而有時候需要對這些特殊字符進行匹配的話就需要進行轉義了,下面這篇文章主要給大家介紹了Java正則表達式處理特殊字符轉義的方法,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-01-01
  • Jdbctemplate多數(shù)據(jù)源配置方法詳解

    Jdbctemplate多數(shù)據(jù)源配置方法詳解

    這篇文章主要介紹了Jdbctemplate多數(shù)據(jù)源配置方法詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-06-06
  • java自定義JDBC實現(xiàn)連接池

    java自定義JDBC實現(xiàn)連接池

    本文主要介紹了java自定義JDBC實現(xiàn)連接池,包含實現(xiàn)JDBC連接池以及SQLException?異常的處理,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2024-02-02
  • MyBatis 核心配置文件及映射文件詳解

    MyBatis 核心配置文件及映射文件詳解

    MyBatis是支持定制化SQL、存儲過程以及高級映射的優(yōu)秀的持久層框架,本文重點介紹MyBatis 核心配置文件及映射文件,需要的朋友可以參考下
    2023-01-01
  • java使用正則抓取網(wǎng)頁郵箱

    java使用正則抓取網(wǎng)頁郵箱

    這篇文章主要為大家詳細介紹了java使用正則抓取網(wǎng)頁郵箱的相關資料,感興趣的小伙伴們可以參考一下
    2016-05-05
  • Spark Maven項目打包后找不到主類問題及解決

    Spark Maven項目打包后找不到主類問題及解決

    在使用IDEA、Maven創(chuàng)建Spark項目時,遇到打包后Scala程序找不到主類的問題,原因是Maven缺少scala-maven-plugin插件,通過添加該插件并重新構建項目,問題得到解決
    2026-02-02
  • Java8中Stream使用的一個注意事項

    Java8中Stream使用的一個注意事項

    最近在工作中發(fā)現(xiàn)了對于集合操作轉換的神器,java8新特性 stream,但在使用中遇到了一個非常重要的注意點,所以這篇文章主要給大家介紹了關于Java8中Stream使用過程中的一個注意事項,需要的朋友可以參考借鑒,下面隨著小編來一起學習學習吧。
    2017-11-11
  • SpringMvc+Angularjs 實現(xiàn)多文件批量上傳

    SpringMvc+Angularjs 實現(xiàn)多文件批量上傳

    本文通過實例代碼給大家講解了SpringMvc+Angularjs 實現(xiàn)多文件批量上傳功能,非常不錯,具有參考借鑒價值,需要的朋友一起學習吧
    2017-03-03
  • SpringBoot2.x使用POI實現(xiàn)導入數(shù)據(jù)到Excel

    SpringBoot2.x使用POI實現(xiàn)導入數(shù)據(jù)到Excel

    這篇文章主要為大家詳細介紹了SpringBoot2.x如何使用POI實現(xiàn)導入數(shù)據(jù)到Excel,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學習一下
    2025-07-07
  • Java代理的幾種實現(xiàn)方式總結

    Java代理的幾種實現(xiàn)方式總結

    本文將通過例子說明java代理的幾種實現(xiàn)方式,并比較它們之間的差異,文中通過代碼示例給大家介紹的非常詳細,對大家的學習或工作有一定的參考價值,需要的朋友可以參考下
    2023-12-12

最新評論

姚安县| 静宁县| 深州市| 兴山县| 宝山区| 泽普县| 衡山县| 灵丘县| 葫芦岛市| 昌吉市| 保康县| 南部县| 安达市| 堆龙德庆县| 舟山市| 汉阴县| 拜泉县| 南平市| 通城县| 安岳县| 阜城县| 南部县| 应城市| 茂名市| 新丰县| 广东省| 蒙城县| 大余县| 罗田县| 班戈县| 尼玛县| 炎陵县| 中方县| 邹城市| 华安县| 明水县| 阿瓦提县| 高陵县| 辰溪县| 鸡东县| 马尔康县|