Google Cloud EndpointsをつかってGoogle App Engine上にREST apiを実装してみる

GAE上で動くREST apiを作りたかったのでちょっと調べたところ、Google Cloud Endpointsなるものを使うと楽に出来そうなので試して見ます。

基本的に本家チュートリアル
https://developers.google.com/appengine/docs/java/endpoints/getstarted/backend/
をなぞってみた作業ログです。

1. プロジェクトの作成
[ファイル] > [新規] > [その他] で [Mavenプロジェクト]を選択して[次へ]

[デフォルト・ワークスペース・ロケーションの使用]のみチェックした状態で[次へ]


[groupId]がcom.google.appengine.archetypes, [artifactId]がskeleton-archetypeであるアーキタイプを選択して[次へ]
(アーキタイプが一覧に表示されない場合は前回記事を参考に追加する)


[groupId]に com.google.devrel.samples.helloendpoints
[artifactId]に helloendpoints
を入力して[完了]

これでhelloendpointsという名前のプロジェクトが出来た。

2. プロジェクトの設定
2.1. 依存関係の追加

pom.xmlを開き[依存関係]タブ内左側の[追加]ボタンから、以下の二つを追加する

<dependency>
    <groupId>com.google.appengine</groupId>
    <artifactId>appengine-endpoints</artifactId>
    <version>${appengine.target.version}</version>
</dependency>
<dependency>
    <groupId>javax.inject</groupId>
    <artifactId>javax.inject</artifactId>
    <version>1</version>
</dependency>

pom.xmlを保存する。

2.2. プラグインの設定

pom.xmlを編集する。
ただし、本家チュートリアルのままやるとディレクトリの設定が変だって怒られるので以下のように修正する。

本家

   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.3</version>
    <configuration>
        <webXml>${project.build.directory}/generated-sources/appengine-endpoints/WEB-INF/web.xml</webXml>
        <webResources>
            <resource>
                <!-- this is relative to the pom.xml directory -->
                <directory>${project.build.directory}/generated-sources/appengine-endpoints</directory>
                <!-- the list has a default value of ** -->
                <includes>
                    <include>WEB-INF/*.discovery</include>
                    <include>WEB-INF/*.api</include>
                </includes>
            </resource>
        </webResources>
    </configuration>
</plugin>
||

変更後
>|xml|
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>2.3</version>
    <configuration>
        <webXml>${basedir}/src/main/webapp/WEB-INF/web.xml</webXml>
        <webResources>
            <resource>
                <!-- this is relative to the pom.xml directory -->
                <directory>${basedir}/src/main/webapp/WEB-INF</directory>
                <!-- the list has a default value of ** -->
                <includes>
                    <include>WEB-INF/*.discovery</include>
                    <include>WEB-INF/*.api</include>
                </includes>
            </resource>
        </webResources>
    </configuration>
</plugin>
||

appengine-maven-pluginのほうはそのまま使用
>|xml|
<plugin>
              <groupId>com.google.appengine</groupId>
              <artifactId>appengine-maven-plugin</artifactId>
              <version>${appengine.target.version}</version>
              <configuration>
                <enableJarClasses>false</enableJarClasses>
              </configuration>
              <executions>
                <execution>
                  <goals>
                    <goal>endpoints_get_discovery_doc</goal>
                  </goals>
                </execution>
              </executions>
            </plugin>

なんかeclipseだとpom.xmlでエラーが出てますがとりあえずmvn installが通るのでそのままいきます

3. Getを実装してみる

https://developers.google.com/appengine/docs/java/endpoints/getstarted/backend/write_api

からそのままHelloGreetingとGreetingsクラスをコピってきます。

本家チュートリアルはここで

mvn appengine:devserver

して

http://localhost:8080/_ah/api/explorer

にアクセスすると使えるapiのリストが表示されるよって書いてありますがエラーになってしまいました。なんかweb.xmlの設定が必要なようです。

ということでweb.xmlに以下を追加します

 <servlet>
  <servlet-name>SystemServiceServlet</servlet-name>
  <servlet-class>com.google.api.server.spi.SystemServiceServlet</servlet-class>
  <init-param>
   <param-name>services</param-name>
   <param-value>com.google.devrel.samples.helloendpoints.Greetings</param-value>
  </init-param>
 </servlet>
 <servlet-mapping>
  <servlet-name>SystemServiceServlet</servlet-name>
  <url-pattern>/_ah/spi/*</url-pattern>
 </servlet-mapping>

これで再び先ほどのURLにアクセスすると以下のようなページが表示されます。


getGreetingを選んで実行してみるとjsonが帰ってきているのが分かります


データストアとの連携とかバリデーションとか調べることはありますが、とりあえずapiの実装の流れがなんとなく分かったのでOKとします