首选 quarkus-resteasy,若未用到 Jakarta StreamingOutput 则可换用 reactive 的 quarkus-rest。
quarkus-rest 中需要用 Mutiny 库的 Multi<byte[]> 来替代 StreamingOutput;其 Uni 类型名的含义为单一性前缀。
quarkus-rest-client 兼容 quarkus-rest 和 quarkus-resteasy 服务端,JVM跑不了其依赖的ArC库CDI环境,已取代 quarkus-resteasy-client。
@RegisterRestClient(baseUri = "https://domain.name/apis") 方式或 QuarkusRestClientBuilder.newBuilder().baseUri(URI.create("https://domain.name/api")).build(ClientInterface.class);
资源:
自动包含该目录下资源 src\main\resources\META-INF\resources\
需显式包含资源根目录 src\main\resources\
读取资源文件亲测可用 Thread.currentThread().getContextClassLoader().getResourceAsStream("x.txt");
HTTP/HTTPS:
environment variable QUARKUS_HTTP_SSL_CERTIFICATE_KEY_STORE_PASSWORD.
quarkus.http.port=8080
# 关闭80端口访问:
quarkus.http.insecure-requests=disabled
quarkus.http.ssl-port=8443
quarkus.http.ssl.certificate.key-store-file=/path/to/keystore
非quarkus环境Rest Client:
implementation("org.jboss.weld.se:weld-se-core:6.0.3.Final")
implementation("io.smallrye:jandex:3.4.0") // for weld
implementation("org.jboss.resteasy.microprofile:microprofile-rest-client:3.0.1.Final")
implementation("io.smallrye.config:smallrye-config:3.13.4") // for resteasy
implementation("io.quarkus:quarkus-undertow") // Servlet
@Path("meta")
@RegisterRestClient(baseUri = "https://congci.com/main/apis/core")
public interface DefaultRestClient {
@GET // https://congci.com/main/apis/core/meta/servlet-info
@Path("/servlet-info") @Produces(MediaType.TEXT_PLAIN)
public String servletInfo();
}
CDI.current().select(DefaultRestClient.class).get();
最简实例:
dependencies {
implementation(enforcedPlatform("${quarkusPlatformGroupId}:${quarkusPlatformArtifactId}:${quarkusPlatformVersion}"))
implementation("io.quarkus:quarkus-arc") // Build-Time CDI
implementation("io.quarkus:quarkus-rest") // Vert.x 反应式 JAX-RS or 换为传统的 quarkus-resteasy
}
Restful:
Quarkus 支持 class/record 自定义实体写成内部类,但必须注解@RegisterForReflection,且 class 必须存在无参构造器:
@RegisterForReflection public record ResultRecord(String text) { }
Response.ok(new ResultRecord(r)).header("X-Robots-Tag", "noindex").build();
Quarkus 不支持 @FormParam("key") 接参,需换用 MultipartFormDataInput - input.getFormDataMap().get("k").getFirst().getBodyAsString();