Springboot3.4.x中的RestClient 和 RestTemplate

前言

Springboot3.4.x发布之后,对于RestClient 和 RestTemplate,添加了支持http客户端配置的支持

RestClient等配置

http客户端类型

Springboot3.4.x新增配置spring.http.client.factory,支持对http客户端的配置,支持的类型有

java 复制代码
Apache HTTP Components (HttpComponentsClientHttpRequestFactory)

Jetty Client (JettyClientHttpRequestFactory)

Reactor Netty HttpClient (ReactorClientHttpRequestFactory)

JDK HttpClient (JdkClientHttpRequestFactory)

Simple JDK HttpURLConnection (SimpleClientHttpRequestFactory)

示例

新增一个controller类

typescript 复制代码
@Slf4j
@RestController
public class HttpController {

    @RequestMapping("/hello")
    public String hello() {
        RestClient restClient = RestClient.create();
        String result = restClient.get().uri("http://localhost:8019/test").retrieve().body(String.class);
        log.info("返回结果为:{{}}", result);
        return "success";
    }

    @GetMapping("/test")
    public String test() {
        log.info("info日志");
        return "hello";
    }


}

application.properties配置如下

ini 复制代码
server.port=8019
spring.profiles.active=dev
spring.application.name=demo-rest-client
jdk

配置如下

yaml 复制代码
spring:
  http:
    client:
      factory: jdk

访问

bash 复制代码
http://localhost:8019/hello

输出结果

http_components

这个需要加入httpclient5包

xml 复制代码
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents.client5/httpclient5 -->
<dependency>
    <groupId>org.apache.httpcomponents.client5</groupId>
    <artifactId>httpclient5</artifactId>
    <version>5.5</version>
</dependency>
bash 复制代码
http://localhost:8019/hello

输出结果

jetty
yaml 复制代码
spring:
  http:
    client:
      factory: jetty

pom.xml配置加入

xml 复制代码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <!-- 去除Tomcat容器 -->
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>
</dependency>


<!-- 增加Jetty容器 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jetty</artifactId>
</dependency>

访问

bash 复制代码
http://localhost:8019/hello

输出结果

simple
yaml 复制代码
spring:
  http:
    client:
      factory: simple

使用jdk默认的HttpURLConnection 访问

bash 复制代码
http://localhost:8019/hello

输出结果

总结

Springboot3版本迭代之后,提供了许多优秀的新属性,如果升级版本之后,有兴趣可以使用

相关推荐
万少1 小时前
Qoder sonus模型 20分钟,复刻 马斯克 飞天汽车
前端·后端·算法
夕除1 小时前
redis--主从还是集群,内存
java·spring·mybatis
白露与泡影1 小时前
Spring Boot 实现验证码:Hutool-captcha、AJ-Captcha、EasyCaptcha 到底怎么选?
java·spring boot·后端
seconp1 小时前
基于Spring Boot和Vue 3的智慧旅游网站:住宿预订、行程规划与千问接入
vue.js·spring boot·毕业设计·软件工程·课程设计·旅游·毕设
vx_bisheyuange1 小时前
基于SpringBoot的校园数字化图书馆系统
spring boot·毕业设计
小蒜学长1 小时前
基于Springboot+Vue的高校教师教学业绩综合管理系统的设计与开发(代码+数据库+LW)
java·数据库·spring boot·后端·教学业绩管理·三级权限
熊哈哈O_o1 小时前
千亿级时序数据:存储压缩、高速写入与时序超表技术
后端
Mikko71 小时前
Spring Boot 3.5.16 自带的 Tomcat 10.1.55 中了哪些 CVE?其中一条是修上一个漏洞时带进来的
java·spring boot·后端·安全·tomcat
十年Java程序媛2 小时前
Java 内部类全解|成员 / 静态 / 匿名内部类,理清和 Lambda 的区别
java·spring boot·面试
站大爷IP2 小时前
Python的字符串查找把我坑惨了,原来find和index的区别这么大
后端