Pages

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.data.mongodb.core.MongoOperations;
import org.springframework.stereotype.Service;
import com.dimalimonov.restmongo.domain.BasicPerson;

@Service
public class PersistenceServiceImpl implements PersistenceService {

    private static final String COLLECTION_NAME="restperson";

    @Autowired
    private MongoOperations mongoOperations = null;

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

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

    @Override
    public BasicPerson update(BasicPerson bp) {
        mongoOperations.save(bp, COLLECTION_NAME);
    return bp;
    }
}

XML Configuration for the system:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/data/mongo http://www.springframework.org/schema/data/mongo/spring-mongo-1.0.xsd">

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

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

<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" /> </beans>

Explanation

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 mongoOperations.save(bp, 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"
}