JAX-RS Web Service to produce XML using JAXB

In this tutorial we are going to see how to produce XML response using JAX-RS RESTful web service with JAXB.

Create maven web application project and add Jersey dependency in pom.xml. Find Jersey 2.x maven dependency configuration below. If you are new to maven refer our maven tutorials .

Jersey 2.x dependency in pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.ehowtonow</groupId>
  <artifactId>RestTutorials</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>RestTutorials Maven Webapp</name>
  <url>http://maven.apache.org</url>
   <properties>
        <jersey2.version>2.19</jersey2.version>
        <jaxrs.version>2.0.1</jaxrs.version>
    </properties>
  <dependencies>
      <!-- JAX-RS -->
        <dependency>
            <groupId>javax.ws.rs</groupId>
            <artifactId>javax.ws.rs-api</artifactId>
            <version>${jaxrs.version}</version>
        </dependency>
        <!-- Jersey 2.19 -->
        <dependency>
            <groupId>org.glassfish.jersey.containers</groupId>
            <artifactId>jersey-container-servlet</artifactId>
            <version>${jersey2.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-server</artifactId>
            <version>${jersey2.version}</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.jersey.core</groupId>
            <artifactId>jersey-client</artifactId>
            <version>${jersey2.version}</version>
        </dependency>
  </dependencies>
  <build>
    <finalName>RestTutorials</finalName>
  </build>
</project>

JAXBXMLService.java

package com.ehowtonow.webservice;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/xml")
public class JAXBXMLService {

 @GET
 @Path("/jaxb/{id}")
 @Produces(MediaType.APPLICATION_XML)
 public Student getStudentDetail(@PathParam("id") int id) {
  Student student = new Student();
  student.setName("Ram");
  student.setId(id);
  student.setDepartment("EEE");
  student.setYear(2011);

  return student;
 }
}

@Produces(MediaType.APPLICATION_XML) annotation will return XML response. Jersey will automatically convert JAXB annotated object into XML. We will see JAXB XML Tutorials in detail in upcoming chapters.  

Student.java

package com.ehowtonow.webservice;

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

@XmlRootElement(name = "student")
public class Student {

 
 String name;
 int id;
 String department;
 int year;
 
 @XmlElement
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 
 @XmlAttribute
 public int getId() {
  return id;
 }
 public void setId(int id) {
  this.id = id;
 }

 @XmlElement
 public String getDepartment() {
  return department;
 }
 public void setDepartment(String department) {
  this.department = department;
 }
 
 @XmlElement
 public int getYear() {
  return year;
 }
 public void setYear(int year) {
  this.year = year;
 }
 
}

This class used to convert object to XML using JAXB and it will  return following XML

<student id="32">
<department>EEE</department>
<name>Ram</name>
<year>2011</year>
</student>

Configure Jersey 2.x Servlet dispatcher

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
	<display-name>Restful Webservice Example</display-name>
	<servlet>
		<servlet-name>jersey-serlvet</servlet-name>
		<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
             <param-name>jersey.config.server.provider.packages</param-name>
			<param-value>com.ehowtonow.webservice</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>jersey-serlvet</servlet-name>
		<url-pattern>/rest/*</url-pattern>
	</servlet-mapping>
</web-app>

Now you can run the service and access the service by calling the following URL

http://localhost:8080/RestTutorials/rest/xml/jaxb/32


Ask your questions in eHowToNow Forum

Post your technical, non-technical doubts, questions in our site. Get answer as soon as possible, meanwhile you can help others by answering, unanswered questions.
To Ask new Question : Ask Question
Check our existing discussions : Questions & Answers

  1. JAX-RS Get HTTP Headers using @Context
  2. Download image using JAX-RS RESTful Webservice
  3. JAX-RS @MatrixParam Restful Webservice
  4. RESTful Web service to consume JSON and Produce XML – Jersey 1.x
  5. Restful Webservice HelloWorld Jersey 2.x Example
  6. Download PDF file using JAX-RS RESTful Webservice
  7. Download Excel file using JAX-RS RESTful Webservice
  8. JAX-RS get QueryParam using @Context UriInfo
  9. JAX-RS @QueryParam with @DefaultValue – Jersey
  10. JAX-RS Send List using QueryParam Annotation
  11. JAX-RS @PathParm Web service Example
  12. File Upload using JAX-RS RESTful Webservice (Jersey 2.x)
  13. JAX-RS Get HTTP Headers using @HeaderParam
  14. JAX-RS Web service to Consume JSON using Jersey 1.x
  15. JAX-RS QueryParam – Jersey

Be the first to comment

Leave a Reply