当前位置: 首页 > news >正文

玉溪市住房和建设局公布网站海南网站建设报价方案

玉溪市住房和建设局公布网站,海南网站建设报价方案,网页制作总结报告,网站建设视频教程最新在 Java 开发中#xff0c;进行网络通信是常见的需求。WebClient 和 HttpClient 是两种常用的用于发送 HTTP 请求的工具。它们都具有相似的功能#xff0c;但在实现细节和用法上存在一些差异。本文将详细介绍 WebClient 和 HttpClient 的异同#xff0c;帮助您选择适合您项目…在 Java 开发中进行网络通信是常见的需求。WebClient 和 HttpClient 是两种常用的用于发送 HTTP 请求的工具。它们都具有相似的功能但在实现细节和用法上存在一些差异。本文将详细介绍 WebClient 和 HttpClient 的异同帮助您选择适合您项目需求的工具。 1. 引入依赖 首先我们需要在项目中引入相应的依赖。对于 WebClient我们可以使用 Spring WebFlux 提供的 spring-webflux 依赖。对于 HttpClient我们可以使用 Java 11 提供的 java.net.http 包无需额外引入依赖。 2. 发送 GET 请求 2.1 使用 WebClient WebClient client WebClient.create(); MonoString result client.get().uri(https://api.example.com/users).retrieve().bodyToMono(String.class); String response result.block(); System.out.println(response);2.2 使用 HttpClient HttpClient client HttpClient.newHttpClient(); HttpRequest request HttpRequest.newBuilder().uri(URI.create(https://api.example.com/users)).build(); HttpResponseString response client.send(request, HttpResponse.BodyHandlers.ofString()); System.out.println(response.body());3. 发送 POST 请求 3.1 使用 WebClient WebClient client WebClient.create(); MonoString result client.post().uri(https://api.example.com/users).bodyValue({ \name\: \John\, \age\: 30 }).retrieve().bodyToMono(String.class); String response result.block(); System.out.println(response);3.2 使用 HttpClient HttpClient client HttpClient.newHttpClient(); HttpRequest request HttpRequest.newBuilder().uri(URI.create(https://api.example.com/users)).header(Content-Type, application/json).POST(HttpRequest.BodyPublishers.ofString({ \name\: \John\, \age\: 30 })).build(); HttpResponseString response client.send(request, HttpResponse.BodyHandlers.ofString()); System.out.println(response.body());4. 配置超时时间 4.1 使用 WebClient WebClient client WebClient.builder().clientConnector(new ReactorClientHttpConnector(HttpClient.create().responseTimeout(Duration.ofSeconds(10)))).build();4.2 使用 HttpClient HttpClient client HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(10)).build();5. 异步请求 5.1 使用 WebClient WebClient client WebClient.create(); MonoString result client.get().uri(https://api.example.com/users).retrieve().bodyToMono(String.class); result.subscribe(response - System.out.println(response));5.2 使用 HttpClient HttpClient client HttpClient.newHttpClient(); HttpRequest request HttpRequest.newBuilder().uri(URI.create(https://api.example.com/users)).build(); client.sendAsync(request, HttpResponse.BodyHandlers.ofString()).thenApply(HttpResponse::body).thenAccept(System.out::println);6. 错误处理 6.1 使用 WebClient WebClient client WebClient.create(); MonoString result client.get().uri(https://api.example.com/users).retrieve().onStatus(HttpStatus::isError, response - Mono.error(new RuntimeException(Request failed))).bodyToMono(String.class); String response result.block(); System.out.println(response);6.2 使用 HttpClient HttpClient client HttpClient.newHttpClient(); HttpRequest request HttpRequest.newBuilder().uri(URI.create(https://api.example.com/users)).build(); HttpResponseString response client.send(request, HttpResponse.BodyHandlers.ofString()); if (response.statusCode() 400) {throw new RuntimeException(Request failed); } System.out.println(response.body());7. 性能和扩展性 7.1 WebClient 基于 Reactor 和 Netty支持非阻塞 I/O适用于高并发场景。集成了 Spring WebFlux 框架可以与其他 Spring 组件无缝协作。可以通过自定义 ExchangeFilterFunction 对请求和响应进行拦截和处理。 7.2 HttpClient Java 11 内置的标准库无需额外引入依赖。提供了更底层的 API可以更灵活地控制请求和处理响应。支持 HTTP/2性能较好。 8. 结论使用 WebClient 的优点 非阻塞、异步操作WebClient 基于响应式编程模型使用 Reactor 提供的 Mono 和 Flux 类型来表示异步结果。它允许你以非阻塞的方式发送和处理 HTTP 请求从而提高应用程序的性能和吞吐量。 简洁的 APIWebClient 提供了简洁而直观的 API通过链式调用可以方便地设置请求参数、发送请求和处理响应。它的 API 设计使得代码易于阅读和维护。 内置的异常处理WebClient 提供了丰富的异常处理机制可以通过 onStatus() 方法处理不同的 HTTP 状态码和错误情况。这使得处理错误和异常变得更加方便和灵活。 集成 Spring 生态系统WebClient 是 Spring Framework 的一部分与其他 Spring 组件如 Spring Boot无缝集成。它可以与 Spring 的其他功能如响应式 Web 框架配合使用提供全面的开发体验。 9. WebClient 工具类及使用示例 当使用 WebClient 来进行 HTTP 请求时可以创建一个工具类来封装常用的请求操作。下面是一个示例的 WebClient 工具类其中包含了 GET、POST、PUT 和 DELETE 方法的实现 import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.web.reactive.function.BodyInserters; import org.springframework.web.reactive.function.client.WebClient; import reactor.core.publisher.Mono;public class WebClientUtils {private WebClient webClient;public WebClientUtils(String baseUrl) {this.webClient WebClient.builder().baseUrl(baseUrl).defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE).build();}public T MonoT get(String uri, ClassT responseType) {return webClient.get().uri(uri).retrieve().bodyToMono(responseType);}public T MonoT post(String uri, Object request, ClassT responseType) {return webClient.post().uri(uri).body(BodyInserters.fromValue(request)).retrieve().bodyToMono(responseType);}public T MonoT put(String uri, Object request, ClassT responseType) {return webClient.put().uri(uri).body(BodyInserters.fromValue(request)).retrieve().bodyToMono(responseType);}public T MonoT delete(String uri, ClassT responseType) {return webClient.delete().uri(uri).retrieve().bodyToMono(responseType);} }使用示例 public class Main {public static void main(String[] args) {WebClientUtils webClientUtils new WebClientUtils(https://api.example.com);// 发起 GET 请求webClientUtils.get(/users/1, User.class).subscribe(user - System.out.println(GET response: user));// 发起 POST 请求User newUser new User(John, Doe);webClientUtils.post(/users, newUser, User.class).subscribe(user - System.out.println(POST response: user));// 发起 PUT 请求User updatedUser new User(Jane, Doe);webClientUtils.put(/users/1, updatedUser, User.class).subscribe(user - System.out.println(PUT response: user));// 发起 DELETE 请求webClientUtils.delete(/users/1, Void.class).subscribe(response - System.out.println(DELETE response: response));} }请注意上述代码中的 User 类是一个自定义的 POJO 类用于表示用户信息。您需要根据实际情况进行相应的定义和调整。 10. 结论 WebClient 和 HttpClient 都是常用的发送 HTTP 请求的工具具有相似的功能但在实现细节和用法上存在一些差异。如果您使用 Spring 框架且对性能要求较高可以选择 WebClient。如果您使用的是 Java 11 或更高版本并且对底层控制和灵活性有要求可以选择 HttpClient。 以上是对 WebClient 和 HttpClient 的异同的详细讲解。希望对大家有所帮助 公众号请关注 果酱桑, 一起学习,一起进步!
http://www.w-s-a.com/news/146878/

相关文章:

  • 网站在那里备案成都成华区网站建设
  • 做网站选哪家好搜索引擎优化的目标体系包括哪些
  • 做数据可视化的网站ppt2016是制作网页的软件
  • 济宁市建设工程质量监督站网站徐州网站优化推广
  • 北京网站设计多少钱php做商品网站
  • 能打开的网站你了解的彩票网站开发dadi163
  • 手机做网站价格优秀企业网站建设价格
  • 电商网站建设企业做网站的客户多吗
  • 有做思维图的网站吗西安建设市场诚信信息平台网站
  • 网站建设求职具备什么30岁学网站开发
  • 官方网站minecraft北京低价做网站
  • 网站建设报价兴田德润机械加工网络接单
  • 免费的推广网站安卓app制作平台
  • 长春火车站附近美食建设信用卡银行积分兑换商城网站
  • 网站提交网址如何备份wordpress网页
  • 龙腾盛世网站建设医院管理系统
  • 网站切换图片做背景怎么写外贸营销邮件主题一般怎么写
  • 基于html5的网站开发wordpress主题工具
  • php网站开发的成功经历公司网站现状
  • 软件发布网站源码中国企业公示信息网
  • flash 的网站网站型销售怎么做
  • 营销型网站单页网站的域名和密码
  • 建网站保定seo自动发布外链工具
  • 做公众号关注网站做课件用这15大网站
  • 怎么制作公司自己网站店铺设计软件手机版
  • 深圳网站关键词优化公司哪家好怎么选择锦州网站建设
  • 标准网站优势项目合作网站
  • 无人机东莞网站建设wordpress站群管理破解版
  • 深圳企业官网网站建设教育培训学校
  • 医疗网站建设及优化西安网站建设开发公司