2

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?

3
  • 2
    1. It's ok to use PathParam in PUT call. 2. You should use DELETE with PathParam instead of PUT for delete operation. 3. You shouldn't use verbs like 'delete' in your path. 4. It depends if teamName is unique identifier - if yes, then it's ok, if no - you should use id in Pathparam. Commented Aug 7, 2017 at 6:24
  • Yeah you most of the time should (would), as PUT implies a resource that exists, usually with a unique identifier. The @PathParam signifies the identifier is variable. Commented Aug 7, 2017 at 6:26
  • @Consumes annotation specifies what kind of data the server expects in the body of the request (i.e. not in the URL or query parameters). If your endpoint expects only @PathParam then you don't need @Consumes since you're not reading whatever is in the body anyway. Commented Aug 7, 2017 at 6:43

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.