I am trying to build a RESTful client with jakarta 10.0.0
ClientBuilder.newClient() is failing with this error message:
Exception in thread "main" java.lang.RuntimeException: java.lang.ClassNotFoundException: Provider for jakarta.ws.rs.client.ClientBuilder cannot be found
A Provider is not found. What could be the problem?
How can I fix that?
I have added the jakarta 10.0.0 dependency to the maven project pom file. What else do I have to add?
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();
}
}
This is the pom file.
<?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>
Compiling works, when trying to run the app the described error occurs.
Jakarta is a specification and the dependency you have contains only the specification not the implementation.
An implementation for the specification is required in the runtime and
jerseyis one such implementation and it can be used in the runtime.Jersey 3.1.5 provides implementation for Jakarta RESTful WebServices 3.1.0(Released for Jakarta EE 10)
Add following dependency to resolve the problem.