Friday, October 4, 2013

REST Service on JBoss 7 (using RESTEasy) - Part 2

In this post we are going to enhance our REST service (which we developed in the last post) to return a user defined object instead of java.lang.String or any other primitive type.

1. Create a class called Book and paste the following contents into it.
package org.jbosstest.jbosstest;

import java.io.Serializable;

public class Book implements Serializable {

    private String name;
    private String author;
    private float price;

    public Book() {
    }

    public Book(String name, String author, float price) {
        this.name = name;
        this.author = author;
        this.price = price;

    }

    public String getName() {
        return name;
    }

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

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public float getPrice() {
        return price;
    }

    public void setPrice(float price) {
        this.price = price;
    }

}


2. Create a class called LibraryService and paste the following contents into it.
package org.jbosstest.jbosstest;

import java.util.ArrayList;
import java.util.List;
import javax.ejb.LocalBean;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@LocalBean
@Path("/library")
public class LibraryService {

    @GET
    @Path("available-books")
    @Produces({MediaType.APPLICATION_JSON})
    public List<Book> getAvailableBooks() {
        List<Book> books = new ArrayList<Book>();
        books.add(new Book("Java The Complete Reference, 8th Edition", "Herbert Schildt", 479));
        books.add(new Book("Jboss As 7 Configuration, Deployment, And Administration", "Francesco Marchioni", 450));
        books.add(new Book("RESTful Java with JAX-RS 2.0 2ed", "Bill Burke", 2018));
        return books;
    }
}

3. Build and run your application
4. Open chrome/chromium
5. Install JSON formatter, from chrome store, if you havent installed.
6. Hit the url http://localhost:8080/JbossTest/webresources/library/available-books

You will see the following screen.
Suppose your consumers are XML clients; they want response in XML format. What do you do?

1. Add  @XmlRootElement annotation to the Book class.

@XmlRootElement
public class Book implements Serializable {
     // the body remains same
}

2. Add MediaType.APPLICATION_XML as one of the produced type in LibraryService.getAvailableBooks() method.

...
public class LibraryService {

    @GET
    @Path("available-books")
    @Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
    public List<Book> getAvailableBooks() {
        ...
    }
}

3. Hit the url http://localhost:8080/JbossTest/webresources/library/available-books once again.
4. Now you will get the response in XML format as appears below.


Now you see that your service is capable of outputting in both XML and JSON formats. Use poster (for Firefox) or postman (for chromium/chrome) to choose the response type of your choice.