How To Post XML Data With POST Method in REST Service

The most popular data structure to submit data with POST method to a REST service is JSON.  However you may still need to post XML data.  Let us see how to do that.  Lets see how you can POST XML data with Jersey REST service.

The REST service takes a Book object using POST method.  That is done using @POST annotation.  This service takes Xml data as object and it gets converted to Book object.   Service retrieves a list of books.  To create a list of books a utility method has been created in the BookCreator class.  That class simply returns a list which contains two Book objects.  The book received from the client is added to the list and the book is then returned back.

To receive the book as XML two things are needed.

The Book class should be annotated with following –

@XmlRootElement(name =”book”)

The name attribute attribute indicates that the root element the xml data sent from the client should be named “book”.

When the XML is sent, the make sure that the XML structure has elements present in the book class.

Here are the classes you need.

PostXmlService.java

package com.freetipscentral;

import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import com.freetipscentral.domain.Book;
import com.freetipscentral.utility.BookCreator;

@Path("/postXML")
public class PostXmlService {

  @Produces(MediaType.APPLICATION_XML)
  @POST
  @Consumes(MediaType.APPLICATION_XML)
  public Book getBookAsJSON(Book book1) {
    List<Book> books = BookCreator.getBooks();
    books.add(book1);
    
    return book1;
  }
}

Book.java

package com.freetipscentral.domain;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name ="book")
public class Book {
  
  String name;
  int serialNumber;
  
  public Book() {}
  
  public Book(String name, int serialNumber) {
    this.name = name;
    this.serialNumber = serialNumber;
  }
  
  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public int getSerialNumber() {
    return serialNumber;
  }

  public void setSerialNumber(int serialNumber) {
    this.serialNumber = serialNumber;
  }

  public String toString() {
    return serialNumber + " "  + name;
  }	
}

BookCreator.java

package com.freetipscentral.utility;

import java.util.ArrayList;
import java.util.List;

import com.freetipscentral.domain.Book;

public class BookCreator {
  
  public static List<Book> getBooks() {
    List<Book> books = new ArrayList<Book>();
    Book book1 = new Book("Head First Java",1);
    Book book2 = new Book("Head First Design Pattern",2);
    books.add(book1);
    books.add(book2);
    return books;
  }

}

Here is the input Body

<book>
  <name>My Experiments With Truth</name>
  <serialNumber>3</serialNumber>
</book>

Input Header

Content-Type  application/xml

Here is the Output

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<book>
    <name>My Experiments With Truth</name>
    <serialNumber>3</serialNumber>
</book>

Here is a Video Tutorial I created for the above steps