Question

Je viens juste de commencer à utiliser Jersey pour créer une API RESTful pour mon site. Son un changement merveilleux d'avoir à rouler mon soutien aux services RESTful en Java. Une chose que je ne arrive pas à comprendre comment est « faux » une méthode SUPPRIMER et PUT.

Jersey prend en charge les annotations @PUT et @DELETE, mais beaucoup équilibreurs de charge ne permettra pas à ces méthodes par. Dans le passé, je suis appuyé sur la possibilité de définir un en-tête HTTP personnalisé (par exemple x-méthode-override: SUPPRIMER). Et « tunnel » dans une requête POST

Quelqu'un at-il trouvé un moyen de lier une méthode utilisant Jersey / JAX-RS annotations aux en-têtes personnalisés? Sinon, est-il une meilleure façon autour du manque de soutien pour PUT et DELETE?

Était-ce utile?

La solution

est bien ici que j'ai décidé de gérer la situation dans mon API. Son relativement simple et ne nécessite pas de codage beaucoup plus. Pour illustrer envisager une api RESTful pour adresse:

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

}

Autres conseils

Il est pas vraiment REPOS plus, mais dans une situation similaire nous avons défini POST / collection / à insérer (comme d'habitude), POST / collection / {id} pour être mise à jour, POST / collection / {id} sans corps pour être supprimer.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top