mmts1007’s diary

プログラミング関連の技術系ブログです。

Java Spring Boot

今回は Spring Boot の記事を書こうと思います。
開発メモという形で何回かに分けて上げていこうと思います。

まずは Spring Boot のセットアップから

開発環境

セットアップ

Gradle ファイルの作成、Java プロジェクトのディレクトリ構成作成

$ mkdir hello_boot
$ cd hello_boot
$ gradle init --type java-library

# 不要なファイル削除
$ rm src/main/java/Library.java
$ rm src/test/java/LibraryTest.java

Spring Boot 用の設定に修正

参考: http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#getting-started-gradle-installation

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'org.springframework.boot:spring-boot-gradle-plugin:1.2.6.RELEASE'
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'spring-boot'

jar {
    baseName = 'hello_boot'
    version =  '0.0.1-SNAPSHOT'
}

repositories {
    jcenter()
}

dependencies {
    compile 'org.springframework.boot:spring-boot-starter-web'
    testCompile 'org.springframework.boot:spring-boot-starter-test'
}

Eclipse 設定ファイルを作成

$ gradle eclipse

サンプルアプリケーションを作成

参考:http://projects.spring.io/spring-boot/

package hello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@EnableAutoConfiguration
public class SampleController {

    @RequestMapping("/")
    @ResponseBody
    String home() {
        return "Hello Spring Boot!";
    }

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

サンプルアプリケーションを実行

$ gradle run

http://localhost:8080/ にアクセスして、Hello Spring Boot! と表示されれば OK!