LoginSignup
73
73

More than 5 years have passed since last update.

EclipseでJersey(JAX-RS)を始める

Posted at

はじめに

最近jersey使うようになって、結構便利なので始め方をメモします。

手順

環境の準備

必要なものをインストールします(括弧内は今回使ったもの)
* Java (jdk8)
* tomcat (Tomcat7.0.54)
* Eclipse (Luna Java EE)

Eclipse起動、設定

Eclipseを起動

archetypeの登録

サーバーにTomcatを追加

  • 設定(Preference)からServer->Runtime Environmentsを開く
  • 追加でインストールしたTomcatを選択、保存
  • サーバービューを開く
  • ビュー内右クリック->サーバー追加から追加する

プロジェクト作成

  1. メニューから新規プロジェクト->Maven Projectを選択
  2. workspaceは任意(デフォルトでOK)に設定してOK
  3. Archetype選択画面でgroupId=org.glassfish.jersey.archetypes、ArtifactId=jersey-quickstart-webappを選択(間違ってgroupId=com.sun.jersey.archetypesの方を選ばないように)
  4. Maven ProjectのGroupId, ArtifactId, Version, Packageを設定(任意)してFinish

今回は次のように設定しました

  • GroupId=com.github.kamegu (公開する可能性があるなら他の人と重複しないように)
  • ArtifactId=jersey-eclipse (プロジェクト名となります)
  • Version=0.0.1-SNAPSHOT (デフォルトのまま)
  • package=com.github.kamegu.first

Eclipseのプロジェクトが出来てると思います。
プロジェクト上でエラーとなっているかも知れませんが「javax.servlet.http.HttpServletが見つからない」というエラーなら問題ありません。
サーバービューからサーバーを起動して下記URLを開く
http://localhost:8080/jersey-eclipse/
"jersey-eclipse"はプロジェクト名に置き換えてください
ページが表示され"Jersey resource"というリンクがあるはずですのでこれをクリックします。
"Got it!"と表示されていればOKです。

動作の説明

JAX-RS2.0ではリソースを提供するために主にアプリケーションクラスとリソースクラスが必要です。
アプリケーションクラスはjavax.ws.rs.core.Applicationインターフェースを実装するクラスで主に設定を司る。リソースクラスはリソースに対応するメソッドを持つ。

アプリケーションクラス

上で作成されたプロジェクトではアプリケーションクラスは作られていない。アプリケーションクラスがあれば、自動的にそれが読み込まれるが、ないばあいはweb.xmlに登録する必要がある。実際にweb.xmlに次のように登録されている。

web.xml
    <servlet>
        <servlet-name>Jersey Web Application</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>jersey.config.server.provider.packages</param-name>
            <param-value>com.github.kamegu.first</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey Web Application</servlet-name>
        <url-pattern>/webapi/*</url-pattern>
    </servlet-mapping>

Jersey document

リソースクラス

リソースクラスは一つだけ上のpackageで指定したパッケージに作られている。

MyResource.java
@Path("myresource")
public class MyResource {
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String getIt() {
        return "Got it!";
    }
}

web.xmlで指定されている/webapp/とMyResourceの@Pathアノテーションで指定されている"myresource"をコンテキストパスである"jersey-eclipse"に連結した/jersey-eclipse/webapp/myresourceがgetItメソッドに対応するURLとなる。
先ほどの"Jersey resource"のリンクURLと一致しているはず。
メソッドにも@Pathアノテーションを付けられる。たとえば@Path("aa/bb")とするとURLは"〜〜myresource/aa/bb"となる

@GETアノテーションがついているので、GETリクエストに対応する。

ここまでをGitHubに挙げてます
https://github.com/kamegu/jersey-eclipse-test/tree/0.0.1-create-project

ちょっと修正

今のままだと今後の拡張性が乏しいので修正する。

依存ライブラリ変更

まず、tomcat7なのでservlet3に対応するように
jersey-container-servlet-core -> jersey-container-servlet に変更

jspファイルの「javax.servlet.http.HttpServletが見つからない」エラーを解消するためにservlet-apiを追加

アプリケーションクラスを作成

Applicationインターフェースを直接実装してもいいが、jerseyにはResourceConfigという便利なクラスがあるので、それを継承して作る。

WebapiConfig
package com.github.kamegu.first;

import javax.ws.rs.ApplicationPath;

import org.glassfish.jersey.server.ResourceConfig;

@ApplicationPath("webapi")
public class WebapiConfig extends ResourceConfig {
    public WebapiConfig() {
        packages(this.getClass().getPackage().getName());
//      packages("com.github.kamegu.first");
    }
}

これがweb.xmlにあるservlet設定と同等の意味を持つ。
なので、web.xmlの設定は削除する。

ここまでをGitHubに挙げてます。
https://github.com/kamegu/jersey-eclipse-test/tree/0.0.1-change-config

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