TIL_009 | Kotlin + Spring Boot

묘한묘랑·2023년 12월 8일
0

TIL

목록 보기
9/31

Kotlin + Spring Boot를 시작하며 발생한 에러들에 대해서 적어보려 한다.

Spring Initializr를 통하여 일단 프로젝트를 생성하였다.

가장 처음 보고 놀란 것은

@SpringBootApplication
class KtspringApplication

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

위와 같은 형태였다.

SpringBootApplicatioin
class KtspringApplication

이 코드는 기본 Application Class를 Annotation을통해 구성을 해준다.

runApplication< KtspringApplication >(*args)

spring boot의 함수인 runApplication을 통하여 실행 시킨다.

일단 class에 중괄호가 없었기에 저런 식으로도 사용이 가능한 사실을 알게 되어 설치와 동시에 상당히 만족스러웠다.


그리고 간단한 Controller를 만들고 실행하자 바로 만난 에러.

Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class
Action:

Consider the following:
If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).

설치할 때 DB관련 dependency를 추가해놓고 그대로 실행하였다.
당장 바로 연결해서 테스트 해볼것이 아니기에 annotation을 따로 만들어 db관련 Auto Configuration을 진행하지 않기로 하였다.

@Retention(AnnotationRetention.RUNTIME)
@Target(AnnotationTarget.CLASS)
@SpringBootApplication(exclude = [DataSourceAutoConfiguration::class])
annotation class TempSpringBootApplication{}

이후 간단한 RestController를 정상 작동하는지 테스트 해본 후 디컴파일을 해보았다.

class SimpleController {

    @GetMapping
    fun getSomeData(): String?{
        return "test";
    }
}
->
public class SimpleController {
   @GetMapping
   @Nullable
   public String getSomeData() {
      return "test";
   }
}

kotlin의 NullSafety를 @Nullable과 @NotNull을 통하여 처리하고 있었다.

간단한 테스트를 끝내고 db연결을 해봐야겠다.

profile
상황에 맞는 기술을 떠올리고 사용할 수 있는 개발자가 되고 싶은 개발자

0개의 댓글