Interface vs Implementation
At compile time, you need only the interfaces (API) defined in the Jakarta RESTful Web Services specification (formerly JAX-RS). Those interfaces are defined in a library provided by the Jakarta EE project at the Eclipse Foundation (Oracle having donated Java EE).
You need a copy of that library of interfaces (API) for use at compile-time, so the compiler understands your calls to Jakarta RESTful Web Services. Include that as a dependency in your Maven POM file. Mark the dependency with a scope
of provided
so the library is not included in your final software artifact (your WAR file).
<!-- https://mvnrepository.com/artifact/jakarta.ws.rs/jakarta.ws.rs-api -->
<dependency>
<groupId>jakarta.ws.rs</groupId>
<artifactId>jakarta.ws.rs-api</artifactId>
<version>4.0.0</version>
<scope>provided</scope>
</dependency>
What Maven Artifact Contains javax.ws.rs.Path?
The artifact is jakarta.ws.rs-api
shown in that dependency
element quoted above. This artifact is produced by the Jakarta EE project. The Jakarta EE project does not produce an implementation; implementing is the job of third-parties. Those third-parties might happen to be other projects at the Eclipse Foundation, or at the Apache Foundation, or commercial vendors, or anybody who wants to write an implementation.
How do I find out what jar, or what Maven artifact contains the necessary classes
- The interfaces for Jakarta RESTful Web Services are found in the artifact
jakarta.ws.rs-api
discussed directly above.
- An implementation of those interfaces is provided at runtime by the Jakarta EE compliant server of your choice.
At present, multiple parties provide various implementations of the Jakarta RESTful Web Services interfaces. See a list at Wikipedia.
Choosing Apache CXF implementation
I would like to try the Apache CXF implementation
That is irrelevant during development time. Your code only calls on the methods defined in the interfaces of the API library discussed above. Your code knows nothing about the Apache CXF implementation of those interfaces.
Your web app will only encounter Apache CXF at runtime, not compile-time. Your web app will run in some kind of Jakarta EE compliant server. That server, being Jakarta EE compliant, will provide its own copy of both the interfaces and an implementation. If you want to use Apache CXF, obtain a Jakarta EE compliant server product that chose Apache CXF as its implementation.
Notice in the POM below that we specify only the jakarta.ws.rs-api
library of interfaces as a dependency. No mention there of an implementation. If we deploy to Glassfish server, Eclipse Jersey will be the bundled implementation. If we deploy to Wildfly, RESTEasy will be the bundled implementation. If you want Apache CXF as your implementation, use a Jakarta EE compliant server whose creators made that choice.
Apache TomEE
Apache TomEE is one Jakarta EE compliant server using Apache CXF as its choice for an implementation of Jakarta RESTful Web Services.
Other server products may use Apache CXF.
Hello World example app
Restful Java with JAX-RS … to make a "Hello World" web service
Here is the source code for two simple classes needed to make a RESTful Web Service. Code provided by the new-project template in IntelliJ 2024.2 Ultimate Edition.
This example is aimed at Jakarta EE 11 generation of interfaces and implementations.
package work.basil.example.exjakartarestserver;
import jakarta.ws.rs.ApplicationPath;
import jakarta.ws.rs.core.Application;
@ApplicationPath ( "/api" )
public class HelloApplication extends Application { }
… and:
package work.basil.example.exjakartarestserver;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
@Path ( "/hello-world" )
public class HelloResource
{
@GET
@Produces ( "text/plain" )
public String hello ( )
{
return "Hello, World!";
}
}
My 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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>work.basil.example</groupId>
<artifactId>ExJakartaRestServer</artifactId>
<version>1.0-SNAPSHOT</version>
<name>ExJakartaRestServer</name>
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.release>22</maven.compiler.release>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/jakarta.ws.rs/jakarta.ws.rs-api -->
<dependency>
<groupId>jakarta.ws.rs</groupId>
<artifactId>jakarta.ws.rs-api</artifactId>
<version>4.0.0</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.11.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.4.0</version>
</plugin>
</plugins>
</build>
</project>
See more in-depth discussion of this example at my Answer on another Question.