Frage

Ich habe gerade begonnen, eine RESTful API für meine Website verwenden Jersey zu erstellen. Es ist eine willkommene Abwechslung von mit meiner eigenen Unterstützung für RESTful Services in Java rollen. Eine Sache, ich kann einfach nicht auf Figur scheinen heraus ist, wie man „fake“ eine DELETE und PUT-Methode.

Jersey unterstützt die Anmerkungen @PUT und @DELETE jedoch viele Last-Balancers werden diese Methoden nicht zulassen, dass durch. In der Vergangenheit habe ich auf die Fähigkeit verlassen einen benutzerdefinierten HTTP-Header (zum Beispiel x-Methodenüberschreibung: DELETE) zu definieren. Innerhalb einer POST-Anforderung und „Tunneling“

Hat jemand einen Weg gefunden, ein Verfahren unter Verwendung von Jersey / JAX-RS Anmerkungen benutzerdefinierte Header zu binden? Alternativ gibt es einen besseren Weg, um fehlende Unterstützung für PUT und DELETE?

War es hilfreich?

Lösung

Nun, hier ist, wie ich beschlossen habe, die Situation in meiner API zu behandeln. Seine relativ einfach und erfordert nicht viel zusätzliche Codierung erfordern. veranschaulichen, um eine RESTful api für Adresse betrachten:

@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) { ... }

}

Andere Tipps

Es ist REST nicht wirklich mehr, aber in einer ähnlichen Situation definierten wir POST / Sammlung / bis (als normal), POST / Sammlung / {id} einfügen werden Update sein, POST / Sammlung / {id} ohne Körper sein löschen.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top