简体   繁体   中英

MongoDB configuration with Spring Boot?

I am using ReactiveMongo in the Spring Boot configured project.

service

@Document(collection = "tbl_created_service")
public class Services {
 @Id
 private ObjectId objectId;
 private Long id;
  // Getters and setters
}

ServiceReactiveRepository

public interface ServiceReactiveRepository extends ReactiveMongoRepository<Services, String> 
{
}

ServiceService

public interface ServicesService {
   Long addNewService(Services services);
}

ServiceImpl

@Service
public class ServicesServiceImpl implements ServicesService {

  @Autowired
  ServiceReactiveRepository serviceReactiveRepository;

  @Override
  public Long addNewService(Services servicesTypeDto)  {
     serviceReactiveRepository.save(servicesTypeDto);
     return null;
  }

application.yml

  spring:
  profiles:
    active: dev
---
spring:
  profiles: dev
  data.mongodb:
    host: 127.0.0.1
    port: 27017
    database: dev

---

MainApllication class

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

When I am hitting the endpoint, I can see the data is binding and coming to ServicesServiceImpl.addNewService() but somehow it's not creating a new collection in DB (If not exists) ie tbl_created_service

However, If I add the collection manually and hit the service, Then it's creating a new document in "tbl_created_service" .

Is the right way to do it? or I am missing something?

Any hint would be appreciable:)

just put this at your application.properties spring.data.mongodb.uri=URIconnection

this will work when you will use an MongoDb atlas. Just create cluster and connect get the Java URI link then put it in your property. When you run your application it will auto generate the collections declared at your Entity class.

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