Java Jersey: Difference between revisions
Line 17: | Line 17: | ||
# Goto the directory where you wanna build the Jersey Simple Service (i.e. /Sources/Java/Jersey/Refmaven) | # Goto the directory where you wanna build the Jersey Simple Service (i.e. /Sources/Java/Jersey/Refmaven) | ||
# Type: | # Type: | ||
< | <syntaxhighlight lang="bash"> | ||
mvn archetype:generate -DarchetypeArtifactId=jersey-quickstart-grizzly2 -DarchetypeGroupId=org.glassfish.jersey.archetypes -DinteractiveMode=false -DgroupId=com.example -DartifactId=simple-service -Dpackage=com.example -DarchetypeVersion=2.18 | mvn archetype:generate -DarchetypeArtifactId=jersey-quickstart-grizzly2 -DarchetypeGroupId=org.glassfish.jersey.archetypes -DinteractiveMode=false -DgroupId=com.example -DartifactId=simple-service -Dpackage=com.example -DarchetypeVersion=2.18 | ||
</ | </syntaxhighlight> | ||
which should end-up with | which should end-up with | ||
< | <syntaxhighlight lang="bash"> | ||
[INFO] ------------------------------------------------------------------------ | [INFO] ------------------------------------------------------------------------ | ||
[INFO] BUILD SUCCESS | [INFO] BUILD SUCCESS | ||
Line 30: | Line 30: | ||
[INFO] ------------------------------------------------------------------------ | [INFO] ------------------------------------------------------------------------ | ||
$ | $ | ||
</ | </syntaxhighlight> | ||
The results are: | The results are: | ||
Line 42: | Line 42: | ||
==== MyResource class ==== | ==== MyResource class ==== | ||
< | <syntaxhighlight lang="java"> | ||
package com.example; | package com.example; | ||
Line 68: | Line 68: | ||
} | } | ||
} | } | ||
</ | </syntaxhighlight> | ||
== See also == | == See also == |
Revision as of 11:14, 20 June 2015
Developing RESTful Web services that seamlessly support exposing your data in a variety of representation media types and abstract away the low-level details of the client-server communication is not an easy task without a good toolkit.
In order to simplify development of RESTful Web services and their clients in Java, a standard and portable JAX-RS API
[1]
[2]
has been designed. Jersey RESTful Web Services framework is open source, production quality, framework for developing RESTful Web Services in Java that provides support for JAX-RS APIs and serves as a JAX-RS (JSR 311 & JSR 339) Reference Implementation.
Jersey framework [3] is more than the JAX-RS Reference Implementation. Jersey provides it’s own API that extend the JAX-RS toolkit with additional features and utilities to further simplify RESTful service and client development. Jersey also exposes numerous extension SPIs so that developers may extend Jersey to best suit their needs.
Maven
Jersey is complete maven build. The following is tested for OS/X and should work on Linux/Unix. Assumed is that you have installed maven successful and also have the right location for the m2 libraries.
Creating a New Project from Maven Archetype
- Open Terminal
- Goto the directory where you wanna build the Jersey Simple Service (i.e. /Sources/Java/Jersey/Refmaven)
- Type:
<syntaxhighlight lang="bash"> mvn archetype:generate -DarchetypeArtifactId=jersey-quickstart-grizzly2 -DarchetypeGroupId=org.glassfish.jersey.archetypes -DinteractiveMode=false -DgroupId=com.example -DartifactId=simple-service -Dpackage=com.example -DarchetypeVersion=2.18 </syntaxhighlight> which should end-up with <syntaxhighlight lang="bash"> [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 15.289s [INFO] Finished at: Fri Jun 19 20:01:09 CEST 2015 [INFO] Final Memory: 13M/126M [INFO] ------------------------------------------------------------------------ $ </syntaxhighlight>
The results are:
- A new simple-service project directory created in your current location.
- Project build and management configuration is described in the pom.xml located in the project root directory.
- Project sources are located under src/main/java.
- Project test sources are located under src/test/java.
- There are 2 classes in the project source directory in the com.example package:
- The
Main class
is responsible for bootstrapping the Grizzly container as well as configuring and deploying the project's JAX-RS application to the container. - The
MyResource class
contains implementation of a simple JAX-RS resource.
- The
MyResource class
<syntaxhighlight lang="java"> package com.example;
import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType;
/**
* Root resource (exposed at "myresource" path) */
@Path("myresource") public class MyResource {
/** * Method handling HTTP GET requests. The returned object will be sent * to the client as "text/plain" media type. * * @return String that will be returned as a text/plain response. */ @GET @Produces(MediaType.TEXT_PLAIN) public String getIt() { return "Got it!"; }
} </syntaxhighlight>
See also
top Implementations of JAX-RS include:[4]
- Apache CXF, an open source Web service framework
- Jersey, the reference implementation from Sun (now Oracle)
- RESTeasy, JBoss's implementation
- Restlet
- Apache Wink, Apache Software Foundation Incubator project, the server module implements JAX-RS
- WebSphere Application Server from IBM:
- Version 7.0: via the "Feature Pack for Communications Enabled Applications"
- Version 8.0 onwards: natively
- WebLogic Application Server from Oracle, see notes
- Apache Tuscany (http://tuscany.apache.org/documentation-2x/sca-java-bindingrest.html)
- Cuubez framework (http://www.cuubez.com)
- Everrest, Codenvy's Implementation
Reference
- ↑ Wikipedia JAX API WS, Java API for XML Webservices.
- ↑ Wikipedai JAX RS, Java API for RESTful WebServices
- ↑ Jersey Homepage, Jersey
- ↑ Mark Little, A Comparison of JAX-RS Implementations