How to POST XML List To REST Service As ArrayList

In one of the previous tutorials we saw how to POST XML data to a Jersey REST webservice.  We sent a simple xml structure which represented a book.  In this tutorial we will learn how to send a list of books to the REST service using POST method.  The service should add the books sent to the service and then return back a list which contains pre-existing books in the system along with the books sent to the service.

Here is how the service works

The REST service takes a list of Book objects as XML using POST method.  The input also contains price of the books.  The price is not processed.  It is sent just to show that we can send other elements along with book list.

The service is annotated with @POST so that POST method can be used from the client.  This service takes Xml data as input and it gets converted to Books object which has structure to match the XML input.  The Books class is a container class which contains list of book objects and a price field.  It matches with the input from the XML.

Service then retrieves a list of books from a utility class and adds the books sent to the method.  Then the list is sent back to the client.

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.

To receive the book as XML two things are needed.

The Books class should be annotated with following –

@XmlRootElement(name =”books”)

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

The Book class

should be annotated with following –

@XmlRootElement(name =”book”)

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.domain.Books;
import com.freetipscentral.utility.BookCreator;

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

  @Produces(MediaType.APPLICATION_XML)
  @POST
  @Consumes(MediaType.APPLICATION_XML)
  public List<Book> getBookAsXMLList(Books bookSet) {
    List<Book> books = BookCreator.getBooks();
    books.addAll(bookSet.getBooks());
    
    return books;
  }
}

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;
  }	
}

Books.java

The Books class should have @XmlElement(name=”book”) annotation for the books field which is the ArrayList of Book class.  This part is crucial.  This tells that books field maps to “book” element in the XML.  It also needs @XmlRootElement(name =”books”) at the class level.  The class should also be annotated with  @XmlAccessorType(XmlAccessType.FIELD) so as to avoid conflict for the annotating book.

package com.freetipscentral.domain;

import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name ="books")
@XmlAccessorType(XmlAccessType.FIELD)
public class Books {
  
  @XmlElement(name="book")
  List<Book> books;
  int totalPrice;
  
  public List<Book> getBooks() {
    return books;
  }
  public void setBooks(List<Book> books) {
    this.books = books;
  }
  public int getTotalPrice() {
    return totalPrice;
  }
  public void setTotalPrice(int totalPrice) {
    this.totalPrice = totalPrice;
  }
  
  
}

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

<books>
  <book>
  <name>The law of Karma</name>
  <serialNumber>3</serialNumber>
  </book>
  <book>
  <name>Freedom at midnight</name>
  <serialNumber>4</serialNumber>
  </book>
  <book>
  <name>Deep Work</name>
  <serialNumber>5</serialNumber>
  </book>
  <totalPrice>100</totalPrice>
</books>

 

 

Input Header

Content-Type  application/xml

Here is the Output

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<books>
    <book>
        <name>Head First Java</name>
        <serialNumber>1</serialNumber>
    </book>
    <book>
        <name>Head First Design Pattern</name>
        <serialNumber>2</serialNumber>
    </book>
    <book>
        <name>The law of Karma</name>
        <serialNumber>3</serialNumber>
    </book>
    <book>
        <name>Freedom at midnight</name>
        <serialNumber>4</serialNumber>
    </book>
    <book>
        <name>Deep Work</name>
        <serialNumber>5</serialNumber>
    </book>
</books>

Here is the Video version of this tutorial

 

One thought to “How to POST XML List To REST Service As ArrayList”

Comments are closed.