0
1

More than 3 years have passed since last update.

IntelliJでJava SpringBootのプロジェクトを作成

Last updated at Posted at 2020-07-04

はじめに

IntelliJ IDEAでSpringBootのプロジェクトを作成する手順を整理します。
今回はJava、Gradleを利用します。

環境

MacOS Catalina (10.15.7)
Java11(openjdk 11.0.12 2021-07-20 LTS)
IntelliJ IDEA 2021.2(Ultimate Edition)

手順

プロジェクト作成

IntelliJを起動してCreate New Projectをクリックします。

スクリーンショット 2021-09-05 0.27.49.png

Gradle、Javaを選択してNextをクリックします。

スクリーンショット 2021-09-05 0.28.37.png

SprintBootTestとしてプロジェクトを作成します。

スクリーンショット 2021-09-05 0.29.36.png

build.gradleの編集

プロジェクトで作成されるbuild.gradle

build.gradle
plugins {
    id 'java'
}

group 'com.ykdevs'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
}

test {
    useJUnitPlatform()
}

以下の様に編集。
バージョンはSpringBootから最新版を指定した。
pluginsで(org.springframework.boot](https://plugins.gradle.org/plugin/org.springframework.boot)と[io.spring.dependency-management](https://plugins.gradle.org/plugin/io.spring.dependency-management)を指定。
参考

IntelliJを利用するので、pluginsにideaを指定した。参考
JUnitはJUnit5を利用する。

簡単なWebアプリケーションを利用するのでspring-boot-starter-webを指定する。

build.gradle
plugins {
    id 'org.springframework.boot' version '2.5.+'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
    id 'idea'
}

group 'com.ykdevs'
version '1.0-SNAPSHOT'

repositories {
    mavenCentral()
}

dependencies {
    // SpringBoot
    implementation "org.springframework.boot:spring-boot-starter-web"
    testImplementation "org.springframework.boot:spring-boot-starter-test"

    // JUnit
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0'
    testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0'
}

test {
    useJUnitPlatform()
}

アプリケーション実行クラスの作成

プロジェクト作成時に以下のようなディレクトリができている。

src
├─ main
│  │
│  ├─ java
│  │
│  └─ resources
└─ test
   │
   ├─ java
   │
   └─ resources

main/java配下でパッケージの作成を行う。

スクリーンショット 2021-09-05 0.39.25.png

カンマ区切りでパッケージ名とアプリケーションクラスを指定すれば、ディレクトリとクラスファイルが同時に作成される。

スクリーンショット 2021-09-05 0.39.52.png

@SpringBootApplicationのアノテーションをつけ、main関数を作成する。

Application.java
package com.ykdevs.SpringBootTest;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

Webアプリケーションクラスを作成する

スクリーンショット 2021-09-05 0.47.21.png

IndexController.java
package com.ykdevs.SpringBootTest.app;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class IndexController {

    @GetMapping("/")
    public String index() {
        return "Greetings from Spring Boot!";
    }
}

RunでWebアプリケーションを起動する。

スクリーンショット 2021-09-05 0.52.06.png

参考

Gradle User Guide
Spring Boot入門−公式ドキュメント
JUnit 5 ユーザーガイド

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1