はじめに
タイトルの通りなのですが、IntelliJ IDEA Community Edition で Spring Boot プロジェクトを作成して起動する方法がネットを検索かけても見つからなかったので調査した結果を掲載します。
IntelliJ IDEAとは
IntelliJ IDEA は、Java プロジェクトを開発するための高機能な統合開発環境です。
Community Edition は無償で使用できる Edition です。機能制限があるものの、商用利用も可能とのことです。
Community Edition is open-source, licensed under Apache 2.0. Projects like Android and Swift use Apache 2.0, so you're in good company. It can also be used for commercial development.
この記事では、IntelliJ IDEA Community Edition を使用します。
IntelliJ IDEA Community Edition のダウンロードとインストール
下記サイトにアクセスし、「Community」をダウンロードしてインストールします。
インストール方法は、デフォルトインストールなので特に悩むことはないと思います。
Spring Boot プロジェクトの作成
IntelliJ IDEA Community Edition では Spring Boot のプロジェクトを作成できないので、以下の Spring Initializr というページで作成します。
Spring Initializr は、ウィザード形式で Spring Boot プロジェクトを作成できるツールです。
この Spring Initializr を使用して、Spring Boot のプロジェクトを作成します。
最初は、デフォルトのままの指定を使用します。赤枠で囲んだところが指定内容になります。
Dependencies の Web で Spring Web Starter を選択します。
Template Engines には Thymeleaf を指定して、[Generate the project]をクリックすると、demo.zip がダウンロードされます。
demo.zip を解凍して作成された demo フォルダをプロジェクトとして使用します。
IntelliJ IDEA Community Edition で Spring Boot プロジェクトを開く
IntelliJ IDEA Community Edition を起動して、先ほど作成した demo フォルダを開きます。
API を作成する
com.demo.controller
パッケージを作成して、その配下に WebApiController
クラスを以下のように作成します。
WebApiController
クラスの内容は、以下のように記述します。
package com.example.demo.controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("api") public class WebApiController { @RequestMapping("hello") private String hello() { return "Spring Boot!!"; } }
これで API を呼び出すことができるようになります。
Spring Boot プロジェクトの起動
以下のように、デフォルトでDemoApplication
クラスが作成されます。
DemoApplication
クラスは以下のようになっており、Spring Boot を起動するクラスになります。
package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
このDemoApplication
クラスを右クリックして、[Run 'DemoApplication.main()']を選択すると Spring Boot を起動できます。
この情報が見つからず苦労しました。
Spring Boot が起動しましたね。
この状態でhttp://localhost:8080/api/hello
にブラウザでアクセスすると、以下のように API の結果が返ってきます。
おわりに
IntelliJ IDEA Community EditionでSpring Bootプロジェクトを扱う方法をご紹介しましたが、いかがでしたでしょうか。
IntelliJ IDEA は企業内で使用される IDE であるためネットには情報が少ないですが、使えると非常に便利と思うので、是非活用していきたいですね。