What Maven Artifact Contains javax.ws.rs.Path?
Asked Answered
E

5

6

I am working through examples in Restful Java with JAX-RS by Bill Burke. I'm using intellij and created a Maven project to make a "Hello World" web service. I understand that JAX-RS is a specification, and not an implementation. I would like to try the Apache CXF implementation.

How do I find out what jar, or what Maven artifact contains the necessary classes to:

import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.GET;
Epineurium answered 9/8, 2013 at 1:51 Comment(5)
Google can help you. A quick search got me among others aredko.blogspot.com/2013/01/… which provide samples (you an try newer version of the plugins)Burnell
Great. This was the one I was looking for. Java hardcores seem to know about it, but Im not sure how you find out an artifact like that has the package your looking for. How would I have found that on my own (ie without knowing about cxf)? I know there is find jars, but that only listed jersey. In general I want to get better at finding maven artifacts for certain classes. Does anything make that easier?Epineurium
Yes, the two proposed answer are what we all do. If you don't know what implementation to use, you need to google/yahoo to list the various implementation (and read what their limitation/support/dependencies/ease of use), then search on maven-oriented search sites like Central (search.maven.org)Burnell
Interesting, thanks. Sounds like a slightly daunting task, faster in groups of people probably. I grabbed cxf-rt-frontend-jaxrs and am giving it a try. That blog post is great btw, thanks for that.Epineurium
Central is your friend. You can go there and search for a class name or an artifact name for nearly any open-source project, and it's defined in the Maven super POM, so anything that's on there just requires adding the dependency.Traject
T
4

Try searching for the class on Central. It looks like jaxrs-api is what you're looking for; there are a couple of different packagers, and which one you want is probably whichever goes with your container.

Traject answered 9/8, 2013 at 1:54 Comment(0)
T
13

Add below lines in pom.xml

<dependency>
    <groupId>javax.ws.rs</groupId>
    <artifactId>javax.ws.rs-api</artifactId>
    <version>2.0</version>
</dependency>
Turntable answered 4/1, 2015 at 15:59 Comment(0)
T
4

Try searching for the class on Central. It looks like jaxrs-api is what you're looking for; there are a couple of different packagers, and which one you want is probably whichever goes with your container.

Traject answered 9/8, 2013 at 1:54 Comment(0)
R
4

g:"javax.ws.rs" AND a:"jsr311-api"

http://search.maven.org/

Rhesus answered 9/8, 2013 at 1:58 Comment(0)
A
1

In 2024 javax.ws.rs.Path et. al. are part of Jakarta, specifically jakarta.ws.rs:jakarta.ws.rs-api, currently jakarta.ws.rs:jakarta.ws.rs-api:3.1.0

Aam answered 15/1 at 13:49 Comment(0)
P
0

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.

Penury answered 4/9 at 6:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.