37

I'm trying to get into JAX-RS. My project jdk is set to 1.7.03. Does there have to be definitions of annotations for JAX-RS(javax.ws.rs)? If not, where I can find them?

1

4 Answers 4

40

These classes (JSR 311: JAX-RS: The JavaTM API for RESTful Web Services) are not part of the JDK. You need to include appropriate JAR file to your CLASSPATH. You can find the API e.g. in maven repository.

Also check out , (reference implementation), from JBoss, and few other JAX-RS implementations.

4
12

Try: http://download.oracle.com/otndocs/jcp/jaxrs-2_0_rev_A-mrel-spec/index.html

Download: javax.ws.rs-api-2.0.rev.A.jar

Then add to your Project Properties(YourProjectName)->Libraries->Add JAR/Folder

1
  • 3
    Better late than never. Maven rep points to JSR 311 and today we are at JSR 339-JAX-RS-2.0
    – Tirath
    Commented Apr 17, 2016 at 7:32
5

It doesn't matter what your JDK is set to.

Go here= http://www.java2s.com/Code/Jar/j/Downloadjavaxwsrsjar.htm

Or type in "Download javax.ws.rs" into Google.

Download “javax.ws/javax.ws.rs.jar.zip”

enter image description here

Unzip to your desktop. The resulting file should be a jar file. Open your project preferences and add it as external jar.

enter image description here

enter image description here

Select your javax.ws.rs.jar file from your Desktop.

Click “Apply” and then “OK”.

Now you should be able to use the javax.ws.rs libraries. You can test it by typing "import javax.ws.rs.GET;" in one of your Java classes.

0

Update for Jakarta EE 11

Oracle has donated Java EE technologies to the Eclipse Foundation where it became Jakarta EE.

Jakarta EE defines specifications and interfaces for enterprise-oriented technologies. Jakarta EE does not provide any implementations. Implementing is left to third-parties. Those parties may other projects at the Eclipse Foundation, or at the Apache Foundation, or by vendors, or by anyone who cares to write an implementation.

Jakarta RESTful Web Services (formerly JAX-RS) makes it very easy to write REST oriented servers or clients.

Web app

For a Web app, specify the library of interfaces provided by the Jakarta EE project.

       <!-- 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>

In the POM snippet above, note the scope of provided (see manual). The value provided means you want a copy of the library downloaded for your project to use when compiling, but do not want the library included in your final software artifact (your WAR file).

Notice that you do not need to specify an implementation. Your code you write will address only the Jakarta interfaces, without any direct knowledge of the implementation. When you run your Web app, you will run within a Jakarta EE compliant server. Being Jakarta EE compliant means that web server will bring its own copy of the Jakarta interfaces, and will bring some implementation of those interfaces. For example:

Desktop app

If you are writing a desktop app rather than Web app, either as a console app or with a GUI, then you need to obtain and include both the interfaces and an implementation. A desktop app runs without any Jakarta EE server.

You will research the various implementations available, and decide which best meets your needs. Then add a depending in your POM for your product of choice.

<!-- 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>compile</scope>
</dependency>

… add another dependency here for implementation of your choice …

Notice the scope here is compile which means you want the dependency library available during compilation and you want a copy included in your final software artifact (likely a JAR file).

For more details, see my related Answers on other Questions, here and here.

Not the answer you're looking for? Browse other questions tagged or ask your own question.