RESTful service using Spring MVC and Hibernate

1 1 1 1 1 1 1 1 1 1 Rating 0.00 (0 Votes)


Create a RESTful service using Spring MVC and Hibernate for fetching, adding, updating and deleting product information.  The product information will be persisted in a database table. The service should be available to be consumed by any client like mobile, html, java application etc. using the REST url created.

GitHub Project URL


  • MySQL and Tomcat is already installed
  • Eclipse with JEE support is already installed
  • Basics about Spring and Hibernate framework
  • Basics about RESTful service

Analysis and Design

We will use a bottom-up approach to design our application. We start from the database layer, then hibernate layer and finally the Spring layer.


For implementing this requirement, we create a table PRODUCT. It has basic information about a product like name, description, partnumber, unit price etc. We do not have any more table for fulfilling this requirement. However, if you create a complex application, your product information might be normalized into multiple related tables.

Below is the CREATE table statement for creating the PRODUCT table.

  `product_id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) NOT NULL,
  `partnumber` varchar(50) NOT NULL,
  `description` varchar(1024) NOT NULL,
  `thumbnail_url` varchar(240) DEFAULT NULL,
  `buyable` char(1) NOT NULL DEFAULT 'Y',
  `unitprice` decimal(8,2) NOT NULL,
  PRIMARY KEY (`product_id`)


There is two sql script in the github repository root folder create_tables.sql and product_load.sql. As the name suggests, the first sql can be used to create a database and PRODUCT table. The second sql can be run to insert some starter products.


Nothing to think about here! We will have a hibernate class which will be mapped to PRODUCT table. If you would want to reverse engineer the from the database table, review the steps in the article Reverse Engineering Hibernate objects using JBoss eclipse plugin


We will have a single Controller, which will have different methods for each verb like GET, PUT, POST and DELETE and will act upon Product table data. Following the Spring convention, we will interact with the database using the following layered approach

Spring Controller -> Service -> DAO -> Hibernate -> Database

Spring Controller:



Furthermore, the Rest controller will have the following methods

@RequestMapping(method = RequestMethod.GET)
public List<Product> getProducts(); - Get all products

@RequestMapping(value = "/{productId}", method = RequestMethod.GET)
public ResponseEntity<?> getProduct(@PathVariable Integer productId) – Get Products with ProductId

@RequestMapping(method = RequestMethod.POST)
public ResponseEntity<?> add(@RequestBody Product input) – Insert a product for input received

@RequestMapping(value = "/{productId}", method = RequestMethod.PUT)
public ResponseEntity<?> update(@PathVariable Integer productId,
@RequestBody Product input) – Update product information for productId

@RequestMapping(value = "/{productId}", method = RequestMethod.DELETE)
public ResponseEntity<?> remove(@PathVariable Integer productId) – Delete product for the provided productId


The above also shows at a high level usage of Spring annotations for specifying different verbs.

Word of caution: This design allows a REST client to insert, delete or update records in database, without any authentication and authorization. For a production application, generally, the only verb which might allow access without authorization is the GET. All the other verbs which modify the database might be authenticated and authorized. Oauth is a commonly used open standard of authorization used to authorize client before they can make changes to the underlying data source.

We will need to further create XML configuration file for web application, Spring framework and add libraries. We will cover this in environment setup and development sections.

Joomla SEO by MijoSEF