View RSS Feed

Spring Framework

RESTful DELETE Method in Spring

Rate this Entry
by , 11-28-2011 at 12:59 AM (4766 Views)
Continuing on my previous tip, of showing how to write RESTful methods, in this tip, I will show you how to write RESTful methods using the verb HTTP Verb, PUT.

There are times when we might want to get rid of a resource in its entirety. In the case we would use the HTTP DELETE method in order to accomplish this. In this case, I will add another method to the CarController that handles the DELETE requests to remove a Car resource:

Java Code:
@RequestMapping(value="/{id}", method=RequestMethod.DELETE) 
@ResponseStatus(HttpStatus.NO_CONTENT) 
public void deleteCar(@PathVariable("id") long id) {
   carService.deleteCar(id);
}
Similar to the last tip, the @RequestMapping annotation is similar to the one used on getCar() and putCar(). It differs in that this methodís @RequestMapping has its method attribute set to handle DELETE requests. The URL pattern that identifies a Car resource remains the same. Again similar to putCar() and deleteCar(), it is annotated with @ResponseStatus to let the client know whether the request was processed successfully. Remember that no content will be returned in the response. In the next post, we will create a resource using POST methods.

Submit "RESTful DELETE Method in Spring" to Facebook Submit "RESTful DELETE Method in Spring" to Digg Submit "RESTful DELETE Method in Spring" to del.icio.us Submit "RESTful DELETE Method in Spring" to StumbleUpon Submit "RESTful DELETE Method in Spring" to Google

Updated 11-30-2011 at 01:40 PM by Spring Framework

Categories
REST , HTTP

Comments