
本文档将向您展示如何向其他服务执行 HTTP 请求。
您将构建一个 Vert.x 应用程序,它使用 https://icanhazdadjoke.com/api 的 API,并每 3 秒显示一个新的笑话
该应用程序包含在一个单独的 JokeVerticle
类中。
文本编辑器或 IDE
Java 11 或更高版本
Maven 或 Gradle
此项目的代码包含功能等效的 Maven 和 Gradle 构建文件。
以下是您应该使用的 pom.xml
文件的内容
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>io.vertx.howtos</groupId>
<artifactId>http-client-howto</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<vertx.version>5.0.0.CR2</vertx.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-stack-depchain</artifactId>
<version>${vertx.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-core</artifactId>
</dependency>
<dependency>
<groupId>io.vertx</groupId>
<artifactId>vertx-web-client</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.13.0</version>
<configuration>
<release>11</release>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.5.0</version>
<configuration>
<mainClass>io.vertx.howtos.httpclient.JokeVerticle</mainClass>
</configuration>
</plugin>
</plugins>
</build>
</project>
假设您使用带有 Kotlin DSL 的 Gradle,您的 build.gradle.kts
文件应如下所示
build.gradle.kts
plugins {
java
application
}
repositories {
mavenCentral()
}
dependencies {
val vertxVersion = "5.0.0.CR2"
implementation("io.vertx:vertx-core:${vertxVersion}")
implementation("io.vertx:vertx-web-client:${vertxVersion}")
}
application {
mainClass = "io.vertx.howtos.httpclient.JokeVerticle"
}
与 Vert.x 核心 API 中更底层的 API 相比,Vert.x Web 客户端极大地简化了 HTTP 请求的创建。WebClient
类可在 io.vertx:vertx-web-client
工件中找到。
要获取新笑话,我们需要向 https://icanhazdadjoke.com/api 发送 HTTP GET 请求。每 3 秒执行一次此操作,我们将简单地使用 Vert.x 周期性定时器。
该 API 返回简单的 JSON 对象。我们可以使用像 curl
这样的命令行工具进行测试
$ curl -H "Accept: application/json" https://icanhazdadjoke.com/ {"id":"IJBAsrrzPmb","joke":"What do you call a cow with two legs? Lean beef.","status":200}
以下是 JokeVerticle
类的代码
package io.vertx.howtos.httpclient;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Vertx;
import io.vertx.core.http.HttpResponseExpectation;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.web.client.HttpRequest;
import io.vertx.ext.web.client.WebClient;
import io.vertx.ext.web.codec.BodyCodec;
public class JokeVerticle extends AbstractVerticle {
private HttpRequest<JsonObject> request;
@Override
public void start() {
request = WebClient.create(vertx) // (1)
.get(443, "icanhazdadjoke.com", "/") // (2)
.ssl(true) // (3)
.putHeader("Accept", "application/json") // (4)
.as(BodyCodec.jsonObject()); // (5)
vertx.setPeriodic(3000, id -> fetchJoke());
}
private void fetchJoke() {
request.send()
.expecting(HttpResponseExpectation.SC_OK) // (6)
.onSuccess(result -> {
System.out.println(result.body().getString("joke")); // (7)
System.out.println("🤣");
System.out.println();
})
.onFailure(e -> System.out.println("No joke fetched: " + e.getMessage()));
}
public static void main(String[] args) {
Vertx vertx = Vertx.vertx();
vertx.deployVerticle(new JokeVerticle()).await();
}
}
获取一个附加到当前 Vert.x 实例的 WebClient
。
对路径 /
向主机 icanhazdadjoke.com
的 HTTP GET 请求,端口为 443
(HTTPS)。
不要忘记启用 SSL 加密。
明确指出我们需要 JSON 数据。
响应将自动转换为 JSON。
我们期望 HTTP 200 状态码,否则响应将失败。
响应体是一个 JSON 对象,我们将其结果写入控制台。
JokeVerticle
已经有一个 main
方法,因此可以直接使用它来
创建一个 Vertx
上下文,然后
部署 JokeVerticle
。
您可以从以下位置运行应用程序
您的 IDE,通过运行 JokeVerticle
类的 main
方法,或者
使用 Maven:mvn compile exec:java
,或者
使用 Gradle:./gradlew run
(Linux、macOS)或 gradle run
(Windows)。
本文档涵盖了
用于执行 HTTP 请求的 Vert.x Web 客户端,
从 JSON 响应中提取数据,
Vert.x 周期性任务。
最后发布时间:2025-02-03 01:38:44 +0000。