http delete with REST
I am currently using Jersey Framework (JAX-RS implementation) for building RESTful Web Service开发者_运维百科s. The Resource classes in the project have implemented the standard HTTP operations - GET,POST & DELETE. I am trying to figure out how to send request parameters from client to these methods.
For GET it would be in the query string(extract using @QueryParam
) and POST would be name/value pair list (extract using @FormParam
) sent in with the request body. I tested them using HTTPClient and worked fine. For DELETE operation, I am not finding any conclusive answers on the parameter type/format. Does DELETE operation receive parameters in the query string(extract using @QueryParam
) or in the body(extract using @FormParam
)?
In most DELETE examples on the web, I observe the use of @PathParam
annotation for parameter extraction(this would be from the query string again).
Is this the correct way of passing parameters to the DELETE method? I just want to be careful here so that I am not violating any REST principles.
Yes, its up to you, but as I get REST ideology, DELETE URL should delete something that is returned by a GET URL request. For example, if
GET http://server/app/item/45678
returns item with id 45678,
DELETE http://server/app/item/45678
should delete it.
Thus, I think it is better to use PathParam than QueryParam, when QueryParam can be used to control some aspects of work.
DELETE http://server/app/item/45678?wipeData=true
The DELETE method should use the URL to identify the resource to delete. This means you can use either path parameters or query parameters. Beyond that, there is no right and wrong way to construct an URL as far as REST is concerned.
You can use like this
URL is http://yourapp/person/personid
@DELETE
@Path("/person/{id}")
@Produces(MediaType.APPLICATION_JSON)
public Response deletePerson(@PathParam("id") String id){
Result result = new Result();
try{
persenService.deletePerson(id);
result.setResponce("success");
}
catch (Exception e){
result.setResponce("fail");
e.printStackTrace();
}
return Response.status(200).entity(result).build();
}
@QueryParam
would be the correct way. @PathParam
is only for things before any url parameters (stuff after the '?'). And @FormParam
is only for submitted web forms that have the form content type.
精彩评论