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

Connect with

JAX-RS jersey XMLJAX-RS Jersey XML Rest API is one of the way of exposing web services API. JAX-RS Jersey Rest API with XML 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

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:

I used follwoing environment for this JAX-RS tutorial series for exposposing of REST API with Jersey Rest API.

  • 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 returns 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 the 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 for JAX-RS Jersey API

Imn this JAX-RS tutorial series of Jersey API this is the model class which we use to parse in controller.

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 for JAX-RS Jersey API

Jersey Official site
JAX-RS java
Oracle Java JEE

Happy Learning JAX-RS tutorial series of jersey rest api which consumed as XML and produces as XML format of data.


Connect with

5 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.

  2. Your method of telling the whole thіng in tһis post is genuinely goߋd, every one Ьe able to easily be aware of it, Tһanks
    a ⅼot.

  3. Well composed articles like yours renew my faith in today’s
    writers.You’ve written information I can finally agree
    on and also use. Many thanks for sharing.

Leave a Comment

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