본문 바로가기

Study/Kotlin

Kotlin - SpringBoot + Kotlin 프로젝트 생성

1. 프로젝트 생성

1-1. IntelliJ > New Project > Spring Initializr

IntelliJ > New Project > Spring Initializr

 

1-2. Project Metadata

  • Group : com.springboot.kotlin
  • Artifact : springboot-kotlin
  • Type : Maven Project
  • Language : Kotlin
  • Package : com.kotlintest

Project Metadata

 

1-3. Dependency

  • Spring Web
  • Thymeleaf

Dependency

 

1-4. pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.2.4.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.springboot.kotlin</groupId>
	<artifactId>springboot-kotlin</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>springboot-kotlin</name>
	<description>Demo project for Spring Boot</description>

	<properties>
		<java.version>1.8</java.version>
		<kotlin.version>1.3.61</kotlin.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>com.fasterxml.jackson.module</groupId>
			<artifactId>jackson-module-kotlin</artifactId>
		</dependency>
		<dependency>
			<groupId>org.jetbrains.kotlin</groupId>
			<artifactId>kotlin-reflect</artifactId>
		</dependency>
		<dependency>
			<groupId>org.jetbrains.kotlin</groupId>
			<artifactId>kotlin-stdlib-jdk8</artifactId>
		</dependency>

		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
			<exclusions>
				<exclusion>
					<groupId>org.junit.vintage</groupId>
					<artifactId>junit-vintage-engine</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
	</dependencies>

	<build>
		<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
		<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
			<plugin>
				<groupId>org.jetbrains.kotlin</groupId>
				<artifactId>kotlin-maven-plugin</artifactId>
				<configuration>
					<args>
						<arg>-Xjsr305=strict</arg>
					</args>
					<compilerPlugins>
						<plugin>spring</plugin>
					</compilerPlugins>
				</configuration>
				<dependencies>
					<dependency>
						<groupId>org.jetbrains.kotlin</groupId>
						<artifactId>kotlin-maven-allopen</artifactId>
						<version>${kotlin.version}</version>
					</dependency>
				</dependencies>
			</plugin>
		</plugins>
	</build>

</project>

 

2. 소스코드 작성

2-1. Model

  • New Package > model
  • New Kotlin File/Class > User / Class
package com.kotlintest.model

import java.time.Instant

data class User (
    val username: String,
    val email: String,
    val registered: Instant
)

 

2-2. Service

  • New Package > service
  • New Kotlin File/Class > UserService / Interface
  • New Kotlin File/Class > UserServiceImpl / Class
package com.kotlintest.service

import com.kotlintest.model.User

interface UserService {

    fun getUser() : User

}



package com.kotlintest.service

import com.kotlintest.model.User
import org.springframework.stereotype.Service
import java.time.Instant

@Service
class UserServiceImpl : UserService {

    override fun getUser(): User {
        return User("SSING", "ssing@naver.com", Instant.now())
    }
}

 

2-3. Controller

  • New Package > controller
  • New Kotlin File/Class > IndexController / Class
package com.kotlintest.controller

import com.kotlintest.service.UserService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Controller
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestMethod
import org.springframework.web.bind.annotation.ResponseBody

@Controller
class IndexController {

    @Autowired
    lateinit var userService: UserService

    @GetMapping("/index")
    fun welcome():String {
        return "index"
    }

    @ResponseBody
    @RequestMapping( method = [ RequestMethod.GET ], value = "/find/user")
    fun findUser() = userService.getUser()

}

 

2-4. View

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    Hello Kotlin
</body>
</html>

 

2-5. 프로젝트 파일 구조

프로젝트 파일 구조

 

 

3. 테스트