首页 > 其他分享 >在springboot 中使用Apache HttpClient 5的详细示例

在springboot 中使用Apache HttpClient 5的详细示例

时间:2024-07-12 11:12:01浏览次数:9  
标签:springboot 示例 springframework public apache org Apache import annotation

Apache HttpComponents Client 5.x 是 HttpClient 的最新版本,与 4.x 系列相比,5.x 系列进行了许多改进和重构,提供了更现代的 API 和更好的性能。
以下是使用步骤

步骤 1:添加依赖

在你的 pom.xml 文件中添加 Apache HttpClient 5.x 的依赖:

<dependency>
    <groupId>org.apache.httpcomponents.client5</groupId>
    <artifactId>httpclient5</artifactId>
    <version>5.1</version>
</dependency>

步骤 2:配置 HttpClient Bean

在你的 Spring Boot 应用程序中,配置一个 HttpClient 5.x 的 Bean。你可以在一个配置类中进行这个操作:

import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class HttpClientConfig {

    @Bean
    public CloseableHttpClient httpClient() {
        return HttpClients.createDefault();
    }
}

步骤 3:创建一个服务类来使用 HttpClient

创建一个服务类来使用 HttpClient 5.x 发起 HTTP 请求:

import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.classic.methods.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;

@Service
public class ApiService {

    @Autowired
    private CloseableHttpClient httpClient;

    public String getData(String url) throws IOException {
        HttpGet request = new HttpGet(url);
        try (CloseableHttpResponse response = httpClient.execute(request)) {
            return EntityUtils.toString(response.getEntity());
        }
    }
}

步骤 4:使用服务类

在你的控制器或其他服务类中注入 ApiService 并调用它的方法:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
public class ApiController {

    @Autowired
    private ApiService apiService;

    @GetMapping("/fetch-data")
    public String fetchData(@RequestParam String url) {
        try {
            return apiService.getData(url);
        } catch (IOException e) {
            e.printStackTrace();
            return "Error fetching data";
        }
    }
}

完整示例

综合以上步骤,下面是一个完整的 Spring Boot 应用示例,使用 Apache HttpClient 5.x:

import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.classic.methods.CloseableHttpResponse;
import org.apache.hc.core5.http.io.entity.EntityUtils;

import java.io.IOException;

@SpringBootApplication
public class HttpClientExampleApplication {

    public static void main(String[] args) {
        SpringApplication.run(HttpClientExampleApplication.class, args);
    }
}

@Configuration
class HttpClientConfig {

    @Bean
    public CloseableHttpClient httpClient() {
        return HttpClients.createDefault();
    }
}

@Service
class ApiService {

    @Autowired
    private CloseableHttpClient httpClient;

    public String getData(String url) throws IOException {
        HttpGet request = new HttpGet(url);
        try (CloseableHttpResponse response = httpClient.execute(request)) {
            return EntityUtils.toString(response.getEntity());
        }
    }
}

@RestController
class ApiController {

    @Autowired
    private ApiService apiService;

    @GetMapping("/fetch-data")
    public String fetchData(@RequestParam String url) {
        try {
            return apiService.getData(url);
        } catch (IOException e) {
            e.printStackTrace();
            return "Error fetching data";
        }
    }
}

在这个示例中,HttpClientConfig 配置类定义了一个 CloseableHttpClient Bean。ApiService 服务类使用这个 Bean 来发起 HTTP 请求,并在 ApiController 控制器中调用 ApiService 的方法来处理 /fetch-data 请求。

通过这种方式,你可以在 Spring Boot 应用程序中灵活地使用 Apache HttpClient 5.x 发起和处理 HTTP 请求。

标签:springboot,示例,springframework,public,apache,org,Apache,import,annotation
From: https://www.cnblogs.com/gongchengship/p/18297873

