Spring Boot Mongodb Example

By | | Updated : 2021-11-01 | Viewed : 22 times

spring boot mongodb example

As mongo DB is being used a lot in many and different types of applications, we will see here the spring boot MongoDB example.

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 spring-boot-starter-data-mongodb. Here i have added manually in maven pom.xml.

Maven Dependencies For Mongo DB in Spring Boot
        <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.

application.properties
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 MongoRepository for curd operations. See the below-given properties.

Notice below the given code snippet for the same MongoRepository setup for curd operations.

ProductRepository for curd operations
@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

ProductsController.java
@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

ProductServiceImpl.java
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

ProductRepository.java

@Repository
public interface ProductRepository extends MongoRepository<Product, String> {

}

For the entire code repo, you can refer the Spring-Boot-Curd-With-MongoDB-Example-App

Leave A Reply