Spring WebClient 的 basic auth 使用

本文介绍了Spring WebClient如何进行基本认证(basic auth),并强调了其同步和异步使用方式。作者分享了一个在测试中遇到的问题:由于将数组转换为Map导致的超出缓冲区限制的异常。通过调整接收数据类型为List,问题得以解决。提醒开发者,类似异常可能隐藏了类型匹配错误,需要仔细排查。
摘要由CSDN通过智能技术生成

接上一篇 RestTemplate的basic auth使用;

在spring5后官方带来了WebClient,说是以后替代RestTemplate了,不知道真假哈,既然有了新东西,就研究一下使用方法呗,也同时做个备忘笔记。

老规矩,先上结果,后说问题:

import org.springframework.http.HttpHeaders;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.List;
import java.util.Map;

public class WebClientRequest {

    private static WebClient webClient = WebClient.builder()
            .baseUrl("http://jsonplaceholder.typicode.com")
            .build();

    private static void getHttp() throws InterruptedException {

//同步阻塞处理
//返回单个对象的处理
        Mono<Map> result = webClient.get().uri("/posts/1").headers(headers -> headers.setBasicAuth("username", "password")).retrieve().bodyToM
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
是的,您可以在 Spring Cloud Gateway 中使用 WebClient 进行服务调用。WebClientSpring WebFlux 中的一个非阻塞式 HTTP 客户端,可以用于调用其他服务的 RESTful API。 下面是一个简单的示例,展示了如何在 Spring Cloud Gateway 中使用 WebClient 进行服务调用: 1. 首先,在您的 Spring Cloud Gateway 项目中添加以下依赖: ``` <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-webflux</artifactId> </dependency> ``` 2. 然后,在您的 Spring Cloud Gateway 配置类中,注入一个 WebClient 对象: ``` @Configuration public class GatewayConfig { @Bean public WebClient webClient() { return WebClient.builder().build(); } } ``` 3. 最后,在您的路由配置中,使用注入的 WebClient 对象进行服务调用: ``` @Configuration public class GatewayRoutesConfig { @Autowired private WebClient webClient; @Bean public RouteLocator customRouteLocator(RouteLocatorBuilder builder) { return builder.routes() .route("example", r -> r.path("/example") .uri("http://example.com")) .route("example-api", r -> r.path("/example-api/**") .filters(f -> f.rewritePath("/example-api/(?<path>.*)", "/${path}")) .uri("http://example.com")) .route("example-service", r -> r.path("/example-service/**") .filters(f -> f.rewritePath("/example-service/(?<path>.*)", "/${path}")) .uri("lb://example-service")) .route("example-service-webclient", r -> r.path("/example-service-webclient/**") .uri("http://example-service.com") .filter((exchange, chain) -> { URI uri = exchange.getRequest().getURI(); String path = uri.getPath().replace("/example-service-webclient", ""); return webClient .method(exchange.getRequest().getMethod()) .uri("http://example-service.com" + path) .headers(headers -> headers.addAll(exchange.getRequest().getHeaders())) .body(exchange.getRequest().getBody()) .exchange() .flatMap(clientResponse -> { ServerHttpResponse response = exchange.getResponse(); response.getHeaders().putAll(clientResponse.headers().asHttpHeaders()); response.setStatusCode(clientResponse.statusCode()); return response.writeWith(clientResponse.body(BodyExtractors.toDataBuffers())); }); })) .build(); } } ``` 在上面的示例中,我们注入了一个 WebClient 对象,并在路由配置中使用它进行服务调用。在 `example-service-webclient` 路由中,我们使用 `webClient` 对象发出了一个 HTTP 请求,并将响应写回到响应流中。需要注意的是,我们需要将请求的头部和请求体等信息都传递给 WebClient,以确保请求可以正确地被发送。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值