相关文章

  • php实名认证示例、实人认证接口
    随着互联网的高速发展,人们可以发表言论的渠道越来越多。网络平台不断汲取各地、各人、各时发表的各种信息。人们喜欢将信息发布到微博、知乎、天涯、豆瓣等等网络平台,逐步的,网络信息进入大爆炸时代。这些大量涌现的信息中难免掺杂着一些不良信息,比如:虚假信息、污言秽语、违法......
  • springboot快速整合任务
    springboot整合任务有很多种方法,下面以Quartz跟Task作为整合,快速把握。其中Task是比较常用以及我个人推荐,而且上手比较简单。Task技术整合spring根据定时任务的特征,将定时任务的开发简化到了极致。在springboot项目中使用也是同样的道理。只要设置一个定时任务告诉容器有,然后定......
  • Java毕业设计基于Vue+SpringBoot的电影院订票选座管理系统(代码+数据库+文档LW+运行成
    很多朋友发现后期找不到文章,收藏关注不迷路文章目录项目介绍技术介绍项目界面关键代码目录项目介绍在飞速发展的今天,网络已成为人们重要的交流平台。电影院每天都有大量的需要通过网络发布,为此,本人开发了一个基于B/S;浏览器/服务器;模式的电影院管理系统。该系......
  • Java毕业设计基于Vue+SpringBoot的畅游游戏销售平台(代码+数据库+文档LW+运行成功)
    很多朋友发现后期找不到文章,收藏关注不迷路文章目录项目介绍技术介绍项目界面关键代码目录项目介绍近些年来,随着科技的飞速发展,互联网的普及逐渐延伸到各行各业中,给人们生活带来了十分的便利,畅游游戏销售平台利用计算机网络实现信息化管理,使整个畅游游戏销售平......
  • Java毕业设计基于Vue+SpringBoot的爱看漫画小程序(代码+数据库+文档LW+运行成功)
    很多朋友发现后期找不到文章,收藏关注不迷路文章目录项目介绍技术介绍项目界面关键代码目录项目介绍相比于以前的传统手工管理方式,智能化的管理方式可以大幅降低爱看漫画的运营人员成本,实现了爱看漫画的标准化、制度化、程序化的管理,有效地防止了爱看漫画的随意管......
  • 我是如何从零到成为 Apache 顶级项目的 Committer
    最近收到了ApachePulsar和ApacheHertzBeat社区的邀请邮件,成为了这两个项目的Committer。一路走来我从最开始的打游击战的闲散人员到如今活跃在各个开源项目里的“老兵”,用现在流行的话来说Apache的这两个Committer就相当于是拿到了编制,进入了正规军。下面就分享一下......
  • 基于java+springboot+vue实现的在线教育系统(文末源码+Lw)111
    基于SpringBoot+Vue的实现的在线教育系统(源码+数据库+万字Lun文+流程图+ER图+结构图+演示视频+软件包)系统功能:本在线教育系统管理员功能有个人中心,用户管理,讲师管理,普通管理员管理,课程管理员管理,课程管理,课程分类管理,教师管理,名师管理,系统管理,订单管理。普通管理员和课程......
  • 基于java+springboot+vue实现的作业管理系统(文末源码+Lw)110
    基于SpringBoot+Vue的实现的作业管理系统(源码+数据库+万字Lun文+流程图+ER图+结构图+演示视频+软件包)功能描述:作业管理系统有管理员,教师,学生三个角色。教师和学生都可以进行注册然后再登录。学生可以修改自己的密码,查看和下载作业信息,并且可以提交自己写好的作业,并且可以......
  • SpringBoot2.6.13版本引入Swagger
    1.引入依赖<!--https://mvnrepository.com/artifact/io.springfox/springfox-swagger2--><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>3.0.0</version&g......
  • 【SpringBoot框架】-- 快速入门
    目录1.spring简介1.1springboot快速入门1.1.1开发步骤1.1.2创建项目2.springboot的特点3.配置文件种类4.读取配置文件中的内容4.1 @ConfigurationPropertie4.2  @Value5.profile多环境配置 6.springboot注册web组件7.springboot包扫描的原理8.spr......