0
1

More than 3 years have passed since last update.

SpringBootことはじめ 0.SpringCLIでHello World

Last updated at Posted at 2020-11-15

はじめに

職場でSpring(Framework)を使うことになったので
前々から興味のあったSpringBootを触ってみることにした。
ちなみに SpringBoot≠SpringFramework だけどプライベートではあえてSpringBootを選択。

今回は、SpringBootの前にSpringCLIでお試し。

環境

ソフトウェア バージョン
OS Windows10 Pro
Java OpneJDK 12.0.2
Spring CLI v2.3.5.RELEASE

実施

1. SpringCLIを落とす。

公式入門ドキュメント(日本語)に記載してあるspring-boot-cli-2.3.5.RELEASE-bin.zipを落として解凍。
解凍後のフォルダ配下のspring-2.3.5.RELEASE\binフォルダをパスに通す。

これでspringコマンドが使えるようになる。
試しに以下のコマンドを実行してバージョンが返ってきたらインストール成功。

バージョン確認コマンド
spring version
実行結果
C:\>spring version
Spring CLI v2.3.5.RELEASE

2. REST API用コード実装

PC上のどこでもいいのでREST API用のコードを書く。
公式入門ドキュメントではGroovyで書いているが、Javaでもいい。

【Java】app.java
@RestController
public class Test {

    @RequestMapping("/")
    public String home() {
        return "Hello World!";
    }

    @RequestMapping("/sb")
    public String helloSb() {
        return "Hello SpringBoot!";
    }

}
【Groovy】app.groovy
@RestController
class ThisWillActuallyRun {

    @RequestMapping("/")
    String home() {
        "Hello World!"
    }

    @RequestMapping("/sb")
    String helloSb() {
        "Hello SpringBoot!"
    }

}

ちなみに上記程度ならimport文は不要。(IDEでやるとコンパイルエラー表示出るけど)

3. 実行

REST API用ソースコードがある場所で以下のコマンドを実行。

spring run app.java

すると以下のメッセージが表示されてREST APIアプリケーションが起動する。
必要なライブラリは自動的にDLされている模様。


  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.3.5.RELEASE)

2020-11-15 12:31:05.416  INFO 9532 --- [       runner-0] o.s.boot.SpringApplication               : Starting application on XXXXXXXXXX(マシン名) with PID 9532 (started by xxxx in M:\develop\works\Spring\20201115_springboot_start)
2020-11-15 12:31:05.421  INFO 9532 --- [       runner-0] o.s.boot.SpringApplication               : No active profile set, falling back to default profiles: default
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.codehaus.groovy.reflection.CachedClass (jar:file:/M:/develop/tools/Spring/spring-boot-cli-2.3.5.RELEASE-bin/spring-2.3.5.RELEASE/lib/spring-boot-cli-2.3.5.RELEASE.jar!/BOOT-INF/lib/groovy-2.5.13.jar!/) to method java.lang.Object.finalize()
WARNING: Please consider reporting this to the maintainers of org.codehaus.groovy.reflection.CachedClass
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
2020-11-15 12:31:06.384  INFO 9532 --- [       runner-0] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-11-15 12:31:06.394  INFO 9532 --- [       runner-0] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-11-15 12:31:06.394  INFO 9532 --- [       runner-0] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.39]
2020-11-15 12:31:06.426  INFO 9532 --- [       runner-0] org.apache.catalina.loader.WebappLoader  : Unknown class loader [org.springframework.boot.cli.compiler.ExtendedGroovyClassLoader$DefaultScopeParentClassLoader@6adca536] of class [class org.springframework.boot.cli.compiler.ExtendedGroovyClassLoader$DefaultScopeParentClassLoader]
2020-11-15 12:31:06.458  INFO 9532 --- [       runner-0] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-11-15 12:31:06.458  INFO 9532 --- [       runner-0] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 889 ms
2020-11-15 12:31:06.601  INFO 9532 --- [       runner-0] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-11-15 12:31:06.879  INFO 9532 --- [       runner-0] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2020-11-15 12:31:06.887  INFO 9532 --- [       runner-0] o.s.boot.SpringApplication               : Started application in 1.795 seconds (JVM running for 3.034)

4. 稼働確認

以下にアクセスしてみる。

http://localhost:8080/
image.png

http://localhost:8080/sb
image.png

おぉ~いいっスね~:grinning:

まとめ

SpringCLIだけで速攻でREST APIができてしまった。(文字列返すだけだけど)
この時点ならIDEすら不要。
めちゃくちゃ簡単なAPIモック作るならこれでもいいかもしれない。

次回はSpringBootのちゃんとした?アプリケーションを作りたい。

参考

公式入門ドキュメント(日本語)

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