我正在尝试使用
RESTful
构建
jakarta 10.0.0
客户端
ClientBuilder.newClient()
失败并显示以下错误消息:
Exception in thread "main" java.lang.RuntimeException: java.lang.ClassNotFoundException: Provider for jakarta.ws.rs.client.ClientBuilder cannot be found
未找到提供者。可能是什么问题呢? 我该如何解决这个问题? 我已将
jakarta 10.0.0
依赖项添加到 Maven 项目 pom 文件中。我还有什么要补充的吗?
package com.tbs.companyclient;
import jakarta.ws.rs.client.ClientBuilder;
import jakarta.ws.rs.client.Client;
import jakarta.ws.rs.client.WebTarget;
import jakarta.ws.rs.core.MediaType;
/**
*
* @author tom
*/
public class CompanyWebClient {
public static void main(String[] args) {
// Create a JAX-RS client
Client client = ClientBuilder.newClient();
// Define the target endpoint of the RESTful web service
WebTarget target = client.target("http://localhost:8080/CompanyWebService/resources/company/departments");
// Make a GET request to the target endpoint and specify the expected response media type
String response = target.request(MediaType.APPLICATION_JSON).get(String.class);
// Print the response
System.out.println("Response:");
System.out.println(response);
// Close the client
client.close();
}
}
这是pom文件。
<?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>com.tbs</groupId>
<artifactId>CompanyClient</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>22</maven.compiler.source>
<maven.compiler.target>22</maven.compiler.target>
<exec.mainClass>com.tbs.companyclient.CompanyClient</exec.mainClass>
</properties>
<description>RESTful Client for the CompanyWebService</description>
<name>CompanyWebClient</name>
<dependencies>
<dependency>
<groupId>jakarta.platform</groupId>
<artifactId>jakarta.jakartaee-api</artifactId>
<version>10.0.0</version>
</dependency>
</dependencies>
</project>
编译正常,当尝试运行应用程序时,出现所描述的错误。
Jakarta 是一个规范,您所拥有的依赖项仅包含规范而不包含实现。
运行时需要规范的实现,
jersey
就是这样一种实现,它可以在运行时使用。
Jersey 3.1.5 提供 Jakarta RESTful WebServices 3.1.0
的实现添加以下依赖来解决问题。
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>3.1.5</version>
</dependency>