1

ジャージーを使用して自分のサイトのRESTfulAPIを作成し始めたところです。これは、JavaでRESTfulサービスに対する独自のサポートを提供する必要があることからの素晴らしい変化です。私が理解できないことの1つは、DELETEおよびPUTメソッドを「偽造」する方法です。

ジャージーはアノテーション@PUTと@DELETEをサポートしていますが、多くのロードバランサーはこれらのメソッドを許可しません。これまで、私はカスタムHTTPヘッダー(例:x-method-override:DELETE)とPOSTリクエスト内の「トンネリング」を定義する機能に依存していました。

誰かがJersey/JAX-RSアノテーションを使用してメソッドをカスタムヘッダーにバインドする方法を見つけましたか?あるいは、PUTとDELETEのサポートの欠如を回避するためのより良い方法はありますか?

4

2 に答える 2

2

これが、API 内で状況を処理する方法を決定した方法です。比較的単純で、追加のコーディングはあまり必要ありません。Address の RESTful API を例に説明します。

@Path("/address")
public class AddressService {

    @GET
    @Produces("application/xml")
    public StreamingOutput findAll() { ... }

    @POST
    @Produces("application/xml")
    @Consumes("application/x-www-form-urlencoded")
    public StreamingOutput create(...) { ... }

    //
    // This is the alternative to a "PUT" method used to indicate an "Update"
    // action.  Notice that the @Path expects "/id/{id}" which allows 
    // us to bind to "POST" and not get confused with a "Create"
    // action (see create() above).
    //
    @POST
    @Produces("application/xml")
    @Consumes("application/x-www-form-urlencoded")
    @Path("/id/{id}")
    public StreamingOutput update(@PathParam("id") Long id, ...) { ... }

    //
    // This is the typical "GET" method with the addition of a check
    // for a custom header "x-method-override" which is designed to 
    // look for inbound requests that come in as a "GET" but are 
    // intended as "DELETE".  If the methodOverride is set to "DELETE"
    // then the *real* delete() method is called (See below)
    //
    @GET
    @Produces("application/xml")
    @Path("/id/{id}")
    public StreamingOutput retrieve(
      @PathParam("id") Long id, 
      @HeaderParam("x-method-override") String methodOverride)
    {
      if (methodOverride != null && methodOverride.equalsIgnoreCase("DELETE")) {
        this.delete(id);
      }

      ...
    }


    // 
    // This is the typical "DELETE" method.  The onlything special about it is that
    // it may get invoked by the @GET equivalent is the "x-method-override" header
    // is configured for "DELETE"
    //
    @DELETE
    @Produces("application/xml")
    @Path("/id/{id}")
    public StreamingOutput retrieve(@PathParam("id") Long id) { ... }

}

于 2009-09-28T03:21:33.367 に答える
1

これはもう REST ではありませんが、同様の状況で、POST /collection/ を (通常どおり) 挿入、POST /collection/{id} を更新、POST /collection/{id} を削除するように定義しました。

于 2011-07-05T21:16:24.350 に答える