Setting a RESTful webservice for cruds operations
We’ll follow up with the SOA architechture/Restful webservices tutorials series. After learning what SOA architecure is, what Restful APIs are and how to build and consume one, we’ll pass to setting a restful webservice for cruds operations. We’ll be creating an example of a webservice for managing books in a library management system. The main purpose of this tutorial is learning how does REST APIs work. You obviously have to find more advanced tutorials/projects if you are building a REST API for real usage.
Project set up
First, you have to set your entities. I prepared this project containing the entities’ implementation and business logic code for Book and Shelf objects presented below. These represent the REST resources composing our example of a webservice:
You have to download the project as long as we’ll be working on it, we’ll add to it the implementation of each resource. Our final project hierarchy would be the following:
You will only need to add the BooksResource and ShelfResource classes through the tutorial.
Rest resources implementations
For each resource we’ll define basic CRUDS methods using the HTTP verbs POST, GET, DELETE and PUT. We’ll start with the BookResource.java where we’ll write a method that creates a new book for our API consumers. The API description here would be the following:
Request POST /books Content-Type: application/json { "isbn": "123", "title": "Cracking the coding interview", "authorName": "Gayle Laakmann McDowell", "shelf": {"id": "4"} } Response Status 201 Created | 404 Not Found
We need to write a method that gets called when consumers use a POST request to our API defined with /books path on the server, with json data as content. And they will get a response with either status “201 created” or “404 not found”.
Our implementation of this description would be the following :
@Path("books") public class BookResource { BooksBusiness booksBusiness = new BooksBusiness(); /** addBook **/ @POST @Consumes(MediaType.APPLICATION_JSON) public Response newRendezvous(Book book) { if (booksBusiness.addBook(book)) { return Response.status(Status.CREATED).build(); } else { return Response.status(Status.NOT_FOUND).build(); } } }
We’ll do the same for reading data, let’s say retreiving the list of books with a certain shelf id. API description would be this :
Request GET /books?shelfId=4 Response Status 200 Ok | 404 Not Found Content-Type: application/json [2] 0: { "isbn": "123", "title": "Cracking the coding interview", "authorName": "Gayle Laakmann McDowell", "shelf": {"id": "4", "location": "room3", "category": "Programming" } } - 1: { "isbn": "1783", "title": "The Pragmatic Programmer", "authorName": "Andy Hunt", "shelf": {"id": "4", "location": "room3", "category": "Programming" } } -
and its implementation would be :
@Path("books") public class BookResource { BooksBusiness booksBusiness = new BooksBusiness(); ... /** getBooksByShelf **/ @GET @Produces(MediaType.APPLICATION_JSON) public Response getBooksByShelf(@QueryParam("shelfId") int shelfId) { List books = new ArrayList(); if (shelfId == 0) { books = booksBusiness.getbooksList(); } else { books = booksBusiness.getbooksListByShelfId(shelfId); } if (books.size() == 0) { return Response.status(Status.NOT_FOUND).build(); } return Response.status(Status.OK).entity(books).build(); } }
This is the way to implement basic cruds for consumption on a a rest api ! code source and the video linked below would contain and explain even better the rest of the operations. We’ll pass to test what we’ve coded.
Rest resources consumption
To checkout what we’ve built we’ll need an API development tool like Postman :
The BooksBusiness class statically adds some data for test , so we’ll start with the GET request for retreiving all books saved (http://localhost:portNumber/CrudsREST/books), we have to select GET as the HTTP verb and JSON as the result data type. The result is what follows :
If we need to send data to our server through Postman we’ll do the following : ( in our case if the request passes right we’ll have a response with 201 Created status as shown below )
These are two simple examples on how to test your rest API with postman! The video linked below demos how to code and test the rest of HTTP methods.
I tried to make the whole tutorial as simple as it could be, hoping it explains how are complex restful APIs created and deployed . I insist on following up with the video for a better learning !
Recent Comments