JAX-RS RESTEasy 3 JAXB Provider Example for XML with Gradle and Tomcat

By Arvind Rai, March 09, 2015
In this page, we will discuss JAX-RS RESTEasy 3 JAXB provider example for XML with Gradle and Tomcat. RESTEasy implements JAXB specification and provides JAXB provider. To work with XML, we need to create an entity which will use JAXB annotations to define our XML root tag , child tag and attributes. To use this entity, JAX-RS methods should be defined with XML media type. RESTEasy also provides @Formatted annotation that formats XML response. We will see here complete example with explanation.

Required Software for Demo

In our demo, we are using following software.
1. Java 7
2. Tomcat 7
3. Eclipse
4. Gradle

Gradle File to Resolve JAR Dependency

Find the Gradle file to resolve RESTEasy and JAXB provider JAR dependency.
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'war'
archivesBaseName = 'concretepage'
version = '1' 
repositories {
dependencies {
	compile 'org.jboss.resteasy:resteasy-jaxrs:3.0.10.Final'
	compile 'org.jboss.resteasy:resteasy-jaxb-provider:3.0.10.Final'

Create Entity for XML

To work with XML, we need to create a java class. The class will use different JAXB annotations.

@XmlRootElement: Defines the root of XML. By default the class name is taken as root element name. If we want to assign another name, we can use name attribute of this annotation.
@XmlAccessorType: Defines accessor type. It can be NONE, FIELD, PROPERTY, PUBLIC_MEMBER.
@XmlAttribute: Defines the tag attribute.
@XmlElement: Defines child tag in XML.

Find the entity.
package com.concretepage;
import java.util.Date;
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 ="article")
public class Article {
    private String articleId;
    private String title;  
    private String author;
    private Date date;
    private String reference;
    public Article(){}
    public Article(String articleId, String title,  String author, Date date, String reference){
    	this.articleId = articleId;
    	this.title = title;
    	this.author = author;
    	this.date = date;
    	this.reference = reference;

Create Service Class

In service class, we need to create JAX-RS methods with XML media type.

@Produces("application/xml"): To respond XML format, JAX-RS method should have XML media type.
@Formatted: RESTEasy provides this annotation to format the result with new line.

Find the service class.
package com.concretepage;
import java.util.Date;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import org.jboss.resteasy.annotations.providers.jaxb.Formatted;
public class ArticleService {
    public Article getArticle() {
       Article article = new Article("art11", "Hibernate","Shankar", new Date(), "google.com");	
       return article;
    public Article getArticleFormatted() {
       Article article = new Article("art12", "Spring","Vishnu", new Date(), "google.com");	
       return article;

Application Class

Find the application class.
package com.concretepage;
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.core.Application;
public class ArticleApplication extends Application {
    private Set<Object> singletons = new HashSet<Object>();
    public ArticleApplication() {
        singletons.add(new ArticleService());
    public Set<Object> getSingletons() {
        return singletons;


Find web.xml which is being used in our example.
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"


To check output, we need to run the URL as http://localhost:8080/concretepage-1/post/manage/article.
We will get response as below.
JAX-RS RESTEasy 3 JAXB Provider Example for XML with Gradle and Tomcat
Check the view source, the response will be in single line as
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><article articleId="art11"><title>Hibernate</title><author>Shankar</author><date>2015-03-09T13:04:24.218+05:30</date><reference>google.com</reference></article> 
Now we will test response with @Formatted annotation. Run the URL as http://localhost:8080/concretepage-1/post/manage/articlefmt.
and check the view source. We will get formatted response.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<article articleId="art12">

Download Complete Source Code


©2023 concretepage.com | Privacy Policy | Contact Us