LoginSignup
5
2

More than 3 years have passed since last update.

悲報 SpringBootは二度はまる。。

Last updated at Posted at 2020-11-19

Spring Bootで久しぶりにREST APIを書いてみようとして「はまった」話。おそらくまたはまるので備忘

はまりポイントの経緯

Spring Bootをどれぐらい使ったことあるの?

キックオフミーティング用の簡単APIを作成するに1度使って。「お手軽」のイメージをもっていました。
今回OAuthの認証サンプル作ろうと、受け側のダミーサイトとして利用するつもりだったのですが。あっさりとはまりました。

なにをやらかしたの?

Servlet Container が期待通り起動せず。エラーも出ないので途方に暮れた。

まずはプロジェクトの作成

SpringBoot Initializerを使ってサンプルプロジェクトの作成しました。

実行してみましょう。

> gradle bootRun

と。。さて、Tomcat 起動してくれるよね。。。

> Task :bootRun

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

2020-11-19 19:06:10.101  INFO 27916 --- [           main] com.example.demo.DemoApplication         : Starting DemoApplication using Java 1.8.0_241 on DESKTOP-O85C3T1 with PID 27916 (E:\dev\workspace\springBoot\demo\build\classes\java\main started by shupe in E:\dev\workspace\springBoot\demo)
2020-11-19 19:06:10.103  INFO 27916 --- [           main] com.example.demo.DemoApplication         : No active profile set, falling back to default profiles: default
2020-11-19 19:06:10.375  INFO 27916 --- [           main] com.example.demo.DemoApplication         : Started DemoApplication in 0.49 seconds (JVM running for 0.75)

BUILD SUCCESSFUL in 1s
4 actionable tasks: 4 executed

あれ?プロセス終わってexit 0だ。。。

もしかしてバックグラウンドで走っているのかな?**と考え確認してみたもののそんなはずはなく。

>jps
3904 Jps
23660 GradleDaemon

途方に暮れていたのですが、答えは簡単なものでした。

なにが問題だったの?

答えを探し求めること数時間。答えは意外なところに。というか当たり前のところに。。。「以前書いた自分のコード」・・・・・ではなく、プロジェクト定義ファイル。(当時はpom.xml)

使っているStarterが異なりました。

build.gradle
変更前 implementation 'org.springframework.boot:spring-boot-starter'
変更後 implementation 'org.springframework.boot:spring-boot-starter-web'

普通に、Web用のStarterをしていないので期待通りにServerプロセスが立ち上がってこなかったわけです。
Initializerのデフォルトをそのまま信用していたのが失敗の原因でした。

さて、再実行。。。

> Task :bootRun

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

2020-11-19 19:17:14.164  INFO 1092 --- [           main] com.example.demo.DemoApplication         : Starting DemoApplication using Java 1.8.0_241 on DESKTOP-O85C3T1 with PID 1092 (E:\dev\workspace\springBoot\demo\build\classes\java\main started by shupe in E:\dev\workspace\springBoot\demo)
2020-11-19 19:17:14.166  INFO 1092 --- [           main] com.example.demo.DemoApplication         : No active profile set, falling back to default profiles: default
2020-11-19 19:17:14.668  INFO 1092 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2020-11-19 19:17:14.673  INFO 1092 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-11-19 19:17:14.673  INFO 1092 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.39]
2020-11-19 19:17:14.709  INFO 1092 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-11-19 19:17:14.709  INFO 1092 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 519 ms
2020-11-19 19:17:14.796  INFO 1092 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-11-19 19:17:14.880  INFO 1092 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2020-11-19 19:17:14.886  INFO 1092 --- [           main] com.example.demo.DemoApplication         : Started DemoApplication in 0.914 seconds (JVM running for 1.136)
<==========---> 80% EXECUTING [19s]
2020-11-19 19:17:46.236  INFO 1092 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2020-11-19 19:17:46.236  INFO 1092 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2020-11-19 19:17:46.237  INFO 1092 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 1 ms
<==========---> 80% EXECUTING [19m 18s]                                                                                 > IDLE

無事起動しました。「bootRunしたけど、Tomcatが起動せずにとまってしまう」問題に直面したら思い出してください。デフォルトのツールではweb starterになってないよぉ~と

5
2
2

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
5
2