Implement JAX-RS RESTFul with Jersey 2

JAX-RS: Java API for RESTful Web Services is a Java programming language API that provides support in creating web services according to the Representational State Transfer (REST) architectural pattern (http://en.wikipedia.org/wiki/Java_API_for_RESTful_Web_Services)

In this tutorials will show you how to implement RESTFul webservice with jersey 2.

Tools you need :

1. Create webapp project with maven

Ues maven to create new web app project with maven's structure.

mvn archetype:generate

maven will take sometime to load dependencies and will let you chose type of project. In our case we chose maven webapp project (copy and paste this text to maven prompt).

org.apache.maven.archetypes:maven-archetype-webapp

Choose org.apache.maven.archetypes:maven-archetype-webapp version:

5: 1.0

Define value for property 'groupId': :

com.outlawman.hellojersy

Define value for property 'artifactId': :

hellojersy

hit enter to pass version, and package information and 'yes' for confirmation. Now you get new project directory name hellojersy.

change your working directory to your hellojersy project directory and run command to build your project

mvn clean install

your project should build without any error.

2. Edit pom.xml to defined your project's dependencies

3. Edit web.xml

4. Create resource object

Resource object can be any POJO. Jersey will automatically convert our POJO to JSON. In our case we just use Person class like this :

5. Create an endpoint

We create our RESTFul endpoint by using some jax-rs anotation.

6. Test your RESTFul endpoint

buld your application

mvn clean install

run application :

mvn jetty:run

call your RESTFul endpoint by browser :

http://localhost:9000/hellosjsesy/rest/persons/

http://localhost:9000/hellosjsesy/rest/persons/fox/

7. Code on github

https://github.com/tsongpon/hellojersy