JavaEE7とGlassfish4.0でJAX-RSを試してみた

タイトルの通り。
tomcat + Jersey でやってたことと同じ事ができるのか試してみました。

f:id:hiwa4:20130618100841p:plain
新規プロジェクトで「Java Web」=>「Webアプリケーション」



f:id:hiwa4:20130618100855p:plain
次へ。



f:id:hiwa4:20130618100858p:plain
次へ。



f:id:hiwa4:20130618100859p:plain
プロジェクト右クリックから「新規」「パターンからのRESTful~」



f:id:hiwa4:20130618100902p:plain
次へ。



f:id:hiwa4:20130618100907p:plain
リソースパッケージetcを埋めて終了。



f:id:hiwa4:20130618100910p:plain
生成直後はこんな構成。



ShopResource.java を以下の内容に修正。

package example.resource;

import java.util.ArrayList;
import java.util.List;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.Produces;
import javax.enterprise.context.RequestScoped;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.MediaType;

/**
 * REST Web Service
 *
 * @author Administrator
 */
@Path("Shop")
@RequestScoped
public class ShopResource {

    @Context
    private UriInfo context;

    public ShopResource() {
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public List<Shop> getJson() {
        List<Shop> shopList = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            Shop shop = new Shop();
            shop.setShopId(String.valueOf(i));
            shop.setShopName("shopName" + i);
            shopList.add(shop);
        }
        return shopList;
    }

    @Path("{shopNo}")
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Shop getJson2(@PathParam("shopNo") String shopNo) {
        Shop shop = new Shop();
        shop.setShopId(shopNo);
        shop.setShopName("URIから取得したshopです");

        return shop;
    }
}

箱となるクラスを新規作成。

package example.resource;

/**
 *
 * @author iwa4
 */
public class Shop {

    private String shopId;
    private String shopName;

    public String getShopId() {
        return shopId;
    }

    public void setShopId(String shopId) {
        this.shopId = shopId;
    }

    public String getShopName() {
        return shopName;
    }

    public void setShopName(String shopName) {
        this.shopName = shopName;
    }
}

最後に画面。

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>JAX-RS test</title>
  </head>
  <body>
    <h1>JAX-RS test</h1>
    <button id="button1">get json!</button>
    <button id="button2">get json2!</button>
    <div id="list"></div>
    <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
    <script>
      $("#button1").click(function() {
        $.getJSON("webresources/Shop", function(data) {
          for (var i in data) {
            $("#list").append("<li>" + data[i].shopName + "</li>");
          }
        });
      });

      $("#button2").click(function() {
        $.getJSON("webresources/Shop/999", function(data) {
          $("#list").append("<li>" + data.shopName + "</li>");
        });
      });
    </script>
  </body>
</html>

実行してみる。
f:id:hiwa4:20130618113802p:plain
「get json!」「get json2!」を押下すると、それぞれJSONを取得している。



f:id:hiwa4:20130618100919p:plain
動作確認するにはこのテスト・リソースURIが楽だなぁと思いました。



関係ないけど、Rictyフォントに変えてから少し幸せになりました。