MongoDB and Spring-Data II

And here comes the follow-up for the previous article regarding Mongo, Java and Spring-Data.

The previous topic brought us to the point where we have an initial setup and we are ready to give it a go to our webapp.

The first thing that you need before achieving this is setting up the Home Page:

And the HomeController:

The snippets of code above define the two things that we need most in our webapp, the starting page and the URL of that page. In this case the controller will handle how and when the homepage should be loaded. Since you have a viewfilter defined (there is an example of how to achieve this in the previous article) you can access the homepage jsp by simply returning the name of the view as a string such as return “home”;

To test your web application you can install the VMware vFabric plugin and drag and drop your project in the Servers view in STS.

Then if you surf in your browser at http://localhost:8080/springmvc/ you should see the page bellow (if by any chance you can’t see, please let me know and I will gladly help you):

helloworld

By now you should have the minimum required setup of a web application built with Spring MVC so it is time to get going with the Mongo required configurations.

If you have everything configured as in the examples above and in the previous article, the next steps should be straight-forward.

First of all setup the MongoConfig bean as above. This bean will configure the mongo template with the URL of the mongo instance, the database associated with the given config and the package in which it should scan for @Document annotated classes.

NOTE: If you have multiple databases configured you need to have multiple configuration classes!!!

Then you need to add your domain object that will map over a Mongo collection (traditionally called table), so that you have a place for storing documents (traditionally records/rows/etc.). That is why the class is annotated with @Document (as seen in the example bellow) and the fields are annotated with @Field.

And of course the last step of this topic is persisting the entity into the database:

The classes that handle document persisting are named repositories. Such classes need to be annotated with @Repository and are used as a wrapper over the mongoTemplate (in this case but it can be any other underlying database client that’s handled by spring-data).

So by now you should have a running instance of a SpringMVC app that can store records into a NoSQL databases – MongoDB. To test your application you can hit the URL specified above and then check your local instance of MongoDB. Refresh the page several times and you should see several records inserted.

NOTE: I will not cover mongoDB setup and installation at this time, so for the time being just head on at the MongoDB official website and follow the steps listed there.

In the next chapter we will go in-depth with the Spring-Data tutorial; since I will show you how to get ready to process BigData with a MongoDB, so we will start talking about Map-Reduce.

Leave a Reply

Your email address will not be published. Required fields are marked *