Spring Boot with Mongo DB example | Java @ Desk
Home » , » Spring Boot with Mongo DB example

Spring Boot with Mongo DB example

Spring Boot with Mongo DB example

This example demonstrates the integration of Mongo DB with Spring Boot. If you are running Mongo DB locally, then there is no need to configure anything for the Mongo database in the spring boot application.

Spring boot auto configures the mongo db configuration since by default mongo runs on
1) host - localhost or 127.0.0.1
2) port - 27017

If you are running a mongo db on local system, then you just have to configure the mongo repository interface in your spring boot application and use @EnableMongoRepositories annotation. Once you run the spring boot application you will notice a following log in your mongo db command prompt
connection accepted from 127.0.0.1:52994 #1 (1 connection now open)

This indicates that the spring boot application is connected to mongo db and you can perform db operations.

And once you stop the application you will observe
end connection 127.0.0.1:52994 (1 connection now open)


In any case there is a need for configuring the change in host or port, then perform following changes in application.properties file
server.port=8070
spring.dao.exceptiontranslation.enabled=false
spring.data.mongodb.host=127.0.0.1
spring.data.mongodb.port=27017


Here is the maven dependency for spring data for mongo db
<dependency>
 <groupId>org.springframework.data</groupId>
 <artifactId>spring-data-mongodb</artifactId>
 <version>1.10.1.RELEASE</version>
</dependency>


Spring Boot app java class - This class configures the spring controller file plus enables the mongo repository using @EnableMongoRepositories annotation.
package com.springbootservice;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;

import com.repository.AppRepository;

@SpringBootApplication(scanBasePackages = { "com.controller"})
@EnableMongoRepositories(basePackageClasses = AppRepository.class)
public class App {

 public static void main(String[] args) {
  SpringApplication.run(App.class, args);
 }
}




Repository Interface - Mongo comes with the MongoRepository interface that internally performs all the basic db operations like insert, get & delete on its own. You must have to create a repository interface that extends MongoRepository interface along with the type. If you have mongo db data object Person then MongoRepository enables all basic operations on Person object.
package com.repository;

import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;

import com.model.Person;

@Repository()
public interface AppRepository extends MongoRepository<Person, String> {

}


Mongo Model class
package com.model;

import org.springframework.data.annotation.Id;

public class Person {
 @Id
 private String id;

 private String name;

 public String getId() {
  return id;
 }

 public void setId(String id) {
  this.id = id;
 }

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }

}


Test Controller - Simply autowire the AppRepository object and start performing DB operations as shown below
@Autowired
private AppRepository appRepository;

@RequestMapping(method = RequestMethod.GET, path = "/test/Mongo", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<String> getTest() {
 String temp = "abc";
 Person person = new Person();
 person.setId("Id");
 person.setName("Kumar");
 appRepository.insert(person);

 Person person2 = appRepository.findOne("Id");
 System.out.println("Person - " + person2);
 System.out.println(person2.getName());
 appRepository.delete("Id");
 System.out.println("Deleted Successfully");
 return new ResponseEntity<String>(temp, HttpStatus.OK);

}





0 comments:

Post a Comment