Rest project with JAXB


Java Architecture for XML Binding (JAXB) is an XML-to-Java binding technology which helps to transfer
the java object to XML and vice versa.

In other words, JAXB is used to marshal the Java object to XML and unmarshal the XML file to java object.

We can use JAXB with JAX-RS to return Java object in XML format from the rest service.

Create a new Maven Web project in eclipse (Refer Rest service Hello World project for the same)

Step 1

Update pom.xml with below dependencies

  1. <project xmlns=""
  2.               xmlns:xsi=""
  3.               xsi:schemaLocation="
  4.    ">
  5.   <modelVersion>4.0.0</modelVersion>
  6.   <groupId>RestServiceWithJerseyAndJAXB</groupId>
  7.   <artifactId>RestServiceWithJerseyAndJAXB</artifactId>
  8.   <packaging>war</packaging>
  9.   <version>0.0.1-SNAPSHOT</version>
  10.   <name>RestServiceWithJerseyAndJAXB Maven Webapp</name>
  11.   <url></url>
  12.    <dependencies>
  14.          <dependency>
  15.                <groupId>junit</groupId>
  16.                <artifactId>junit</artifactId>
  17.                <version>3.8.1</version>
  18.                <scope>test</scope>
  19.          </dependency>
  21.          <dependency>
  22.                <groupId>org.glassfish.jersey.containers</groupId>
  23.                <artifactId>jersey-container-servlet</artifactId>
  24.                <version>2.24</version>
  25.          </dependency>
  27.          <dependency>
  28.                <groupId>javax.xml</groupId>
  29.                <artifactId>jaxb-api</artifactId>
  30.                <version>2.1</version>
  31.          </dependency>
  33.          <dependency>
  34.                <groupId>javax.servlet</groupId>
  35.                <artifactId>servlet-api</artifactId>
  36.                <version>2.5</version>
  37.          </dependency>
  39.    </dependencies>
  41.   <build>
  42.     <finalName>RestServiceWithJerseyAndJAXB</finalName>
  43.   </build>
  44. </project>
<project xmlns="" 
  <name>RestServiceWithJerseyAndJAXB Maven Webapp</name>



We have added dependencies for Jersey servlet,Jaxb,servelt api and Junit in the above pom file.

Step 2

Update web.xml file with Jersey servlet container

we have defined a special servlet called “jersey-serlvet” in web.xml and mapped it by the URL pattern /rest/*

So just like any other servlet in web application,any request matching with the given pattern i.e /rest/* will be redirected to “Jersey servelt”.

  1. <!DOCTYPE web-app PUBLIC
  2.  "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
  3.  "" >
  5. <web-app>
  6.   <display-name>Archetype Created Web Application</display-name>
  7.   <servlet>
  8.         <servlet-name>jersey-serlvet</servlet-name>
  9.          <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
  10.         <init-param>
  11.             <param-name>jersey.config.server.provider.packages</param-name>
  12.             <param-value>com.kb.service</param-value>
  13.         </init-param>
  14.         <load-on-startup>1</load-on-startup>
  15.     </servlet>
  17.     <servlet-mapping>
  18.         <servlet-name>jersey-serlvet</servlet-name>
  19.         <url-pattern>/rest/*</url-pattern>
  20.     </servlet-mapping>
  21. </web-app>
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "" >

  <display-name>Archetype Created Web Application</display-name>


We have also provided the package of java classes(to be qualified as Rest services) under init-param tag.

This package will be considered by Jersey servlet container to identify the actual service when the request comes in.

Step 3

Create a domain class which represents the data with JAXB annotations

  1. package com.kb.model;
  3. import java.util.Date;
  5. import javax.xml.bind.annotation.XmlElement;
  6. import javax.xml.bind.annotation.XmlRootElement;
  8. @XmlRootElement(name="User")
  9. public class User {
  10.     private String id;
  11.     private String name;
  12.     private int age;
  13.     public String getId() {
  14.         return id;
  15.     }
  17.     @XmlElement
  18.     public void setId(String id) {
  19. = id;
  20.     }
  21.     public String getName() {
  22.         return name;
  23.     }
  25.     @XmlElement
  26.     public void setName(String name) {
  27. = name;
  28.     }
  29.     public int getAge() {
  30.         return age;
  31.     }
  33.     @XmlElement
  34.     public void setAge(int age) {
  35.         this.age = age;
  36.     }
  37. }
package com.kb.model;

import java.util.Date;

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

public class User {
	private String id;
	private String name;
	private int age;
	public String getId() {
		return id;
	public void setId(String id) { = id;
	public String getName() {
		return name;
	public void setName(String name) { = name;
	public int getAge() {
		return age;
	public void setAge(int age) {
		this.age = age;

We have created a User class with id and name to represent the data

@XmlRootElement – specifies the root tag of each user record in xml.

@XmlElement – specifies the child element for each attribute of User record.

Step 4

Create the Rest service class which will return the list of user in xml format

  1. package;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import;
  6. import;
  7. import;
  8. import;
  9. import com.kb.model.User;
  11. @Path("/allUserDetails")
  13. public class UserService {
  15.     @GET
  16.     @Produces(MediaType.APPLICATION_XML)
  17.     public List<User> getUsers(){
  18.         List<User> userList = new ArrayList();
  19.         User user1 = new User();
  20.         user1.setId("1");
  21.         user1.setAge(20);
  22.         user1.setName("raj");
  23.         User user2 = new User();
  24.         user2.setId("2");
  25.         user2.setAge(21);
  26.         user2.setName("ram");
  27.         userList.add(user1);
  28.         userList.add(user2);
  29.         return userList;
  31.     }
  32. }

import java.util.ArrayList;
import java.util.List;
import com.kb.model.User;


public class UserService {

	public List<User> getUsers(){
		List<User> userList = new ArrayList();
		User user1 = new User();
		User user2 = new User();
		return userList;


This annotation helps to map the request URL to appropriate Rest service.

So any request with /allUserDetails (relative to the base URI) will be mapped to UserService.

This annotation should be used either at the class level or at the method level.


This annotation specifies that only GET requests will be accepted by this method.

This annotation will be used at the method level.


This annotation is used to specify the representation of a data (MIME type) that the service can produce and send it to client.

This annotation can be used either at the class level or at the method level.

If this annotation is applied at the class level,all the methods in the class can produce the same MIME type data by default.

However we can override this at the method level with different MIME type.

We can also specify multiple MIME types as below@Produces({“application/xml”, “application/json”})

Step 5

Build and deploy the project

Step 6

Now access the below url


we can see that XML output is produced by the Rest service.


Rest service has returned a list of User object and JAX-B has marshaled those objects into XML and sent to client.

http://localhost:8080 – it is the Server path

/RestServiceWithJerseyAndJAXB – It is the Context root of war file where the rest services are deployed.

/restURL pattern to which the Jersey server responds (configured in web.xml)

/allUserDetailsrelative URL of the rest service specified with @Path annotation.

Download this project

About the Author

Karibasappa G C (KB)
Founder of
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to

Connect with me on Facebook for more updates

Share this article on