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?
4 Answers
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 apache-cxf, jersey (reference implementation), resteasy from JBoss, restlet and few other JAX-RS implementations.
-
3If they're not part of the JDK, then why are the classes defined in the Java JDK? See docs.oracle.com/javaee/6/api/javax/ws/rs/Path.html Commented Feb 5, 2014 at 20:29
-
8Because they are part of the JavaEE specification and not part of the base Java SDK.– KolbanCommented Sep 6, 2014 at 15:08
-
Jersey 2.25.1 includes
javax.ws.rs-api-2.0.1.jar
, which contains the classes that you need. You can download theJersey JAX-RS 2.0 RI bundle
from jersey.java.net/download.html. Commented Apr 26, 2017 at 18:14 -
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
-
3Better late than never. Maven rep points to
JSR 311
and today we are atJSR 339-JAX-RS-2.0
– TirathCommented Apr 17, 2016 at 7:32
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”
Unzip to your desktop. The resulting file should be a jar file. Open your project preferences and add it as external jar.
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.
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:
- Glassfish server brings Eclipse Jersey as its implementation.
- Wildfly server brings RESTEasy.
- TomEE server brings Apache CXF.
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.