- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个使用 Springboot Resttemplate 的 springboot 项目。我们已经从 1.5.3 迁移到 springboot 2.0.1,我们正在努力其余调用使用 WebClient 异步调用。我们曾经使用 Resttemplate 处理接收到的字符串,如下所示。但是 WebClient 只返回Mono 或 Flux 中的数据。如何获取字符串形式的数据。已经尝试过 block() 方法,但它执行异步调用。
@Retryable(maxAttempts = 4, value = java.net.ConnectException.class,
backoff = @Backoff(delay = 3000, multiplier = 2))
public Mono<String> getResponse(String url) {
return webClient.get().uri(urlForCurrent).accept(APPLICATION_JSON)
.retrieve()
.bodyToMono(String.class);
}
使用 RestTemplate 呈现数据流
Controller .java
@RequestMapping(value = traffic/, method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
public String getTraffic(@RequestParam("place") String location) throws InterruptedException, ExecutionException {
String trafficJSON = Provider.getTrafficJSON(location)
return trafficJSON;
}
提供者.java
public String getTrafficJSON(String location) {
String url = ----;
ResponseEntity<String> response = dataFetcher.getResponse(url);
/// RESPONSEBODY IS READ AS STRING AND IT NEEDS TO BE PROCESSED
if (null != response {
return parser.transformJSON(response.getBody(), params);
}
return null;
}
DataFetcher.java
@Retryable(maxAttempts = 4,
value = java.net.ConnectException.class,
backoff = @Backoff(delay = 3000, multiplier = 2))
public ResponseEntity<String> getResponse(String url) {
/* ----------------------- */
return getRestTemplate().getForEntity(urlForCurrent, String.class);
}
最佳答案
由于存在很多误解,所以在这里我要澄清一些事情。
Spring 已经正式声明 RestTemplate
处于维护模式
所以如果可以的话,如果你想成为 future 的证明,请使用 WebClient
可能。
如 RestTemplate API 中所述
NOTE: As of 5.0 this class is in maintenance mode, with only minor requests for changes and bugs to be accepted going forward. Please, consider using the
org.springframework.web.reactive.client.WebClient
which has a more modern API and supports sync, async, and streaming scenarios.
非 react 性应用
如果您的应用程序是非 react 性应用程序(不向调用客户端返回通量或单声道),您必须做的是在需要该值时使用 block()
。您当然可以在您的应用程序内部使用 Mono
或 Flux
但最后您必须调用 block()
来获取您想要的具体值需要返回调用客户端。
非响应式应用程序使用例如 tomcat
、undertow
作为底层服务器实现,它遵循 servlet 规范,因此它将为每个请求分配 1 个线程,因此您不会获得使用响应式(Reactive)应用程序获得的性能提升。
响应式应用
另一方面,如果您有一个响应式(Reactive)应用程序,则在任何情况下都不应在您的应用程序中调用 block()
。阻塞正是它所说的,它会阻塞一个线程并阻塞该线程的执行直到它可以继续,这在 react 世界中是不好的。
您也不应该在您的应用程序中调用订阅
,除非您的应用程序是响应的最终消费者。例如,如果您正在调用一个 api 来获取数据并写入您的应用程序连接到的数据库。您的后端应用程序是最终消费者。如果外部客户端正在调用您的后端(例如 react 、角度应用程序、移动客户端等),则外部客户端是最终消费者,并且是订阅者。不是你。
这里的底层默认服务器实现是一个 netty
服务器,它是一个非 servlet、基于事件的服务器,不会为每个请求分配一个线程,服务器本身就是线程不可知论者,任何可用的线程都将在任何请求期间随时处理任何事情。
webflux documentation明确指出 servlet 3.1+ 支持的服务器 tomcat 和 jetty 可以与 webflux 以及非 serlet 服务器 netty 和 undertow 一起使用。
我怎么知道我有什么应用程序?
Spring 声明,如果类路径中同时有 spring-web
和 spring-webflux
,应用程序将优先使用 spring-web
和默认启动一个带有底层 tomcat 服务器的非 react 性应用程序。
如果需要作为 spring 状态,可以手动覆盖此行为。
Adding both
spring-boot-starter-web
andspring-boot-starter-webflux
modules in your application results in Spring Boot auto-configuring Spring MVC, not WebFlux. This behavior has been chosen because many Spring developers addspring-boot-starter-webflux
to their Spring MVC application to use the reactive WebClient. You can still enforce your choice by setting the chosen application type toSpringApplication.setWebApplicationType(WebApplicationType.REACTIVE)
.
The “Spring WebFlux Framework”
那么如何根据题目提供的代码实现WebClient呢?
@Retryable(maxAttempts = 4,
value = java.net.ConnectException.class,
backoff = @Backoff(delay = 3000, multiplier = 2))
public Mono<String> getResponse(String url) {
return webClient.get()
.uri(url)
.exchange()
.flatMap(response -> response.toEntity(String.class));
}
我会说这是最简单、干扰最少的实现。你当然需要在 @Bean
中构建一个合适的 webclient 并将其 Autowiring 到它的类中。
关于java - Spring 启动 : How to use WebClient instead of RestTemplate for Performing Non blocking and Asynchronous calls,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57355725/
我正在编写一个类库来在我无法控制的站点上执行操作。该网站正在接受表单帖子作为输入。 谁能告诉我这两种方法除了上传数据的形式之外是否有区别? System.Net.WebClient.Uploa
用于工作的代码。 有问题的网址是 https://yobit.net/api/3/info 它适用于 IE。它曾经与 webclient 一起使用。它现在在 webclient 中不起作用。我想知道问
因此,我将我的 WebClient 包装在一个 using 语句中。但是我突然想知道,如果我的对象实现了 IDisposable 并且包装在 using 语句中,我是否需要取消订阅事件? 下面是我当前
我正在 VS15 测试版中工作并尝试使用 WebClient。虽然 System.Net 被引用,并且智能感知建议 WebClient 类可用,但在构建时我收到以下错误: The type or na
我想知道是否可以将 cookie 从一个 Web 客户端复制到另一个 Web 客户端。 原因 我正在使用并行 Web 请求,它会在每个新线程上创建 Web 客户端的新实例。 问题 信息敏感,需要使用p
我正在尝试使用 WebClient,但它给我错误,所以我检查了几个论坛(包括这个),他们告诉我把它放在哪里 在文件的顶部: using System.Net 在我想使用 WebClient 的地方之后
我正在尝试使用 WebClient 实现以下场景。使用 RestTemplate 很简单,但我不能再这样做了。 伪java代码中Spring Controller 的相关部分: Mono t1 = w
我正在使用 Spring WebClient 调用休息服务。如下所述的 post 调用代码。 Mono response = client.post()
正在尝试使用 WebClient在 Blazor 项目中。 得到以下错误: 在 blazor.webassembly.js:1 WASM: System.Net.WebException: An ex
我正在使用 ASP.NET Core 并尝试将文件下载到绝对路径。 但我遇到的问题是文件总是被下载到项目目录,文件名本身得到整个路径的名称。 我的代码: string path = @"C:\User
我需要自动化涉及使用登录表单的网站的流程。我需要在登录页面之后的页面中捕获一些数据。 我知道如何对普通页面进行屏幕抓取,但不知道如何抓取安全站点背后的页面。 这可以通过 .NET WebClient
我正在尝试逐步下载一系列序列化数据。目标是从服务器发送一个大块,并在下载时在客户端对其进行部分处理。 我正在使用 System.Net.WebClient 类并将其 AllowReadStreamBu
我在 Windows 桌面应用程序上使用此代码来获取组合框的值,之后我需要选择哪个组合框将使用 JavaScript 使用新信息更新页面 private WebBrowser withEventsFi
我正在尝试通过 C# 代码获取网站的 HTML 源代码。当我使用 Windows 身份验证访问站点时,以下代码有效: using (WebClient client = new WebClient()
我只是使用WebClient.DownloadString(),但速度慢得惊人。最大速度为 40kbs 我尝试将 WebClient.Proxy 设置为 null,但这不起作用,而且我还没有达到最大互
为了利用新的 WebClient API,我在我的 Intellij 项目中包含了 spring-webflux。 dependencies { implementation 'org.spr
我已经开始使用 WebClient,并使用过滤器方法添加请求/响应日志记录: WebClient.builder() .baseUrl(properties.getEndpoint())
我正在使用 WebClient.DownloadFile 将图像下载到本地存储库,如下所示: WebClient myWC = new WebClient();
我尝试使用网络客户端非阻塞方法验证验证码响应。所以它的工作,但我需要我的方法返回 boolean 值而不是异常。我如何从订阅返回值? webClient
这个问题已经有答案了: What does a "Cannot find symbol" or "Cannot resolve symbol" error mean? (18 个回答) 已关闭 3 年
我是一名优秀的程序员,十分优秀!