RESTEasy + JAXB xml example

RESTEasy is JBOSS provided implementation of JAX-RS specification for building RESTful Web Services and RESTful Java applications. Though this is not limited to be used in JBOSS only, and you can use with other servers also.

JAXB on the other hand is used for mapping java classes to equivalent xml documents and vice versa. It is done using marshalling and and unmarshalling features of JAXB.

In this post, I am demonstrating the way to use JAXB with RESTEasy, to convert the API response in xml format.

Environment used:

  1. RESTEasy 2.3.1.GA
  2. RESTEasy JAXB provider 2.3.1
  3. Tomcat 7
  4. JDK 1.6

Follow below steps to build a demo application.

1) Create a maven project and convert to eclipse web project

mvn archetype:generate -DgroupId=com.howtodoinjava -DartifactId=RESTfulDemoApplication 
-DarchetypeArtifactId=maven-archetype-webapp -DinteractiveMode=false

mvn eclipse:eclipse -Dwtpversion=2.0

2) Update runtime dependencies in pom.xml

<project xmlns="" xmlns:xsi=""
  <name>RESTfulDemoApplication Maven Webapp</name>
    <!-- core library -->
	<!-- JAXB support -->

3) Update web.xml file for resteasy specific servlet mapping

 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "" >

  	<display-name>Archetype Created Web Application</display-name>
  	<!-- Auto scan REST service -->

4) Write a service class and model class

package com.howtodoinjava.model;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name = "user")
public class User implements Serializable {
    private static final long serialVersionUID = 1L;
    @XmlAttribute(name = "id")
    private int id;
    @XmlElement(name = "firstName")
    private String firstName;
    @XmlElement(name = "lastName")
    private String lastName;
    public int getId() {
        return id;
    public void setId(int id) { = id;
    public String getFirstName() {
        return firstName;
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    public String getLastName() {
        return lastName;
    public void setLastName(String lastName) {
        this.lastName = lastName;

package com.howtodoinjava.service;


import com.howtodoinjava.model.User;

public class UserManagementModule
	public Response getUserById(@PathParam("id") Integer id)
		User user = new User();
		return Response.status(200).entity(user).build();

5) Run the application

When we deploy above built application in tomcat and hit the URL: ” http://localhost:8080/RESTfulDemoApplication/user-management/users/10″, below is the response.


RESTEasy JAXB Example

RESTEasy JAXB Example


Download Source code


Happy Learning !!

9 thoughts on “RESTEasy + JAXB xml example”
  1. 1.)Write a java program to create url and access two functions like [ getoffer() and getvaluefrom() ].
    a.)in getoffer(accesing 3 variables) should be in string format ,which stores values in xml format and it returns in html output.
    b.)in getvaluefrom() should return string from saved xml and once done the xml content should be emptied.

    To test the program we can use advanced rest client.

    Can u provide me with necessary codes for solving the above description .

  2. Lokesh , thanx for ur posts its very usefull.
    how can we implement it for a response have multiple user objects(multiple xml elements)

  3. Lokesh, I am trying to learn REST webservices. Is it possible to fetch the values from database instead of hardcoding it. Do you have any examples to get the values from db? If so please let me know.

    1. I am not sure why it is difficult for you. REST works at View layer, once you got the request, you are free to do anything you want with it.. just like any other application. Try first.

Note:- In comment box, please put your code inside [java] ... [/java] OR [xml] ... [/xml] tags otherwise it may not appear as intended.

Leave a Reply

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