Jersey を用いた Web API 作成 - その1

新人研修でJavaを学んでいるが、まだ座学がメインなので、勉強がてら手を動かしてみたいと思ったのが動機。
今の所、本家サイトに沿って進めています。

環境構築

最初に、以下がインスコされているか確認します。(括弧内は執筆当時の僕の環境)

使っているOSは macOS Sierra

プロジェクトの作成

以下のような mvn コマンドを実行します。

mvn archetype:generate -DarchetypeArtifactId=jersey-quickstart-grizzly2 \
-DarchetypeGroupId=org.glassfish.jersey.archetypes -DinteractiveMode=false \
-DgroupId=your.domain -DartifactId=your-service-name -Dpackage=your.domain \
-DarchetypeVersion=2.27

-DgroupId-DartifactId-Dpackage の右辺は自身のドメインやサービス名に置き換えてください。
上記のコマンドを実行すると以下のような構造のプロジェクトが作成されます。

your-service-name
├── pom.xml
└── src
    ├── main
    │   └── java
    │       └── your
    │           └── domain
    │               ├── Main.java
    │               └── MyResource.java
    └── test
        └── java
            └── your
                └── domain
                    └── MyResourceTest.java

とりあえず動かしてみる

mvn clean test を実行すると、テスト( your-service-name/test/java/your/domain/MyResourceTest.java に記述されているテスト)が実行され、ビルドが行われます。
ビルドに成功した場合、以下のようなログが出力されます。

[INFO] Scanning for projects...
Downloading from central: https://repo.maven.apache.org/maven2/org/glassfish/jersey/jersey-bom/2.27/jersey-bom-2.27.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/glassfish/jersey/jersey-bom/2.27/jersey-bom-2.27.pom (22 kB at 18 kB/s)
[INFO]
[INFO] ---------------------< your.domain:your-service-name >----------------------
[INFO] Building your-service-name 1.0-SNAPSHOT
[INFO] ----------------------------------[ jar ]-----------------------------------
Downloading from central: https://repo.maven.apache.org/maven2/org/codehaus/mojo/exec-maven-plugin/1.2.1/exec-maven-plugin-1.2.1.pom
...(中略)...
-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running your.domain.MyResourceTest
4 14, 2018 9:20:03 午前 org.glassfish.grizzly.http.server.NetworkListener start
情報: Started listener bound to [localhost:8080]
4 14, 2018 9:20:03 午前 org.glassfish.grizzly.http.server.HttpServer start
情報: [HttpServer] Started.
4 14, 2018 9:20:03 午前 org.glassfish.grizzly.http.server.NetworkListener shutdownNow
情報: Stopped listener bound to [localhost:8080]
Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 2.109 sec

Results :

Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 27.850 s
[INFO] Finished at: 2018-04-14T09:20:03+09:00
[INFO] ------------------------------------------------------------------------

上記のようなログが確認できたら mvn exec:java を実行します。
正常に実行できていれば以下のようなログが出力されるはずです。

[INFO] Scanning for projects...
[INFO]
[INFO] ---------------------< your.domain:your-service-name >----------------------
[INFO] Building your-service-name 1.0-SNAPSHOT
[INFO] ----------------------------------[ jar ]-----------------------------------
[INFO]
[INFO] >>> exec-maven-plugin:1.2.1:java (default-cli) > validate @ your-service-name >>>
[INFO]
[INFO] <<< exec-maven-plugin:1.2.1:java (default-cli) < validate @ your-service-name <<<
[INFO]
[INFO]
[INFO] --- exec-maven-plugin:1.2.1:java (default-cli) @ your-service-name ---
Downloading from central: https://repo.maven.apache.org/maven2/junit/junit/3.8.2/junit-3.8.2.pom
Downloaded from central: https://repo.maven.apache.org/maven2/junit/junit/3.8.2/junit-3.8.2.pom (747 B at 699 B/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/maven-plugin-api/2.0/maven-plugin-api-2.0.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/maven-plugin-api/2.0/maven-plugin-api-2.0.pom (601 B at 2.6 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/apache/maven/maven/2.0/maven-2.0.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/maven/maven/2.0/maven-2.0.pom (8.8 kB at 35 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/apache/commons/commons-exec/1.1/commons-exec-1.1.pom
Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/commons/commons-exec/1.1/commons-exec-1.1.pom (11 kB at 43 kB/s)
Downloading from central: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-container-default/1.0-alpha-9/plexus-container-default-1.0-alpha-9.jar
Downloading from central: https://repo.maven.apache.org/maven2/org/apache/commons/commons-exec/1.1/commons-exec-1.1.jar
Downloaded from central: https://repo.maven.apache.org/maven2/org/codehaus/plexus/plexus-container-default/1.0-alpha-9/plexus-container-default-1.0-alpha-9.jar (195 kB at 345 kB/s)
Downloaded from central: https://repo.maven.apache.org/maven2/org/apache/commons/commons-exec/1.1/commons-exec-1.1.jar (53 kB at 73 kB/s)
4 14, 2018 9:20:19 午前 org.glassfish.grizzly.http.server.NetworkListener start
情報: Started listener bound to [localhost:8080]
4 14, 2018 9:20:19 午前 org.glassfish.grizzly.http.server.HttpServer start
情報: [HttpServer] Started.
Jersey app started with WADL available at http://localhost:8080/myapp/application.wadl
Hit enter to stop it...

上記のようなログが出てきたら http://localhost:8080/myapp/application.wadl へアクセスしてみましょう。
今回はcURLで叩いてみます。すると、以下のようなXMLが取得できました。

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<application xmlns="http://wadl.dev.java.net/2009/02">
    <doc xmlns:jersey="http://jersey.java.net/" jersey:generatedBy="Jersey: 2.27 2018-04-10 07:34:57"/>
    <doc xmlns:jersey="http://jersey.java.net/" jersey:hint="This is simplified WADL with user and core resources only. To get full WADL with extended resources use the query parameter detail. Link: http://localhost:8080/myapp/application.wadl?detail=true"/>
    <grammars/>
    <resources base="http://localhost:8080/myapp/">
        <resource path="myresource">
            <method id="getIt" name="GET">
                <response>
                    <representation mediaType="text/plain"/>
                </response>
            </method>
        </resource>
    </resources>
</application>

また、上記のXMLの resources タグの中を見てみると、 http://localhost:8080/myapp/myresource にアクセスできそうな雰囲気がありますね。
http://localhost:8080/myapp/myresource にアクセスしてみると、 Got it! というプレーンテキストが取得できました。
ひとまず動作を確認することができました。

さいごに

今回は環境構築からプロジェクトの起動までやってみました。
次回はプログラムの中身を見て理解を深めていきたいと思います。