0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Spring入門ガイド実施 第1回 Restful WEBサービスの構築

Posted at

公式サイトにあったドキュメントで学習するよ。

このガイドでは、Spring を使用して「Hello, World」RESTful Web サービスを作成するプロセスについて説明します。

http://localhost:8080/greetingでアクセスすると{"id":1,"content":"Hello, World!"}が返ってきて、
http://localhost:8080/greeting?name=Userでアクセスすると{"id":1,"content":"Hello, User!"}が返ってくるWEBサービスを作成する。

必要なもの

・約15分
 →準備している前提。
・任意のテキスト エディターまたは IDE
 →eclipse Version: 2022-12 (4.26.0)を使用します。
・Java 17以降
 →17を使います。
・Gradle 7.5 以降または Maven 3.5 以降
 →Gradle 8.7 を使用します。

Gradleのインストールの際に環境変数で躓いたので備忘↓

soringbootプロジェクト作成

eclipseの新規プロジェクト作成でSpringスタータープロジェクトを選択。
追記:名前とかパッケージとかは任意で良いけど手順で想定しているのは以下。任意でやった場合はパッケージ名変えたりする必要があるからちょっと面倒。以下の設定にすればたぶん問題ない。
名前   :gs-rest-service
パッケージ:com.example.restservice
messageImage_1712146725547.jpg

SpringWEBだけ使用するのでチェックを入れる。
messageImage_1712153097948.jpg

Springフレームワークの中にWEBサービスの開発を補助するフレームワークとしてSpringWEBがあるイメージ。
そしてSpringフレームワークを使用した開発をより快適にするためのSpringBootというイメージでいる。

手順実施

ここからは手順にあるファイルを作成していく。途中よくわからなかったところがあったので調べながら実施した。

全然見慣れないコードが出てきたけど冗長なコードをめちゃくちゃはしょれるrecordクラスというJava16くらいで追加された機能らしい。

package com.example.restservice;

public record Greeting(long id, String content) { }

ビルドする

springbootの特徴として組み込みwebサーバーがある。デフォルトでtomcatが組み込まれたjarを作成出来るのでそれをサーバーに置いてjarを実行するだけで良くなった。アプリを起動するとtomcatを起動してデプロイされてがサイクルの一部となると理解している。面倒なデプロイが不要になり、サーバー上に置くだけで良くなるので便利。tomcat以外にも使えるけど今は割愛。

Gradleでビルドすると実行可能jarがbuild/libs配下に作成される。
これはプロジェクトを作る際にしれっとjarを作ることを選択していたのでそうなる。warも作れる。

jarを実行するコマンド例は以下で、jarは作成されたものを指定する。
java -jar "D:\Spring学習\workspace-sub\demo\build\libs\demo-0.0.1-SNAPSHOT.jar"

eclipseからgradleのビルドを実施する方法は以下参照。

jarを実行すると以下のような出力がされる。

>java -jar "D:\Spring学習\workspace-sub\demo\build\libs\demo-0.0.1-SNAPSHOT.jar"

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

2024-04-04T10:17:56.478+09:00  INFO 8960 --- [demo] [           main] c.e.restservice.RestServiceApplication   : Starting RestServiceApplication v0.0.1-SNAPSHOT using Java 17.0.10 with PID 8960 (D:\Spring学習\workspace-sub\demo\build\libs\demo-0.0.1-SNAPSHOT.jar started by socce in C:\Users\socce)
2024-04-04T10:17:56.492+09:00  INFO 8960 --- [demo] [           main] c.e.restservice.RestServiceApplication   : No active profile set, falling back to 1 default profile: "default"
2024-04-04T10:17:59.116+09:00  INFO 8960 --- [demo] [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port 8080 (http)
2024-04-04T10:17:59.169+09:00  INFO 8960 --- [demo] [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2024-04-04T10:17:59.169+09:00  INFO 8960 --- [demo] [           main] o.apache.catalina.core.StandardEngine    : Starting Servlet engine: [Apache Tomcat/10.1.19]
2024-04-04T10:17:59.234+09:00  INFO 8960 --- [demo] [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2024-04-04T10:17:59.236+09:00  INFO 8960 --- [demo] [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 2619 ms
2024-04-04T10:18:00.052+09:00  INFO 8960 --- [demo] [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port 8080 (http) with context path ''
2024-04-04T10:18:00.272+09:00  INFO 8960 --- [demo] [           main] c.e.restservice.RestServiceApplication   : Started RestServiceApplication in 4.298 seconds (process running for 6.585)
動いているよ

最後の行は想定通りのjarが動いてるか確認するために入れたprintln。

この状態でhttp://localhost:8080/greetingにアクセスすると以下のようなjsonが表示される。
image.png

http://localhost:8080/greeting?name=spitzで好きなnameパラメータを設定してリクエストすればしっかり反映されている。
image.png

とりあえず実施完了。

感想

初めてspringbootのプロジェクトを作成して、gradleも初めてだったので良い経験になった。
準備で手間取ったところが多いけど今回で環境構築はできたので次からはきっと捗る。

0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?