Spring Data MongoDB example with Spring MVC 3.2

Here is another example web application built with Spring MVC 3.2 and Spring Data 1.2, integrating with the MongoDB document database.

STEP 1: Create new webapp project, I will use maven for this. (Note: I am on my macbook with Maven 3 and Java 6 installed.)

mvn archetype:generate

You could create the maven project directly in your IDE as well. But I usually create it on the terminal and import it in eclipse by using the following command (Note: The following command is run within your newly created project directory, i.e. run -> cd HelloSpringWithMongoDB)

mvn eclipse:eclipse -Dwtpversion=2.0

STEP 2: Add Spring Framework 3.2 and Spring Data 1.2 dependencies to your pom.xml

STEP 3: Update your web.xml (src/main/webapp/WEB-INF/web.xml) to use Spring’s DispatcherServlet

STEP 4: Add your spring configuration to the dispatcher-servlet.xml

  • Use MongoFactoryBean to connect to the MongoDB instance.
  • Use MongoTemplate to connect and make queries to the database.

STEP 5: Create a model (using Person as an example), service and controller in the new source directory src/main/java


Controller for the CRUD operations

STEP 5: Create a JSP Page in the folder WEB-INF/jsp called output.jsp (This will currently invoke create and delete).

STEP 6: That’s it! Its time to run your project. You could either run directly from eclipse or you could run “mvn package” to build a war file and deploy it to your application server. I tested this on tomcat running on port 8080 (http://localhost:8080/HelloSpringWithMongoDB/person) and I could store and delete person with provided names. Working! yeah.. Here is a picture of me playing with the app 🙂

Spring Data MongoDB Spring MVC 3.2 Example App

Spring Data MongoDB Spring MVC 3.2 Example App

You can view or download the full project code at https://github.com/manishchhabra/HelloSpringWithMongoDB


  • Is there a dependency missing from your pom above? The package: org.springframework.data.annotation doesn’t appear to be pulled in by any of those indicated. Also, did you happen to run across any JodaTimeConverter issues?

    thanks for the post – very helpful!

    • Prateek Ashtikar

      Hi, I’ve run this program with same dependencies and its running for me. It’s a mavenized project and you check for all jars under the directory named as “Maven Dependency”

  • Just confirmed it; to get this to build and run, you’ll need to add:


  • Manish

    Hi eugene,

    I am glad the post was helpful. After your comment, I checked out the code again from github and performed a build. The missing library (spring-data-commons-1.5.0.RELEASE.jar) you are talking about gets pulled in automatically because spring-data-mongodb artifact depends on it.

    You will not have to explicitly add that dependency. I tested again and it is working fine without any issues.


    • Prateek Ashtikar

      Hi, I think “spring-data-commons-1.5.0.RELEASE.jar” will be downloaded with the depency of “spring-data-mongodb-1.2.0.RELEASE.jar”. It’s a kind of parent child relationship of jars….Please check for “Dependency Hierarchy” of pom.xml file..

  • Maage

    I’m getting a 404 error on the /person page. I downloaded it straight from your github.

    • Manish

      Hi Maage,

      Did you see any errors in tomcat logs? Or are you using tomcat or some other application server to run the application?

      I tested once again just now. I was able to successfully run the application and it was accessible at http://localhost:8080/HelloSpringWithMongoDB/person.

      If you are facing issues, could you share more details on how you are executing it and your development environment?


      • Maage

        Hey Manish,

        Thanks for responding back. I ran this on Fabric through Spring Tool Suite. I downloaded the source code, imported as an existing Maven project, then did a Maven Clean, Maven Install, and then I ran it on Fabric. The index page showed up just fine, actually, but the /person page did not. I’m completely baffled by this.

        • hendisantika

          Me too.

          I have the same problem like Manish.

          How do I solve that?


          • Manish

            Hi hendisantika,

            As mentioned in the next post, it worked without any change when the whole thing was retried.

            Do you see any errors in your logs?


  • Maage

    Okay, I retried this and everything worked this time. I guess I must have did something wrong last time. I’m sorry for the trouble!

    Thanks for this tutorial, it’s managed to explain lucidly what the other tutorials couldn’t. I really appreciate it.

    • Manish

      Happy to hear it worked for you. I am glad that the tutorial was able help.

      Thanks for your comment.

  • chaitanya

    Hi manish,
    I have a problem with the maven archetype web-app. I dont have src/main/java root directory not getting created. only src/main/resources root directory is created. please provide me the info for the correct maven archetype.

  • Marco

    Hi there, I’m trying your example and i got an error on output.jsp

    can not find the tag library descriptor…

    any idea how can i fix it?

    thanks for this post

  • Sandeep Dange

    Hi manish, I have downloaded your project straight from github and imported as maven project in my eclipse. everything works fine as a application. but the data is not getting stored in my mongoDB. Please tell me necessary changes that i had to do in dispatcher-servlet.xml

    • Manish

      Hi Sandeep,

      This is quite strange, as you say the application is running fine. So can you add the data from the application and it works and does not throw any error? But the data is not getting stored in a MongoDB Collection?

      I have just tried the app myself and it seems to be working fine. Which version of MongoDB are you using? I can try the same.


  • grails

    Thanks for the code. I wish there will be a Java framework where I could do Javascript on the server side. It is so cool what Node.js people do. It’s all the same from mongodb, to nodejs, to front end programming.

  • Sravan

    Thanks Manish.Very helpful…


  • Hi,

    I would like to know with your sourcecode how could i change the mongo default port to a different port with the attributes in the mongo template bean. Is there a way?

  • Bharat Savani

    Hello manish, You have provided the tutorial that I was searching on web for mongodb. Thank you very much.

    I have installed tomact 6.0 and I am using spring tool suite.
    When I run the project on browser then I get err 404 page not found.
    Can you tell me what to do to solve this error

    • Manish

      404s are generally issues with the deploy.
      1. How are you running your application in tomcat?
      2. Is your application being loaded when you start up tomcat?
      3. Do you see any error when tomcat is starting up?

  • Fei

    Thanks, Manish
    It works on Tomcat 7 and IBM WebSphere Application Server 8.0 like a charm.

  • Can you please post bean configuration xml using Java Annotation, that way is more simple and easy and intuitive.

  • Daniel Mendoza

    Hi, thanks for the example but it doesn’t work with GlassFish Server 4.0.

    I get a 404 with /person/save

    • Daniel Mendoza

      Sorry I was doing something wrong, now it works with GlassFish, thanks.

  • Rengith

    Hi Manish, How are you handling mutilple checkboxes(eg: Label has checkboxes, user has to select mutilple checkboxes), All these multiple checkboxes values are stored in embedded array along with other values.

    eg: Input from Jsp

    Name: “xyz” (Text filed mapped in to name bean attribute)
    Tag: [games, movies, surfing] – Selected from multiple checkboxes(mapped in to tag bean attribute)

    model would be

    public class ABC {

    private string name;
    getter and setter method

    private ? tag
    getter and setter method

    Mongodb inserted data would be

    abc({“name”: “xyz”, tag : [“games”, “movies”, “surfing”] )}

    Please assist.

  • Ajeet S Sachan

    For All other people who are still facing issues with this sample, here is the solutions for them:

    Issue-1. “Cannot change version of project facet Dynamic Web Module to 2.5”

    This error is due to web.xml specification follows 2.5 while maven-webapp archtype creates for 2.3.

    Follow any of the link that works for you:





    Issue-2. “404” By Tomcat while running the application

    Dependency configuration given in pom.xml are getting updated by maven plugin for having attribute

    “provided” for spring-webmvc & spring-core library, hence not getting included while creation of WAR

    file. remove those “provided” & let it be for compile.

    • Manish

      Thanks Ajeet.

    • aayush

      Can you please elaborate as to how to remove the error please. I am unable to understand what you have written and have been stuck at this HTTP STATUS 404 error since 2 days.

    • aayush

      Hi Ajeet,
      I figured out what you wrote and added compile to the spring-webmvc & spring-core dependency. Yet, its giving the same HTTP STATUS 404 (The requested resource is not available) error. Kindly suggest a solution

  • Raj

    Nice Article Manish. Do you know if spring supports programmatically creating the database, meaning I wont be populating the database name in context.xml? In my scenario I need to the create the database on-the-fly (if it doesn’t exist already) based on the request from UI and load data into collection in that database.


  • Gaurav Ashara

    I have question regarding, is there any requirement to close mongodb connection ? if yes then how to manage connection with Spring data and also mongodb java driver .


  • vijay

    hi, where should I place dispatcher servlet.xml file in eclipse IDE Folder structure

  • aayush

    Hi manish,
    I am getting the ‘HTTP Status 404’ error on the HelloSpringWithMongoDB/person page. I am running it on Tomcat v7.0 in Eclipse. There are no errors shown in my Console of the server. I load this by right clicking and then chosing the ‘Run as’ option wherein I select the ‘Run on server’ option for this project.
    Please help me out since I have spent 2 days on it and I cannot understand what is the problem.

    Here’s the log:

    Mar 27, 2015 2:37:43 PM org.apache.catalina.core.AprLifecycleListener init
    INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /Users/avarshney/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.
    Mar 27, 2015 2:37:44 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
    WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property ‘source’ to ‘org.eclipse.jst.jee.server:HelloSpringWithMongoDB’ did not find a matching property.
    Mar 27, 2015 2:37:44 PM org.apache.coyote.AbstractProtocol init
    INFO: Initializing ProtocolHandler [“http-bio-8081”]
    Mar 27, 2015 2:37:44 PM org.apache.coyote.AbstractProtocol init
    INFO: Initializing ProtocolHandler [“ajp-bio-8009”]
    Mar 27, 2015 2:37:44 PM org.apache.catalina.startup.Catalina load
    INFO: Initialization processed in 956 ms
    Mar 27, 2015 2:37:44 PM org.apache.catalina.core.StandardService startInternal
    INFO: Starting service Catalina
    Mar 27, 2015 2:37:44 PM org.apache.catalina.core.StandardEngine startInternal
    INFO: Starting Servlet Engine: Apache Tomcat/7.0.34
    Mar 27, 2015 2:37:47 PM org.apache.catalina.core.ApplicationContext log
    INFO: No Spring WebApplicationInitializer types detected on classpath
    SLF4J: Failed to load class “org.slf4j.impl.StaticLoggerBinder”.
    SLF4J: Defaulting to no-operation (NOP) logger implementation
    SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
    Mar 27, 2015 2:37:47 PM org.apache.catalina.core.ApplicationContext log
    INFO: Initializing Spring root WebApplicationContext
    Mar 27, 2015 2:37:48 PM org.apache.catalina.core.ApplicationContext log
    INFO: Initializing Spring FrameworkServlet ‘dispatcher’
    Mar 27, 2015 2:37:49 PM org.apache.coyote.AbstractProtocol start
    INFO: Starting ProtocolHandler [“http-bio-8081”]
    Mar 27, 2015 2:37:49 PM org.apache.coyote.AbstractProtocol start
    INFO: Starting ProtocolHandler [“ajp-bio-8009”]
    Mar 27, 2015 2:37:49 PM org.apache.catalina.startup.Catalina start
    INFO: Server startup in 4886 ms