Home  >  Web Services  >  RESTEasy 3

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.
build.gradle
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'war'
archivesBaseName = 'concretepage'
version = '1' 
repositories {
        mavenCentral()
}
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.
Article.java
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")
@XmlAccessorType(XmlAccessType.NONE)
public class Article {
    @XmlAttribute
    private String articleId;
    @XmlElement	
    private String title;  
    @XmlElement	
    private String author;
    @XmlElement	
    private Date date;
    @XmlElement	
    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.
ArticleService.java
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;
@Path("/manage")
public class ArticleService {
    @GET
    @Path("/article")
    @Produces("application/xml")
    public Article getArticle() {
       Article article = new Article("art11", "Hibernate","Shankar", new Date(), "google.com");	
       return article;
    }
    @GET
    @Path("/articlefmt")
    @Produces("application/xml")
    @Formatted
    public Article getArticleFormatted() {
       Article article = new Article("art12", "Spring","Vishnu", new Date(), "google.com");	
       return article;
    }
} 

Application Class

Find the application class.
ArticleApplication.java
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());
    }
    @Override
    public Set<Object> getSingletons() {
        return singletons;
    }
} 

web.xml

Find web.xml which is being used in our example.
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <context-param>
        <param-name>resteasy.servlet.mapping.prefix</param-name>
        <param-value>/post</param-value>
    </context-param>
    <servlet>
        <servlet-name>RESTEasyService</servlet-name>
        <servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
        <init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>com.concretepage.ArticleApplication</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>RESTEasyService</servlet-name>
        <url-pattern>/post/*</url-pattern>
    </servlet-mapping>
</web-app> 

Output

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">
    <title>Spring</title>
    <author>Vishnu</author>
    <date>2015-03-09T13:07:37.870+05:30</date>
    <reference>google.com</reference>
</article> 

Download Complete Source Code

POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us