带有新标头的 Spring WebClient 重试逻辑

编程入门 行业动态 更新时间:2024-10-08 22:50:36
本文介绍了带有新标头的 Spring WebClient 重试逻辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试使用 Spring WebClient 构建重试逻辑.我试图解决的问题非常简单.我正在调用 API 端点以获取一些值.如果 API 返回错误并显示 401 响应,那么我将不得不调用令牌服务并更新我的令牌并使用新令牌并进行相同的 API 调用.

I am trying to build a retry logic using Spring WebClient. The problem that I am trying to solve is very simple. I am calling an API endpoint to get some values. If the API returns an error with say 401 response, then I will have to make call to Token service and renew my token and use the new token and make the same API call.

一般的伪代码是

try { GET /locations data } catch(401 Unauthorized) { POST /token and get renew Token --> This is another WebClient API call With New Token call again GET /locations and return value } catch (Another Exception) { throw Application Error }

这是我正在尝试执行的 Spring 代码,但它看起来似乎不起作用.关于如何做的任何建议.

Here is the Spring code that I am trying to do and it does not look like it is working. Any suggestion on how to do it.

public List<Location> getLocations(final User user) { if (null == user) { throw new ApplicationException("User cannot be null"); } if (null == user.getHoneyWellLinkToken()) { throw new ApplicationException(String.format("%s has not linked the account with Honeywell", user.getUsername())); } List<Location> locations = getLocationsAPI(user).block(); return locations; } private Mono<List<Location>> getLocationsAPI(final User user) { String endpoint = config.getApi().getLocationsEndpoint() .concat("?apikey=") .concat(config.getCredentials().getClientId()); return WebClient.builder().baseUrl(endpoint) .build() .get() .headers(httpHeaders -> httpHeaders.setBearerAuth(user.getHoneyWellLinkToken().getAccessToken())) .retrieve() .bodyToFlux(Location.class) .collectList() .doOnError(err -> { WebClient.builder().baseUrl(endpoint) .build() .get() .headers(httpHeaders -> httpHeaders.setBearerAuth(honeywellService.renewToken(user).block().getHoneyWellLinkToken().getAccessToken())) .retrieve().bodyToFlux(Location.class); }); }

此代码托管在 GitHub github/reflexdemon/home-use/blob/main/src/main/java/io/vpv/homeuse/service/HoneywellThermostatService.java

This code is hosted on GitHub github/reflexdemon/home-use/blob/main/src/main/java/io/vpv/homeuse/service/HoneywellThermostatService.java

推荐答案

  • 使用 onErrorResume 而不是 doOnError
  • 更新令牌时不要block
  • private Mono<List<Location>> getLocationsAPI(final User user) { String endpoint = config.getApi().getLocationsEndpoint() .concat("?apikey=") .concat(config.getCredentials().getClientId()); return getLocations(endpoint, user) .onErrorResume(err -> honeywellService.renewToken(user) .flatMap(newUser -> getLocations(endpoint, newUser))); } private Mono<List<Location>> getLocations(String endpoint, User user) { return WebClient.builder() .baseUrl(endpoint) .build() .get() .headers(httpHeaders -> httpHeaders.setBearerAuth(user .getHoneyWellLinkToken() .getAccessToken())) .retrieve() .bodyToFlux(Location.class) .collectList(); }

    此外,最好使用单个 WebClient 实例,而不是为每个请求构建一个新实例.

    Also, it's a good idea to use a single WebClient instance instead of building a new one for each request.

更多推荐

带有新标头的 Spring WebClient 重试逻辑

本文发布于:2023-11-26 07:45:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1633240.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重试   逻辑   新标头   Spring   WebClient

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!