Spring Boot is a popular Java-based framework used for building microservices. It offers a simple way to create stand-alone, production-grade Spring based Applications that you can "just run".
MongoDB is an open source NoSQL database that uses a document-oriented data model. It is a powerful tool for data management and analysis.
In this article, we will show you how to connect Spring Boot with MongoDB.
To follow along with this article, you will need the following:
We will use Spring Initializr to generate our Spring Boot project.
Go to http://start.spring.io/, and select the following:
Click Generate Project.
Spring Boot will look for a file called application.properties
(or application.yml
) in the src/main/resources
directory. This is where we will configure our MongoDB connection.
Add the following to your application.properties
file:
spring.data.mongodb.uri=mongodb://localhost/test
This will connect to a MongoDB database called test
on localhost
.
If you are using application.yml
, the configuration will look like this:
spring:
data:
mongodb:
uri: mongodb://localhost/test
Next, we will create a repository interface for our MongoDB database. This will extend the MongoRepository
interface provided by Spring Data MongoDB.
package com.example.demo.repository;
import org.springframework.data.mongodb.repository.MongoRepository;
import com.example.demo.model.User;
public interface UserRepository extends MongoRepository<User, String> {
}
The UserRepository
interface will perform CRUD operations on the User
collection in MongoDB.
Now we will create a User
class to represent documents in the User
collection.
package com.example.demo.model;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;
@Document
public class User {
@Id
private String id;
private String name;
private int age;
// Getters and setters
}
The @Document
annotation tells Spring Data MongoDB that the User
class is a document in the User
collection.
The @Id
annotation marks the id
field as the primary key for the document.
Now we will create a REST controller to expose our MongoDB repository as a REST API.
package com.example.demo.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.model.User;
import com.example.demo.repository.UserRepository;
@RestController
@RequestMapping("/api/v1/users")
public class UserController {
@Autowired
private UserRepository userRepository;
@RequestMapping(method = RequestMethod.GET)
public List<User> getUsers() {
return userRepository.findAll();
}
}
The @RestController
annotation marks the UserController
class as a REST controller.
The @RequestMapping
annotation maps HTTP requests to methods in the controller. In this case, the /api/v1/users
path is mapped to the getUsers()
method.
The @Autowired
annotation injects the UserRepository
into the controller.
The getUsers()
method retrieves a list of users from the MongoDB database and returns it as JSON.
To run the application, simply type the following command in your terminal:
./mvnw spring-boot:run
You should see the following output:
...
2017-12-01 10:21:59.593 INFO 85511 --- [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2017-12-01 10:21:59.596 INFO 85511 --- [ main] com.example.demo.DemoApplication : Started DemoApplication in 4.166 seconds (JVM running for 4.681)
You can now access the REST API at http://localhost:8080/api/v1/users.
In this article, we have shown you how to connect Spring Boot with MongoDB. We have also created a simple REST API for CRUD operations on our MongoDB database.
For more information on Spring Boot, please see the following resources:
For more information on MongoDB, please see the following resources: