Monday, January 27, 2014

Configuring Spring MVC with Mongo DB

Mongo DB is a popular NoSql database, that stores JSON objects.
It has few concepts to be aware of:
1. Collections - an equivalent of table in relational database
2. Document - an equivalent of a row in a table.
As a result, one can create collections and store documents inside them

Unlike relational database, structure of documents in the collection does not have to be identical.
It is common practice to have a similar structure, but it is not necessary the case

Now lets build a sample application with Mongo DB.
It will have one domain class BasicPerson and one service class PersistenceServiceImpl

Code for BasicPerson (setters and getters omitted ):
public class BasicPerson {
    private String id = null;
    private String name = null;
    private String lastName = null;

Code for PersistenceServiceImpl(interface declaration omitted ):
package com.dimalimonov.restmongo.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dimalimonov.restmongo.domain.BasicPerson;

public class PersistenceServiceImpl implements PersistenceService {

    private static final String COLLECTION_NAME="restperson";

    private MongoOperations mongoOperations = null;

    public BasicPerson save(BasicPerson bp) {
        mongoOperations.insert(bp, COLLECTION_NAME);
    return bp;

    public List find() {
       List users = mongoOperations.findAll(BasicPerson.class, COLLECTION_NAME);
    return users;

    public BasicPerson update(BasicPerson bp) {, COLLECTION_NAME);
    return bp;

XML Configuration for the system:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
xmlns:xsi="" xmlns:mongo=""

<mongo:mongo id="mongoDefault" host="" port="27017" />

<mongo:db-factory id="mongoDbFactory" dbname="rest" mongo-ref="mongoDefault" />

<bean id="mongoTemplate" class="">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" /> </beans>


What is happening here, is rather simple, if you are familiar with Spring Template mechanism
First, you create a Mongo bean. This bean represents an instance of the database server you run, so you supply it host and port
Secondly, you create a Mongo Factory instance, where you reference a database in the Mongo server. This database will be used by you to create collections and documents in
Lastly, you create a Mongo template using the factory. Template will allow you to execute calls on the db.

When the, COLLECTION_NAME); is invoked, Spring will serialize an instance of
BasicPerson into a JSON object (there is a default serializer for that) and will store it for you.
Other methods work similar using default deserialization
Interesting thing to notice is that when an object stored in Mongo, a special property _class will be stored with a class name for the object:
"_id": ObjectId("52e2b8ed8b4c2330d02d3ab8"),
"_class": "com.dimalimonov.restmongo.domain.BasicPerson",
"name": "newone",
"lastName": "newone1"