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 小时前
C++模板元编程从入门到精通
后端
_風箏1 小时前
Java【代码 14】一个用于判断磁盘空间和分区表是否需要清理的工具类
后端
_風箏1 小时前
Java【代码 13】前端动态添加一条记后端使用JDK1.8实现map对象根据key的部分值进行分组(将map对象封装成指定entity对象)
后端
_風箏1 小时前
Java【代码 12】判断一个集合是否包含另一个集合中的一个或多个元素 retainAll() 及其他方法
后端
angushine1 小时前
logstash采集springboot微服务日志
spring boot·微服务·linq
Java中文社群1 小时前
Coze开源版?别吹了!
人工智能·后端·开源
懂得节能嘛.1 小时前
【SpringAI实战】ChatPDF实现RAG知识库
java·后端·spring
探索java2 小时前
Spring 解析 XML 配置文件的过程(从读取 XML 到生成 BeanDefinition)
xml·java·spring·xmlbeanfactory
站大爷IP2 小时前
Python爬虫库性能与选型实战指南:从需求到落地的全链路解析
后端
武昌库里写JAVA2 小时前
「mysql」Mac osx彻底删除mysql
vue.js·spring boot·毕业设计·layui·课程设计