RESTful service using Spring MVC and Hibernate - Environment Setup

Article Index

1 1 1 1 1 1 1 1 1 1 Rating 2.75 (2 Votes)


Environment Setup

  • Open eclipse and create a ‘Dynamic Web Project’ in eclipse with the name ‘catalog-service’.
  • Click next, then next and finish.
  • If you are using Maven or Gradle, setup to download libraries for Hibernate 4 or above, Spring 4.1 or above and MySQL drivers. If you are using Spring 4.1, make sure to also include the latest libraries for Jackson FastXML. The conversion of Object to JSON and vice versa is handle by Jackson library and it does not work without the latest libraries.
  • If you are not using maven or gradle, make sure the following jars are in your WEB-INF/lib folder as provided in the below screenshot

Library Jars

The source code in the GitHub contains all required libraries.

Web.xml configuration

We configure Spring DispatcherServlet, specify the patch to Spring configuration file (if not using default path and file name) and map the request to a URL pattern. For this project, we use *.json mapping. Hence, all the request URL should have .json at the end. Note that, if you perform a partial URL mapping like /product/*, you will have to configuring the ViewResolver, so that it could use the appropriate resolvers for JSON. However, when an extension mapping like .json, no ViewResolver configuration is required.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="" 
xmlns="" xsi:schemaLocation="" id="WebApp_ID" version="3.0">
  <display-name>Catalog REST Services</display-name>



Most of the tags are self-explanatory. Although this is a Spring configuration, it references Hibernate related files and hibernate.cfg.xml. The ‘datasource’ needs to be updated with your username, password and dbname. Also, note the configuration for transactionManager. Once this is configured, Spring handles the hibernate transactions and you do not have to manually manage the transaction in your code. Just mark the service method with @Transactional annotation.

The ProductDao and ProductService is also declared. We have marked them as @Autowired and hence we do not have to pass the reference for productDao into productService in the XML.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""

	<mvc:annotation-driven />
<!-- Configure JDBC Connection - @TODO update url, username and password per your database configuration -->
      <bean id="dataSource"
            <property name="driverClassName" value="com.mysql.jdbc.Driver" />
            <property name="url" value="jdbc:mysql://localhost:3306/tfcom_articles" />
            <property name="username" value="admin" />
            <property name="password" value="admin*" />
    <tx:annotation-driven transaction-manager="transactionManager" />
     <bean id="transactionManager"
        <property name="sessionFactory" ref="sessionFactory"/>
    <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource">
                  <ref bean="dataSource" />
    <property name="configLocation" value="classpath:hibernate.cfg.xml" />
	<context:component-scan base-package="com.tech_freaks.catalog"/>
	<bean id="productDao"
   	<bean id="productService"



We have used the XML configuration for hibernate and not annotations. This mainly declares the Product hibernate class and its mapping file Product.hbm.xml.

The commented fields in this file will need to be un-commented, if you are reverse engineering the hibernate object as explained in the article Reverse Engineering Hibernate objects using JBoss eclipse plugin

Joomla SEO by MijoSEF