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

Connect with

1. Create Maven project

Maven web project can be created by different way. I have chosen via Eclipse

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. MyNameService.java

This class is used as service handler where jersey mapping are there.

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.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import com.mysoftkey.jaxrs.model.Name;

/**
 * @author ranjeet
 *
 */
@Path("/myname")
public class MyNameService {

 /**
  * This method return the name details in XML format.
  *  
  * URL: http://localhost:8080/jersey/myname/11
  * 
  * @param pin
  * @return
  */
 @GET
 @Path("/{id}")
 @Produces(MediaType.APPLICATION_XML)
 public Name getNameXML(@PathParam("id") int id) {
	Name name = new Name();
	name.setFirstName("Ranjeet");
	name.setMiddleName("Kumar");
	name.setLastName("Jha");
	name.setId(id);
         	return name;

  }
	
/**
 * This method is used to add name xml.
 * 
 * endpoint: http://localhost:8080/jersey/myname/11
 * 
 * XML payload: RanjeetJhaKumar
 * @param id
 * @param name
 * @return
 */
 @POST
 @Path("/{id}")
 @Consumes(MediaType.APPLICATION_XML)
 public Response addNameXML(@PathParam("id") int id, Name name) {

	System.out.println(name.getFirstName() + " " + name.getMiddleName() + " " + name.getLastName());
	return Response.status(200)
				.entity(name.getFirstName() + " " + name.getMiddleName() + " " + name.getLastName())
				.build();

  }
}

6. Model class Name.java

package com.mysoftkey.jaxrs.model;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

/**
 * @author Ranjeet Jha
 *
 */
@XmlRootElement(name = "name")
public class Name {
	
	int id;
	String firstName;
	String lastName;
	String middleName;
	
	@XmlAttribute
	public int getId() {
		return id;
	}

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

	@XmlElement
	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	@XmlElement
	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	@XmlElement
	public String getMiddleName() {
		return middleName;
	}

	public void setMiddleName(String middleName) {
		this.middleName = middleName;
	}
	
}

7. Run application

Add project into Tomcat configured within Eclipse , run Tomcat and open browser and type url as :
1. Get operation which return XML output.
Service endpoint : http://localhost:8080/jersey/myname/11
output on browser as :


  Ranjeet
  Jha
  Kumar

2. For post XML payload , open Advance Rest client or any another client and enter following:

Service Endpoint: http://localhost:8080/jersey/myname/11
Http method: POST
Content-Type: application/xml
jsonPayload: RanjeetJhaKumar


  Ranjeet
  Jha
  Kumar

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

person added successfully : Ranjeet Kumar Jha

8. Download Source Code

Download project by click here

9. References

Jersey Official site
JAX-RS java
Oracle Java JEE


Connect with

2 thoughts on “JAX-RS Jersey Rest service with XML+ Maven + Tomcat

  1. Thanks for sharing your thoughts. I really appreciate your efforts and I am waiting for your next write ups thank you once again.

Leave a Reply

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