Using REST to Manage Books for a Bookstore
This REST Bookstore Sample Using a Database shows how to use the RESTful service to add, delete, update, and retrieve books from Bookstore.
Prerequisites
Install the latest Google Chrome version at the standard default location.
- Download and install PostgreSQL from http://www.postgresql.org/download/.
- Go to SQL Shell (psql) from the installed program.
- The PostgreSQL command line window starts.
- Open the DB and table creation script, dbsetup.sql from the scripts folder under TIBCO_HOME/bw/n.n/samples/binding/rest/BookStore/scripts.
- Run the script from the command line as per the instructions given in the readme file present under the scripts folder.
You see the database and the tables created from the pgAdmin UI from the installed program.
Procedure
Result
The web page lists the following operations for Books and Events:
Books
GET books returns an output similar to the following:
{ "Book": [ { "isbn": "0061122416", "name": "The Alchemist", "description": "Every few decades a book is published that changes the lives of its readers forever. The Alchemist is such a book", "authorName": "Paul Coelho", "releaseDate": "2006-04-25", "vintage": true, "signed": true, "price": 11.9 }, { "isbn": "0071450149", "name": "The Power to Predict", "description": "How Real Time Businesses Anticipate Customer Needs, Create Opportunities, and Beat the Competition", "authorName": "Vivek Ranadive", "releaseDate": "2006-01-26", "vintage": false, "signed": true, "price": 15.999 } ] }
GET books by ISBN returns an output similar to the following for the ISBN 0061122416:
{ "isbn": "0061122416", "name": "The Alchemist", "description": "Every few decades a book is published that changes the lives of its readers forever. The Alchemist is such a book", "authorName": "Paul Coelho", "releaseDate": "2006-04-25", "vintage": true, "signed": true, "price": 11.9 }
The books.log file is generated with the following information:
POST Books------->{"Book":[{"isbn":"1451648537","name":"Steve Jobs","description":"Biography of Apple Co-Founder Steve Jobs","authorName":"Walter Isaacson","releaseDate":"2012-10-24","vintage":false,"signed":false,"price":21},{"isbn":"0385537859","name":"Inferno","description":"Robert Langdon returns in Dan Brown's latest fast paced action thirller","authorName":"Dan Brown","releaseDate":"2013-05-14","vintage":false,"signed":true,"price":14.09},{"isbn":"0399103421","name":"The Godfather","description":"The Godfather is an epic story of a New York's top mafia family, loyalty, and how men of honor live in their own world, and die by their own laws.","authorName":"Mario Puzo","releaseDate":"1969-03-10","vintage":true,"signed":true,"price":50}]}************************************************************** GET Books------->{"Book":[{"isbn":"1451648537","name":"Steve Jobs","description":"Biography of Apple Co-Founder Steve Jobs","authorName":"Walter Isaacson","releaseDate":"2012-10-24+05:30","vintage":false,"signed":false,"price":21},{"isbn":"0385537859","name":"Inferno","description":"Robert Langdon returns in Dan Brown's latest fast paced action thirller","authorName":"Dan Brown","releaseDate":"2013-05-14+05:30","vintage":false,"signed":true,"price":14.09},{"isbn":"0399103421","name":"The Godfather","description":"The Godfather is an epic story of a New York's top mafia family, loyalty, and how men of honor live in their own world, and die by their own laws.","authorName":"Mario Puzo","releaseDate":"1969-03-10+05:30","vintage":true,"signed":true,"price":50}]}************************************************************** GET Book By ISBN------->{"isbn":"1451648537","name":"Steve Jobs","description":"Biography of Apple Co-Founder Steve Jobs","authorName":"Walter Isaacson","releaseDate":"2012-10-24+05:30","vintage":false,"signed":false,"price":21}************************************************************** DELETE Book By ISBN-------->"Deleted book with ISBN - 1451648537"************************************************************** GET Events By ISBN---->{}************************************************************** GET Books By Price More than 10---->{"Book":[{"isbn":"0385537859","name":"Inferno","description":"Robert Langdon returns in Dan Brown's latest fast paced action thirller","authorName":"Dan Brown","releaseDate":"2013-05-14+05:30","vintage":false,"signed":true,"price":14.09},{"isbn":"0399103421","name":"The Godfather","description":"The Godfather is an epic story of a New York's top mafia family, loyalty, and how men of honor live in their own world, and die by their own laws.","authorName":"Mario Puzo","releaseDate":"1969-03-10+05:30","vintage":true,"signed":true,"price":50},{"isbn":"0385537829","name":"Angels and Demons","description":"Robert Langdon returns in Dan Brown's fast paced action thirller","authorName":"Dan Brown","releaseDate":"2013-05-14+05:30","vintage":false,"signed":true,"price":15.09},{"isbn":"0399103439","name":"The Godfather1","description":"The Godfather is an epic story of a New York's top mafia family, loyalty, and how men of honor live in their own world, and die by their own laws.","authorName":"Mario Puzo","releaseDate":"1969-03-10+05:30","vintage":true,"signed":true,"price":50}]}************************************************************** GET Books By Price More than 50---->{"Book":[null]}**************************************************************
Understanding the Configuration
The project contains three processes: Books, Events, Invoke_Client, RESTInvoke.bwp and two subprocesses; BooksDB and EventsDB.
Books Process: In this process, the Book REST service performs the post, get, put, delete operations. It posts one or more books to the bookstore and retrieves all the books from the bookstore. For the post operation, a sub process is called to add books to the database. The addBooks operation is implemented in the BooksDB subprocess. The Book REST service performs three operations: getting a book from the bookstore by ISBN, updating a book in the bookstore using ISBN, and deleting a specific book from the bookstore using ISBN. ISBN is passed as a parameter to these operations. The booksbyprice service gets books from the bookstore using price, which is passed as a query parameter to the GET operation. This service responds with an array of Books if there are books whose price is greater than the provided input (price). If there are no such books, the service returns a Null array.
BooksDB Subprocess: The subprocess handles deletes, updates, retrievals of books from the database. The BooksPersist service implements the following operations: adding books to database, getting all books from database, getting a book by ISBN, updating a book by ISBN, deleting a book by ISBN and getting a book by price. Using BooksPersist/addBooks, books are added to the database.
Events Process: It is similar to the Books process and is used to add, delete, update, and retrieve events.
EventsDB Process: It is similar to the BooksDB process and is used to add, delete, update, and retrieve events from the database.
Invoke_Client Process: This is a REST Client process which uses Invoke REST API activity to retrieve data from the REST Server. It is similar to Swagger UI. Using this process, you can POST, GET, PUT, and DELETE books by specifying the HTTP Client, Resource Path, HTTP Method, Request Type, and Response Type in the Invoke REST API activity.
For example, in POST_Books, we provide HTTP Client Resource which listens to the same host and port as that of server. The Resource Path is "/books", HTTP Method is "POST", and Request and Response type is JSON. In the Message Body, we provide the JSON payload which is read from the file present at BW_HOME\samples\binding\rest\BookStore\samplejson\books.json. This posts books from the books.json payload into the database and you can view the data posted into the output file.
RESTInvoke Process:This is a REST Client process which uses the REST Invoke activity to retrieve data from the REST Server. In addition to invoking the existing services, it invokes the service getBooksByPrice which demonstrates null and array responses.
Troubleshooting
- If you are unable to insert rows into the database using the dbsetup.sql script in the scripts folder, use the sample json payload from the samplejson folder to post the data.
- If you do not see the REST Swagger API page, verify whether the application has started.
- If you see any problem markers in the project, clean the project by invoking or try switching to a clean new workspace.
- If you get any File was not found exception, verify whether books.json and book_put.json file is present at the location as specified in the Input_File and Input_File_1 Module Property.
- If you get an error about unregistered user, open the pgAdmin UI and select all check boxes under the Role Privileges tab for the bwuser.