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

Spring Boot如何使用httpcomponents實(shí)現(xiàn)http請(qǐng)求

 更新時(shí)間:2023年07月17日 10:24:08   作者:青銅愛(ài)碼士  
這篇文章主要介紹了Spring Boot使用httpcomponents實(shí)現(xiàn)http請(qǐng)求的示例代碼,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

基于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)義的方法

    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ù)源配置方法詳解

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

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

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

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

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

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

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

    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)

    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)多文件批量上傳

    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

    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é)

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

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

最新評(píng)論

正安县| 邵阳市| 互助| 汉沽区| 宁夏| 东平县| 河源市| 汉中市| 伊宁县| 姜堰市| 辰溪县| 启东市| 钦州市| 辽阳县| 凤城市| 南乐县| 南郑县| 中卫市| 延川县| 武义县| 蒲城县| 商洛市| 贡觉县| 游戏| 福建省| 衡南县| 彰化县| 元谋县| 乌鲁木齐市| 华容县| 江门市| 潞西市| 宁阳县| 克什克腾旗| 高陵县| 安新县| 马山县| 白山市| 临桂县| 鄯善县| 阳泉市|