SpringBoot 프로젝트에 MongoDB Atlas 연동 및 MongoRepository 사용하여 간단한 조회 API 수행해보기

반응형
728x90
반응형

MongoDB Atlas 설정

https://devfunny.tistory.com/850

 

[MongoDB] MongoDB Altas Free버전 Compass 사용하기

1) monggoDB 사이트 접속 https://account.mongodb.com/account/login?signedOut=true Cloud: MongoDB Cloud account.mongodb.com 2) 회원가입 후 로그인시, 접속되는 화면 3) create a database ■ [Build a Database] ■ [Create] ■ [Create Cluste

devfunny.tistory.com

 

 

MongoDB Atlas 연동

1) [Connect] 클릭

 

2) [Drivers] 선택

 

3) [Driver], [Version] 선택

 

주의할점

제공받은 code
mongodb+srv://studyollemongo:<password>@cluster0.es9fp8f.mongodb.net/?retryWrites=true&w=majority

 

database명을 반드시 추가하여 아래 application.yml에 작성해야한다.
mongodb+srv://studyollemongo:<password>@cluster0.es9fp8f.mongodb.net/<database>?retryWrites=true&w=majority

 

database명 작성하지 않았을 경우 오류 발생
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoDatabaseFactory' defined in class path resource [org/springframework/boot/autoconfigure/data/mongo/MongoDatabaseFactoryConfiguration.class]: Failed to instantiate [org.springframework.data.mongodb.core.MongoDatabaseFactorySupport]: Factory method 'mongoDatabaseFactory' threw exception with message: Database name must not be empty
	at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:659) ~[spring-beans-6.0.11.jar:6.0.11]
	at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:647) ~[spring-beans-6.0.11.jar:6.0.11]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1332) ~[spring-beans-6.0.11.jar:6.0.11]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1162) ~[spring-beans-6.0.11.jar:6.0.11]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:560) ~[spring-beans-6.0.11.jar:6.0.11]
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:520) ~[spring-beans-6.0.11.jar:6.0.11]
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:326) ~[spring-beans-6.0.11.jar:6.0.11]
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-6.0.11.jar:6.0.11]
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:324) ~[spring-beans-6.0.11.jar:6.0.11]
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:200) ~[spring-beans-6.0.11.jar:6.0.11]
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:973) ~[spring-beans-6.0.11.jar:6.0.11]
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:942) ~[spring-context-6.0.11.jar:6.0.11]
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:608) ~[spring-context-6.0.11.jar:6.0.11]
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:146) ~[spring-boot-3.1.2.jar:3.1.2]
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:734) ~[spring-boot-3.1.2.jar:3.1.2]
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:436) ~[spring-boot-3.1.2.jar:3.1.2]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:312) ~[spring-boot-3.1.2.jar:3.1.2]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1306) ~[spring-boot-3.1.2.jar:3.1.2]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1295) ~[spring-boot-3.1.2.jar:3.1.2]
	at com.studyolle.StudyolleServerApplicationKt.main(StudyolleServerApplication.kt:17) ~[main/:na]
	at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104) ~[na:na]
	at java.base/java.lang.reflect.Method.invoke(Method.java:578) ~[na:na]
	at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:50) ~[spring-boot-devtools-3.1.2.jar:3.1.2]
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.mongodb.core.MongoDatabaseFactorySupport]: Factory method 'mongoDatabaseFactory' threw exception with message: Database name must not be empty
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:171) ~[spring-beans-6.0.11.jar:6.0.11]
	at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:655) ~[spring-beans-6.0.11.jar:6.0.11]
	... 22 common frames omitted
Caused by: java.lang.IllegalArgumentException: Database name must not be empty
	at org.springframework.util.Assert.hasText(Assert.java:294) ~[spring-core-6.0.11.jar:6.0.11]
	at org.springframework.data.mongodb.core.MongoDatabaseFactorySupport.<init>(MongoDatabaseFactorySupport.java:68) ~[spring-data-mongodb-4.1.2.jar:4.1.2]
	at org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory.<init>(SimpleMongoClientDatabaseFactory.java:75) ~[spring-data-mongodb-4.1.2.jar:4.1.2]
	at org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory.<init>(SimpleMongoClientDatabaseFactory.java:64) ~[spring-data-mongodb-4.1.2.jar:4.1.2]
	at org.springframework.boot.autoconfigure.data.mongo.MongoDatabaseFactoryConfiguration.mongoDatabaseFactory(MongoDatabaseFactoryConfiguration.java:52) ~[spring-boot-autoconfigure-3.1.2.jar:3.1.2]
	at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104) ~[na:na]
	at java.base/java.lang.reflect.Method.invoke(Method.java:578) ~[na:na]
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:139) ~[spring-beans-6.0.11.jar:6.0.11]
	... 23 common frames omitted

 

 

 

Project 설정

build.gradle.kts
implementation("org.springframework.boot:spring-boot-starter-data-mongodb")

 

 

application.yml 
spring:
  ...
  data:
    mongodb:
      uri: mongodb+srv://studyollemongo:<password>@cluster0.es9fp8f.mongodb.net/studyolle?retryWrites=true&w=majority

 

 

StudyolleServerApplication.kt
@SpringBootApplication
@EnableJpaRepositories
@EnableMongoRepositories
class StudyolleServerApplication

fun main(args: Array<String>) {
    runApplication<StudyolleServerApplication>(*args)
}

 

 

 

Sample 데이터 넣어보기

▶ 이전 포스팅 참고

https://devfunny.tistory.com/851

 

[MongoDB] database 생성 및 테스트 데이터 CRUD(INSERT, SELECT, UPDATE, DELETE) 실행해보기

들어가기전 MongoDB Altas Free 버전을 셋팅하자. https://devfunny.tistory.com/850 [MongoDB] MongoDB Altas Free버전 Compass 사용하기 1) monggoDB 사이트 접속 https://account.mongodb.com/account/login?signedOut=true Cloud: MongoDB Cloud a

devfunny.tistory.com

 

1) studyolledb 데이터베이스의 account_login 컬렉션에 데이터 등록

 

 

SpringBoot 간단한 코드 생성

AccountLogin.kt
@Getter
@Setter
@Document(collection = "account_login")
class AccountLogin(
    @Id
    val _id: String,
    val nickname: String? = null
) {

}

 

AccountLoginRepository.kt
interface AccountLoginRepository : MongoRepository<AccountLogin, String> {
}

 

AccountLoginReaderImpl.kt
@Component
class AccountLoginReaderImpl(
    private val accountLoginRepository: AccountLoginRepository
): AccountLoginReader {
    override fun findAll(): Any {
        return accountLoginRepository.findAll()
    }

}

 

조회 결과 확인

 

 

반응형

Designed by JB FACTORY