Skip to the content.

Back in the pre Spring 3.1 days, we had to configure each and every bean into one of the spring configuration files which only happens to be an XML. And so if we were building a web application with Spring which is also using a relational database to store all its data, we needed to create a bean tag into one of those spring configuration XML files for reading out database connection properties to create a data source bean, which we then feed as a parameter to yet another bean tag to create a database session factory bean.

In this post we will be building a small restful web application with Spring MVC integrated with Hibernate to fetch the user record from MySQL database server and display it as a JSON response on the browser, without writing a single XML file. Furthermore we will also be doing away with tomcat’s web.xml leveraging latest Servlet 3.1 APIs and doing all of that configuration in our Java classes. So what are we waiting for!

Configuring Spring and Hibernate Integration

We can start by making a maven project with webapp archetype into any of our favourite IDE. And in its pom.xml file we can give all the dependencies as per this gist. Notice that as one of the properties we have to set failOnMissingWebXml to be false so that we can safely delete web.xml file.

Now we can start with configuring DispatcherServlet for our Spring MVC application which we can do in a single class:

public class AppConfig extends AbstractAnnotationConfigDispatcherServletInitializer {

  protected Class<?>[] getRootConfigClasses() {
    return new Class[] { HibernateConfig.class };

  protected Class<?>[] getServletConfigClasses() {
    return new Class[] { WebMvcConfig.class };

  protected String[] getServletMappings() {
    return new String[] { "/" };


A couple of points to note here:

This is all we need to configure our DispatcherServlet, now let’s look into and In we need to create all the beans that are necessary for handling interaction with database via Hibernate.

public class HibernateConfig {

  public LocalSessionFactoryBean getSessionFactory() throws PropertyVetoException {
    LocalSessionFactoryBean bean = new LocalSessionFactoryBean();

    Properties hibernateProperties = new Properties();
    hibernateProperties.put("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
    hibernateProperties.put("hibernate.show_sql", "true");

    return bean;

  public ComboPooledDataSource getDataSource() throws PropertyVetoException {
    ComboPooledDataSource dataSource = new ComboPooledDataSource();


    return dataSource;

  public JdbcTemplate getJdbcTemplate() throws PropertyVetoException {
    JdbcTemplate template = new JdbcTemplate();    	
    return template;

  public HibernateTransactionManager getTransactionManager() throws PropertyVetoException {
    HibernateTransactionManager transactionManager = new HibernateTransactionManager();
    return transactionManager;


In we need to create web specific beans, so as we are creating restful endpoints we would need appropriate message converters that would be required to convert Java objects to their proper JSON representations.

public class WebMvcConfig extends WebMvcConfigurationSupport {

  public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {

  public MappingJackson2HttpMessageConverter customJackson2HttpMessageConverter() {
    MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    return jsonConverter;


Implementing REST and Persistence Layers

Now that we have configured all the important parts of our application we now need to implement a REST controller and corresponding Service and DAO that are quite straightforward.

public class UserController {

  private UserService service;

  @RequestMapping(value = "{userId}", method = RequestMethod.GET)
  public @ResponseBody ServerResponse getUser(@PathVariable("userId") Long userId) {
    return this.service.getUser(userId);
public class UserDaoImpl implements UserDao {
  private SessionFactory sessionFactory;

  public UserDTO getUser(Long userId) {
    TypedQuery<UserDTO> typedQuery = sessionFactory.getCurrentSession().createQuery("from UserDTO where id=" + userId.toString());
    return typedQuery.getSingleResult();


And finally our service class is doing some mission critical stuff!

public class UserServiceImpl implements UserService {

  private UserDao userDao;

  @Transactional(readOnly = true)
  public ServerResponse getUser(Long userId) {
    ServerResponse response = new ServerResponse();

    UserDTO dto = userDao.getUser(userId);
    response.setUser(new User(dto.getFirstName() + " " + dto.getLastName(), 
                        new Long(dto.getAge())));

    return response;


Database schema

Lastly for the application to work we also need to create database schema in our MySQL server:

create table if not exists `users` (
  `id`		int not null auto_increment,
  `firstName`	varchar(20),
  `lastName`	varchar(20),
  `age`		int,
  primary key (`id`)
insert into `users` values (1, "john", "doe", 29);

Source Code

I have not put all of the code on this post, but you can check out the working project from this github repository.