Search code examples
javarestput

Use @PathParams with PUT REST Api


I am creating a REST service and I have the following API -

    @PUT
    @Path("/delete/{teamName}")
    @Consumes(MediaType.TEXT_PLAIN)
    @Produces(MediaType.TEXT_PLAIN)
    public Response delete(@PathParam("teamName")String teamName) {
        if (db.delete(teamName)) {
            return Response.status(200).build(); 
        }
        return Response.status(400).build();
    }

This API accepts a PathParam to delete a teamname. It is a PUT call. My questions are -

  1. Is it a good practice to use @PathParams in a PUT call?

  2. What are the advantages and disadvantages?


Solution

  • It's OK to use PathParam in any kind of RESTful API resources handlers. But, PUT method is not suitable here since it breaks basic REST principles.

    REST supposes that specific HTTP methods (like PUT, GET, DELETE, POST) correspond to specific actions that need be performed on the data. For example, if you want to retrieve the data, you need to use GET, while DELETE is what you need to delete the data.

    In your particular case I'd go with something like

    DELETE /teams/{teamName}
    

    I'd recommend you to acknowledge at least Use RESTful URLs and actions to have a better understanding of basic REST principles.