JAX-RS Jersey Rest service with JSON + Maven + Tomcat

Connect with

JAX-RS jerseyJAX-RS Jersey Rest API with JSON is one of the way of exposing web services API. I demonstrated with Maven as a build tool and Tomcat as a web server for this JAX-RS Jersey example.

1. Create Maven project for JAX-RS Jersey Rest

I used Maven as the build tool in this web project deployed in the Tomcat web server for this JAX-RS tutorial series of Jersey Rest API.

1. File -> New -> Others -> Maven ->Maven Project -> click Next -> choose maven-archetype-webapp
or
2. In Eclipse IDE create “Dynamic Web Project” and select Project -> write click on project -> select Configure -> Convert Maven Project.

2. Environment used as:

  • JDK 7
  • Eclipse Luna 4.4.1
  • Tomcat 7
  • Maven 3.2.3
  • Jersey 1.9

3. Maven pom.xml

Add dependency of jersey server specific jar of your suitable version. Maven pull required dependency from specified repository.


  4.0.0
  com.softkey.restJersey
  jersey
  war
  0.0.1-SNAPSHOT
  Rest Jax-rs jersey Maven Webapp
  http://maven.apache.org
  
   
     1.9
    
    
  
	
		com.sun.jersey
		jersey-server
		${jersey.version}
	
	
       
	
		com.sun.jersey
		jersey-json
		${jersey.version}
	

  

  
    jersey
    src/main/java
  

4. web.xml , Deployment descriptor entry

We need to add ServletContainer entry in our Deployment structure i.e. web.xml here. And package we need to specify in init-param , where our services class resides. When web server tomcat start, the instance of ServletContainer loads into servlet container.



  jax-rs
  
  
		jersey-serlvet
		com.sun.jersey.spi.container.servlet.ServletContainer
		
		     com.sun.jersey.config.property.packages
		     com.mysoftkey.jaxrs
		
		
		
		
			com.sun.jersey.api.json.POJOMappingFeature
			true
		
		1
	

	
		jersey-serlvet
		/*
	
	
  
    index.jsp
  

5. PersonJsonService.java for JAX-RS Jersey

/**
 * 
 */
package com.mysoftkey.jaxrs;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import com.mysoftkey.jaxrs.model.Person;

/**
 * This Service class demonstrate input as JSON payload to submit the request
 * and return a json output.
 * 
 * @author Ranjeet Jha
 *
 */
@Path("/person")
public class PersonJsonService {

 /**
  * This method is used to get Person object in JSON as it Produces application/json media type.
  * 
  * URL: http://localhost:8080/jersey/person/get
  * 
  * @return
  */
 @GET
 @Path("/get")
 @Produces(MediaType.APPLICATION_JSON)
 public Response getPersonJSONHandler() {
  	Person person = new Person();
	person.setId(30);
	person.setName("ranjeet Jha");
	//return person;
	
	return Response.status(200).entity(person).build();
}

 /**
  * This method is used to add the person as payload JSON with the endpoint. 
  * 
  * URL: http://localhost:8080/jersey/person/add
  * 
  * jsonPayload: {"id":1, "name":"Ranjeet Jha"}
  * 
  * @param response
  * @return
  */
 @POST
 @Path("/add")
 @Consumes(MediaType.APPLICATION_JSON)
 public Response addPersonHandler(Person person) {
	String result = "Person added successfully : " + person.getName();
	// write code to add person into db or in-memory.
	return Response.status(201).entity(result).build();
		
 }
}

6. Model class Person.java

package com.mysoftkey.jaxrs.model;

import java.io.Serializable;

/**
 * this is model class to encapsulate property of person.
 *
 * @author Ranjeet Jha
 *
 */
public class Person implements Serializable {
	private int id;
	private String name;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}

7. Run application

Add project into Tomcat configured within Eclipse , run Tomcat and open browser and type url as : http://localhost:8080/jersey/person/get
output on browser as :

{
name: "ranjeet Jha",
id: 30
}

For post JSON payload, open Advance Rest client or any other client and enter the following:

Service Enpoint: http://localhost:8080/jersey/person/add
Http method: POST
Content-Type: application/json
jsonPayload:

 {"id":1,="" "name":"ranjeet="" jha"}

Click submit and see output as follows with http status ok:

person added successfully : Ranjeet Jha

8. Download Source code

Download project by click here

9. References for JAX-RS Jersey tutorials

Jersey Official site
JAX-RS java
Oracle Java JEE

Happy learning for JAX-RS Jersey for XML example.


Connect with

6 thoughts on “JAX-RS Jersey Rest service with JSON + Maven + Tomcat”

  1. You completed a number of fine points there. I did a search on the theme and found the majority of folks will agree with your blog.

  2. hello there and thank you for your information –
    I’ve certainly picked up anything new from right here. I did however expertise
    some technical issues using this web site, as I experienced to reload the site lots of times previous to
    I could get it to load properly. I had been wondering if your web host is
    OK? Not that I’m complaining, but sluggish loading instances times
    will very frequently affect your placement in google and could damage your quality
    score if ads and marketing with Adwords. Anyway I’m adding this
    RSS to my e-mail and could look out for a lot more
    of your respective fascinating content. Ensure that you update this again soon.

Leave a Reply to Smitha217 Cancel Reply

Your email address will not be published. Required fields are marked *