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.

Ask your questions in Our Forum

To Ask new Question : Ask Question
Check our existing discussions : Questions & Answers

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">
  <name>RestTutorials Maven Webapp</name>
      <!-- JAX-RS -->
        <!-- Jersey 2.19 -->


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;

public class JAXBXMLService {

 public Student getStudentDetail(@PathParam("id") int id) {
  Student student = new Student();

  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.  


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;
 public String getName() {
  return name;
 public void setName(String name) {
  this.name = name;
 public int getId() {
  return id;
 public void setId(int id) {
  this.id = id;

 public String getDepartment() {
  return department;
 public void setDepartment(String department) {
  this.department = department;
 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">

Configure Jersey 2.x Servlet dispatcher

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

	<display-name>Restful Webservice Example</display-name>


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


Ask your questions in Our Forum

To Ask new Question : Ask Question
Check our existing discussions : Questions & Answers

Posts You may Like

  1. Java 7 Try statement with resources
  2. How to reverse the String using StringBuffer
  3. How to format the String by using Locale
  4. Log4j Example using Property File
  5. Java Best Practice Reuse Objects instead of creating new ones if possible
  6. Java 8 Sort Map by Key
  7. Load and Parse HTML file using JSoup
  8. How to find min and max value from Collection
  9. JAX-RS Get HTTP Headers using @Context
  10. JAX-RS @PathParm Web service Example
  11. Java 8 Stream Collectors groupingBy with sort Example
  12. JSoup to parse body fragment
  13. Java Best Practice Naming Conventions
  14. Remove the particular character from string
  15. How to Count number of Element in XML

Be the first to comment

Leave a Reply

Your email address will not be published.