Spring Boot Mongodb Example
By AmarSivas | | Updated : 2021-11-01 | Viewed : 158 times

As mongo DB is being used a lot in many and different types of applications, we will see here the spring boot MongoDB example.
Table of Contents:
Maven Dependencies For Mongo DB
We can set up mongo DB maven dependencies in different ways. one is while creating spring boot application through spring initializer you can add artifact
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
And we need to configure the mongo DB properties in application properties as given below.
spring.data.mongodb.authentication-database=admin
spring.data.mongodb.username=root
spring.data.mongodb.password=root
spring.data.mongodb.database=spring_boot_db
spring.data.mongodb.port=27017
spring.data.mongodb.host=localhost
MongoDB Configuration
We can configure the mongo DB with spring boot in different ways. Here We are adding the required properties and using
Notice below the given code snippet for the same
@Repository
public interface ProductRepository extends MongoRepository<Product, String> {
}
Spring Boot MongoDB Example
Now we will write the Spring Boot MongoDB Example.
Code For Resource Controller
@RestController
@RequestMapping("/curd-app/api")
public class ProductsController {
@Autowired
ProductServiceImpl productServiceImpl;
@GetMapping("/testWeb")
public String testWeb() {
return "Result: Success";
}
@GetMapping("/products")
public ResponseEntity<?> getAllProducts() {
return productServiceImpl.getAllProducts();
}
@PostMapping(value = "/products")
public ResponseEntity<?> saveProduct(@RequestBody Product product) {
return productServiceImpl.saveProduct(product);
}
@PutMapping(value = "/products")
public ResponseEntity<?> updateProduct(@RequestBody Product product) {
return productServiceImpl.updateProduct(product);
}
@DeleteMapping(value = "/products")
public ResponseEntity<?> deleteProduct(@RequestBody Product product) {
return productServiceImpl.deleteProduct(product);
}
@PatchMapping(value = "/products")
public ResponseEntity<?>patchProduct(@RequestBody Product product) {
return productServiceImpl.patchProduct(product);
}
}
Code for ProductServiceImpl
public class ProductServiceImpl {
@Autowired
ProductRepository productRepository;
public ResponseEntity<?> getAllProducts() {
return ResponseEntity.ok(productRepository.findAll());
}
public ResponseEntity<?> saveProduct(Product product) {
Product resultedProduct = productRepository.save(product);
return ResponseEntity.ok(product);
}
public ResponseEntity<?> updateProduct(Product product) {
Product resultedProduct = productRepository.save(product);
return ResponseEntity.ok(resultedProduct);
}
public ResponseEntity<?> deleteProduct(Product product) {
productRepository.delete(product);
Map map = new HashMap();
map.put("Deleted", Boolean.TRUE);
return ResponseEntity.ok(map);
}
public ResponseEntity<?> patchProduct(Product product) {
Product resultedProduct = productRepository.save(product);
return ResponseEntity.ok(resultedProduct);
}
}
Code for Mongo repository
@Repository
public interface ProductRepository extends MongoRepository<Product, String> {
}
For the entire code repo, you can refer the Spring-Boot-Curd-With-MongoDB-Example-App