怎么制作微信网站,泗县住房和城乡建设局网站,无锡市建设工程质量监督站网站,性价比高seo的排名优化更多SpringBoot3内容请关注我的专栏#xff1a;《SpringBoot3》 期待您的点赞#x1f44d;收藏⭐评论✍ 重学SpringBoot3-WebClient配置与使用详解 1. 简介2. 环境准备2.1 依赖配置 3. WebClient配置3.1 基础配置3.2 高级配置3.3 retrieve()和exchange()区别 4. 使用示例4.1 … 更多SpringBoot3内容请关注我的专栏《SpringBoot3》 期待您的点赞收藏⭐评论✍ 重学SpringBoot3-WebClient配置与使用详解 1. 简介2. 环境准备2.1 依赖配置 3. WebClient配置3.1 基础配置3.2 高级配置3.3 retrieve()和exchange()区别 4. 使用示例4.1 基本请求操作4.2 处理复杂响应4.3 高级用法 5. 最佳实践6. 注意事项7. 与RestTemplate对比8. 总结参考资料 1. 简介 
WebClient是Spring 5引入的响应式Web客户端用于执行HTTP请求。相比传统的RestTemplateWebClient提供了非阻塞、响应式的方式来处理HTTP请求是Spring推荐的新一代HTTP客户端工具。本文将详细介绍如何在SpringBoot 3.x中配置和使用WebClient。 
2. 环境准备 
2.1 依赖配置 
在 pom.xml中添加必要的依赖 parentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion3.2.10/versionrelativePath/ !-- lookup parent from repository --/parentdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-webflux/artifactId/dependency3. WebClient配置 
3.1 基础配置 
Configuration
public class WebClientConfig {Beanpublic WebClient webClient() {return WebClient.builder().baseUrl(https://echo.apifox.com).defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE).defaultHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE).build();}
}3.2 高级配置 
package com.coderjia.boot3webflux.config;import io.netty.channel.ChannelOption;
import io.netty.handler.timeout.ReadTimeoutHandler;
import io.netty.handler.timeout.WriteTimeoutHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
import org.springframework.web.reactive.function.client.ExchangeFilterFunction;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;
import reactor.netty.http.client.HttpClient;
import reactor.netty.resources.ConnectionProvider;import java.time.Duration;/*** author CoderJia* create 2024/12/3 下午 09:42* Description**/
Slf4j
Configuration
public class WebClientConfig {Beanpublic WebClient webClient() {// 配置HTTP连接池ConnectionProvider provider  ConnectionProvider.builder(custom).maxConnections(500).maxIdleTime(Duration.ofSeconds(20)).build();// 配置HTTP客户端HttpClient httpClient  HttpClient.create(provider).option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 5000).responseTimeout(Duration.ofSeconds(5)).doOnConnected(conn -conn.addHandlerLast(new ReadTimeoutHandler(5)).addHandlerLast(new WriteTimeoutHandler(5)));// 构建WebClient实例return WebClient.builder().clientConnector(new ReactorClientHttpConnector(httpClient)).baseUrl(https://echo.apifox.com).defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE).defaultHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE)// 添加请求日志记录功能.filter(ExchangeFilterFunction.ofRequestProcessor(clientRequest - {log.debug(Request: {} {},clientRequest.method(),clientRequest.url());return Mono.just(clientRequest);}))// 添加响应日志记录功能.filter(ExchangeFilterFunction.ofResponseProcessor(clientResponse - {log.debug(Response status: {},clientResponse.statusCode());return Mono.just(clientResponse);})).build();}
}3.3 retrieve()和exchange()区别 
在使用 WebClient 进行 HTTP 请求时retrieve() 和 exchange() 方法都可以用来处理响应但它们有不同的用途和行为。以下是它们的主要区别 retrieve() 
用途retrieve() 方法用于简化响应处理特别是当你只需要响应体时。自动错误处理retrieve() 会自动处理 HTTP 错误状态码例如 4xx 和 5xx并抛出 WebClientResponseException 及其子类。返回值通常用于直接获取响应体例如 bodyToMono(String.class) 或 bodyToFlux(String.class)。适用场景适用于大多数常见的请求处理场景特别是当你不需要手动处理响应状态码时。 
exchange() 
用途exchange() 方法提供了更底层的控制允许你手动处理响应包括响应状态码和响应头。手动错误处理exchange() 不会自动处理 HTTP 错误状态码你需要手动检查响应状态码并进行相应的处理。返回值返回 ClientResponse 对象你可以从中提取响应状态码、响应头和响应体。适用场景适用于需要手动处理响应状态码或响应头的复杂场景。 
示例对比 
retrieve() 
public MonoJSONObject get(String q1) {return webClient.get().uri(uriBuilder - uriBuilder.path(/get).queryParam(q1, q1).build()).accept(MediaType.APPLICATION_JSON).retrieve().bodyToMono(JSONObject.class);
}exchange() 
public MonoJSONObject get(String q1) {return webClient.get().uri(uriBuilder - uriBuilder.path(/get).queryParam(q1, q1).build()).accept(MediaType.APPLICATION_JSON).exchangeToMono(response - {if (response.statusCode().is2xxSuccessful()) {return response.bodyToMono(JSONObject.class);} else {return Mono.error(new RuntimeException(Request failed with status code:   response.statusCode()));}});
}4. 使用示例 
4.1 基本请求操作 
package com.coderjia.boot3webflux.service;import com.alibaba.fastjson.JSONObject;
import jakarta.annotation.Resource;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Mono;/*** author CoderJia* create 2024/12/3 下午 10:22* Description**/
Service
public class ApiService {Resourceprivate WebClient webClient;// GET请求public MonoJSONObject get(String q1) {return webClient.get().uri(uriBuilder - uriBuilder.path(/get).queryParam(q1, q1).build()).accept(MediaType.APPLICATION_JSON).retrieve().bodyToMono(JSONObject.class);}// POST请求public MonoJSONObject post(JSONObject body) {return webClient.post().uri(/post).bodyValue(body).retrieve().bodyToMono(JSONObject.class);}// PUT请求public MonoJSONObject put(String q1, JSONObject JSONObject) {return webClient.put().uri(uriBuilder - uriBuilder.path(/put).queryParam(q1, q1).build()).bodyValue(JSONObject).retrieve().bodyToMono(JSONObject.class);}// DELETE请求public MonoJSONObject delete(String q1) {return webClient.delete().uri(uriBuilder - uriBuilder.path(/delete).queryParam(q1, q1).build()).retrieve().bodyToMono(JSONObject.class);}
}效果展示 4.2 处理复杂响应 
Service
public class ApiService {// 获取列表数据public FluxJSONObject getAllUsers() {return webClient.get().uri(/users).retrieve().bodyToFlux(JSONObject.class);}// 处理错误响应public MonoJSONObject getUserWithErrorHandling(Long id) {return webClient.get().uri(/users/{id}, id).retrieve().onStatus(HttpStatusCode::is4xxClientError, clientResponse - Mono.error(new RuntimeException(客户端错误))).onStatus(HttpStatusCode::is5xxServerError, clientResponse - Mono.error(new RuntimeException(服务器错误))).bodyToMono(JSONObject.class);}// 使用exchange()方法获取完整响应public MonoResponseEntityJSONObject getUserWithFullResponse(Long id) {return webClient.get().uri(/users/{id}, id).accept(MediaType.APPLICATION_JSON).exchange().flatMap(response - response.toEntity(JSONObject.class));}
}4.3 高级用法 
Service
public class ApiService {// 带请求头的请求public MonoJSONObject getUserWithHeaders(Long id, String token) {return webClient.get().uri(/users/{id}, id).header(Authorization, Bearer   token).retrieve().bodyToMono(JSONObject.class);}// 带查询参数的请求public FluxJSONObject searchUsers(String name, int age) {return webClient.get().uri(uriBuilder - uriBuilder.path(/users/search).queryParam(name, name).queryParam(age, age).build()).retrieve().bodyToFlux(JSONObject.class);}// 文件上传public MonoString uploadFile(FilePart filePart) {return webClient.post().uri(/upload).contentType(MediaType.MULTIPART_FORM_DATA).body(BodyInserters.fromMultipartData(file, filePart)).retrieve().bodyToMono(String.class);}
}5. 最佳实践 合理使用响应式类型 使用 Mono 用于单个对象使用 Flux 用于集合数据注意背压处理  错误处理  public MonoJSONObject getUserWithRetry(Long id) {return webClient.get().uri(/users/{id}, id).retrieve().bodyToMono(JSONObject.class).retryWhen(Retry.backoff(3, Duration.ofSeconds(1))).timeout(Duration.ofSeconds(5)).onErrorResume(TimeoutException.class,e - Mono.error(new RuntimeException(请求超时)));}资源管理 使用连接池设置适当的超时时间实现优雅关闭  
6. 注意事项 
WebClient 是非阻塞的需要注意响应式编程的特性合理配置连接池和超时参数在生产环境中实现适当的错误处理和重试机制注意内存使用特别是处理大量数据时 
7. 与RestTemplate对比 
特性WebClientRestTemplate编程模型响应式、非阻塞同步、阻塞性能更好一般资源利用更高效一般学习曲线较陡平缓适用场景高并发、响应式系统简单应用、传统系统 
8. 总结 
WebClient 作为 Spring 推荐的新一代 HTTP 客户端提供了强大的响应式编程能力和更好的性能。虽然相比 RestTemplate 有一定的学习曲线但在现代微服务架构中其带来的好处远超过学习成本。建议在新项目中优先考虑使用WebClient特别是在需要处理高并发请求的场景下。 
参考资料 
Spring WebClient官方文档Spring Boot官方文档Project Reactor文档 文章转载自: http://www.morning.ktskc.cn.gov.cn.ktskc.cn http://www.morning.qmmfr.cn.gov.cn.qmmfr.cn http://www.morning.mnrqq.cn.gov.cn.mnrqq.cn http://www.morning.kqnwy.cn.gov.cn.kqnwy.cn http://www.morning.glxmf.cn.gov.cn.glxmf.cn http://www.morning.cmhkt.cn.gov.cn.cmhkt.cn http://www.morning.rwqk.cn.gov.cn.rwqk.cn http://www.morning.qbwtb.cn.gov.cn.qbwtb.cn http://www.morning.qggm.cn.gov.cn.qggm.cn http://www.morning.qfkdt.cn.gov.cn.qfkdt.cn http://www.morning.jfcbz.cn.gov.cn.jfcbz.cn http://www.morning.qftzk.cn.gov.cn.qftzk.cn http://www.morning.nlqgb.cn.gov.cn.nlqgb.cn http://www.morning.rrcxs.cn.gov.cn.rrcxs.cn http://www.morning.egmux.cn.gov.cn.egmux.cn http://www.morning.cjnfb.cn.gov.cn.cjnfb.cn http://www.morning.fdfsh.cn.gov.cn.fdfsh.cn http://www.morning.lonlie.com.gov.cn.lonlie.com http://www.morning.qnsmk.cn.gov.cn.qnsmk.cn http://www.morning.llthz.cn.gov.cn.llthz.cn http://www.morning.pzjrm.cn.gov.cn.pzjrm.cn http://www.morning.wcyr.cn.gov.cn.wcyr.cn http://www.morning.pthmn.cn.gov.cn.pthmn.cn http://www.morning.rccbt.cn.gov.cn.rccbt.cn http://www.morning.gcthj.cn.gov.cn.gcthj.cn http://www.morning.dmchips.com.gov.cn.dmchips.com http://www.morning.pjfmq.cn.gov.cn.pjfmq.cn http://www.morning.snjpj.cn.gov.cn.snjpj.cn http://www.morning.enjoinfo.cn.gov.cn.enjoinfo.cn http://www.morning.mhrzd.cn.gov.cn.mhrzd.cn http://www.morning.twdwy.cn.gov.cn.twdwy.cn http://www.morning.mnpdy.cn.gov.cn.mnpdy.cn http://www.morning.ghwtn.cn.gov.cn.ghwtn.cn http://www.morning.jwtwf.cn.gov.cn.jwtwf.cn http://www.morning.stwxr.cn.gov.cn.stwxr.cn http://www.morning.nkjjp.cn.gov.cn.nkjjp.cn http://www.morning.qsy38.cn.gov.cn.qsy38.cn http://www.morning.jzsgn.cn.gov.cn.jzsgn.cn http://www.morning.mplld.cn.gov.cn.mplld.cn http://www.morning.zmqb.cn.gov.cn.zmqb.cn http://www.morning.tpqzs.cn.gov.cn.tpqzs.cn http://www.morning.pjtnk.cn.gov.cn.pjtnk.cn http://www.morning.ttryd.cn.gov.cn.ttryd.cn http://www.morning.tongweishi.cn.gov.cn.tongweishi.cn http://www.morning.brfxt.cn.gov.cn.brfxt.cn http://www.morning.ltpph.cn.gov.cn.ltpph.cn http://www.morning.srmdr.cn.gov.cn.srmdr.cn http://www.morning.nstml.cn.gov.cn.nstml.cn http://www.morning.cnfjs.cn.gov.cn.cnfjs.cn http://www.morning.ailvturv.com.gov.cn.ailvturv.com http://www.morning.myfwb.cn.gov.cn.myfwb.cn http://www.morning.wdpt.cn.gov.cn.wdpt.cn http://www.morning.tdwjj.cn.gov.cn.tdwjj.cn http://www.morning.jwsrp.cn.gov.cn.jwsrp.cn http://www.morning.jfjpn.cn.gov.cn.jfjpn.cn http://www.morning.ljzgf.cn.gov.cn.ljzgf.cn http://www.morning.jgnjl.cn.gov.cn.jgnjl.cn http://www.morning.rdbj.cn.gov.cn.rdbj.cn http://www.morning.lhjmq.cn.gov.cn.lhjmq.cn http://www.morning.lxctl.cn.gov.cn.lxctl.cn http://www.morning.jbfjp.cn.gov.cn.jbfjp.cn http://www.morning.xsjfk.cn.gov.cn.xsjfk.cn http://www.morning.kmcfw.cn.gov.cn.kmcfw.cn http://www.morning.wxccm.cn.gov.cn.wxccm.cn http://www.morning.wcjk.cn.gov.cn.wcjk.cn http://www.morning.nhrkl.cn.gov.cn.nhrkl.cn http://www.morning.rcww.cn.gov.cn.rcww.cn http://www.morning.gjsjt.cn.gov.cn.gjsjt.cn http://www.morning.trlhc.cn.gov.cn.trlhc.cn http://www.morning.rbkml.cn.gov.cn.rbkml.cn http://www.morning.gglhj.cn.gov.cn.gglhj.cn http://www.morning.nlrxh.cn.gov.cn.nlrxh.cn http://www.morning.lxkhx.cn.gov.cn.lxkhx.cn http://www.morning.rbkml.cn.gov.cn.rbkml.cn http://www.morning.inheatherskitchen.com.gov.cn.inheatherskitchen.com http://www.morning.nrydm.cn.gov.cn.nrydm.cn http://www.morning.kbdjn.cn.gov.cn.kbdjn.cn http://www.morning.qjxkx.cn.gov.cn.qjxkx.cn http://www.morning.ymwrs.cn.gov.cn.ymwrs.cn http://www.morning.kmqlf.cn.gov.cn.kmqlf.cn