---
title: KotlinでSpring Boot入門 springboot
tags: ["Kotlin", "Spring", "Spring Boot"]
categories: ["Programming", "Java", "Kotlin"]
date: 2014-06-28T21:35:41Z
updated: 2014-06-28T21:35:41Z
---

「[Kotlinことはじめ](/#/entries/273)」と「[Spring BootとSpring LoadedでサクサクHot Reloading Java Webアプリ開発 #springboot](/#/entries/265)」の組み合わせネタです。普通に出来ました。


pom.xml

    <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 http://maven.apache.org/maven-v4_0_0.xsd">
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.example</groupId>
        <artifactId>hajiboot-kotlin</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <packaging>jar</packaging>
    
        <name>hajiboot-kotlin</name>
    
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.1.3.RELEASE</version>
        </parent>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.jetbrains.kotlin</groupId>
                <artifactId>kotlin-stdlib</artifactId>
                <version>${kotlin.version}</version>
            </dependency>
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <dependencies>
                        <dependency>
                            <groupId>org.springframework</groupId>
                            <artifactId>springloaded</artifactId>
                            <version>1.2.0.RELEASE</version>
                        </dependency>
                    </dependencies>
                </plugin>
                <plugin>
                    <artifactId>kotlin-maven-plugin</artifactId>
                    <groupId>org.jetbrains.kotlin</groupId>
                    <version>${kotlin.version}</version>
                    <configuration/>
                    <executions>
                        <execution>
                            <id>compile</id>
                            <phase>process-sources</phase>
                            <goals>
                                <goal>compile</goal>
                            </goals>
                        </execution>
                        <execution>
                            <id>test-compile</id>
                            <phase>process-test-sources</phase>
                            <goals>
                                <goal>test-compile</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    
        <properties>
            <kotlin.version>0.7.270</kotlin.version>
            <java.version>1.8</java.version>
            <start-class>com.example.ExamplePackage</start-class>
        </properties>
    </project>

ほぼ前述の2記事の組み合わせです。注意ポイントは`<properties>`に`<start-class>`でエントリポイントのFQCNを明示的に設定すること。Kotlinでコンパイルすると`main`メソッドのあるクラスが2つできるっぽいので。`com.example`パッケージに`main`関数がある場合は、`com.example.ExamplePackage`を設定する。


`src/main/java/com/example/App.kt`を作成。

    package com.example
    
    import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.boot.SpringApplication
    
    EnableAutoConfiguration
    RestController
    class App {
        RequestMapping
        fun hello(): String {
            return "Hello World!"
        }
    }
    
    fun main(args: Array<String>) {
        SpringApplication.run(array(javaClass<App>()), args);
    }

あとは`mvn spring-boot:run`を実行。http://localhost:8080 にアクセスすれば

![image](https://qiita-image-store.s3.amazonaws.com/0/1852/a4bde45b-85da-be3f-dc68-18e1b99604eb.png)

普通にいけますね。spring-loadedのHot Reloadも効きます。

KotlinにWebフレームワークの決定版ないみたいだし、これでいいんじゃないの？
