JAX-RS XML Rest Service with Jersey and JAX-B Example

In this tutorial we will show you how you can create a jersey XML rest service that will respond with XML output. This tutorial covers both jersey version 1 and 2.

Maven Dependencies

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">





Note: If you want to use jersey version 2 you need to replace the jersey-servlet dependency with the following jersey-container-servlet dependency.



This is the model that will be serialized to XML representation.

Note: That JAX-B needs a default no-args constructor to serialize your Java Object to XML representation. If you don’t have a constructor in you class than the default no-args constructor is sufficient but when you do have a constructor that takes an argument like in our example you need to add a no-args constructor.

package com.memorynotfound.rs;

import javax.xml.bind.annotation.*;

public class Notification {

    private Integer id;
    private String message;

     * Default no-args constructor needed for JAX-B
    public Notification() {

    public Notification(Integer id, String message) {
        this.id = id;
        this.message = message;

    public Integer getId() {
        return id;

    public String getMessage() {
        return message;

Rest Service

We create our Jersey XML Rest service using JAX-RS. We will give a short explanation of what the annotations are for.

  • @Path: maps the java class as a rest service to the specified path. The @Path annotation can be used on your class and/or methods. When you use this on your class this works like a base and all the methods derive from it. In our example we map our class to notification. On our method fetchBy we use a path parameter.
  • @GET: maps the method to a HTTP GET request you can also use @POST, @PUT, @DELETE.
  • @Produces: tells jersey which content type to serve in our case we serve application/xml
package com.memorynotfound.rs;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.ArrayList;
import java.util.List;

public class NotificationRestService {

    public List<Notification> fetchAll() {
        // fetch all notifications
        List<Notification> notifications = new ArrayList<Notification>();
        notifications.add(new Notification(1, "New user created"));
        notifications.add(new Notification(2, "New order created"));
        return notifications;

    @Path("{id: \\d+}")
    public Notification fetchBy(@PathParam("id") int id) {
        // fetch notification by id
        return new Notification(id, "Rise and shine.");

    public Notification create(Notification notification) {
        // create notification
        return notification;

    public void update(Notification notification) {
        // update notification

    public void delete(@PathParam("id") int id) {
        // deleting notification



Now we need to map our rest service to the jersey servlet. In our example we map the servlet to /api/*.

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
                             http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">



Note: If you want to use jersey version 2 you need to replace the servlet definition with the following:



URL: http://localhost:8080/jersey-xml/api/notifications




You may also like...