简体   繁体   中英

Spring boot mongodb application configuration

I'm creating spring boot micro service application. One micro service use spring boot and MySQL and hibernate. Another micro service use spring boot and mongodb. Below you can see Spring boot main application of micro-service which is use spring boot and MySQL and hibernate.

@EnableFeignClients(basePackages = {"com.saman.kamak.nimal.sunilservice"})
@EnableDiscoveryClient
@SpringBootApplication(scanBasePackages = {"com.saman.kamak.nimal.sunilservice"})
@EnableOAuth2Client
@EnableJpaRepositories(basePackages = {"com.saman.kamak.nimal.sunilservice.repository"})
@EntityScan(basePackages = {"com.saman.kamak.nimal.sunilservice.domain"})
public class ColdApplication {
    public static void main(String[] args) {
        SpringApplication.run(ColdApplication.class, args);
    }
}

Now I'm creating a micro-service with spring boot & mongodb. How does main class of it appear like? How should it change this one @EntityScan(basePackages = {"com.saman.kamak.nimal.sunilservice.domain"})

Is it @documentScan(basePackages = {"com.saman.kamak.nimal.sunilservice.domain"})

Below you can see my mongodb model class

import java.io.Serializable;

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@ToString
@Document
public class Cold implements Serializable {

   private static final Long serialVersionUID = 1L;

      @Id
      private String box;
      private String pencil;
      private String pen;
      private String bag;
      private String phone;
}

For Configuring MongoDB with your spring boot app follow the below steps:

  • Add MongoDB starter dependency (for maven add below in your pom.xml ):

     <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency>
  • Add Below configurations in your property file application.properties:

MongoDB Credentials

spring.data.mongodb.authentication-database=admin
spring.data.mongodb.username=your_db_user 
spring.data.mongodb.password=your_db_password
spring.data.mongodb.database=your_db_name
spring.data.mongodb.port=27017 
spring.data.mongodb.host=localhost

Your main class would be something like below:

@EnableFeignClients(basePackages = {"com.saman.kamak.nimal.sunilservice"})
@EnableDiscoveryClient
@SpringBootApplication(scanBasePackages = {"com.saman.kamak.nimal.sunilservice"})
@EnableOAuth2Client
@EnableJpaRepositories(basePackages ={"com.saman.kamak.nimal.sunilservice.repository"})
public class DemoRestApiApplication {

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM