2010-02-11

REST with Spring 3.0, Spring MVC and Dojo. Part 5 - DELETE

In the fifth part of the series (which is also the last one, at least for now) we will take a look at the DELETE part.

DELETE, as its name suggests, is for deleting items and it is done by ID which means you don't need to send the entire object to the server via REST.

Operation REST method Sample URI
Return item(s) GET /books (all books)
/books/ (all books)
/books/12 (one book with ID = 12)
Create a new item POST (you pass the ID in the object itself)
/books
/books/
Update an item PUT (you pass the ID in the object itself)
/books
/books/
Delete an item DELETE /books/12 (book with ID = 12)

The Spring MVC controller part


@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public void handleBookDelete(HttpServletResponse response,
            @PathVariable("id") Long bookId) throws IOException {
        BooksBean booksBean = Factory.getBooksBean();
        try {
            booksBean.deleteBook(bookId);
        } catch (BookNotFoundException e) {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
        }
    }

This is a delete action - the RequestMapping method is DELETE, as you can see. When the book does not exist - we throw a 404, like we did in the previous example.

The Dojo part


function deleteBook(bookId) {
    var deferred = _request("xhrDelete", {
        url: "http://localhost:8080/restsample-0.0.1-SNAPSHOT/rest/books/" +
            bookId + ".json",
        handleAs: "json"
    });
    deferred.addCallback(this, function(value) {
        alert("Book deleted.");
    });
    deferred.addErrback(this, function(value) {                
          alert("Error while deleting book: "  + value);
    });
}

This is Dojo code - simple JavaScript very similar to what we did before. It uses the xhrDelete method, that's it.

Conclusion


This is the end of the series, for now. What else is there to possibly improve or cover?
  • Error handling - something more automatic then writing to response with sendError.
  • Validation - still an open bug at Spring JIRA.
  • Your ideas?
I hope the information here will be helpful to someone!

Links


Download source code for this article

  1. REST with Spring 3.0, Spring MVC and Dojo. Part 1 - GET
  2. REST with Spring 3.0, Spring MVC and Dojo. Part 2 - GET from Dojo perspective
  3. REST with Spring 3.0, Spring MVC and Dojo. Part 3 - POST and JSR-303 validation
  4. REST with Spring 3.0, Spring MVC and Dojo. Part 4 - PUT (updating objects)
  5. REST with Spring 3.0, Spring MVC and Dojo. Part 5 - DELETE

6 comments:

  1. Hi Michal,

    Spring and REST are new for me. I was just wondering how I'd be able to managed incoming Form requests? I removed the MappingJacksonHttpMessageConverter and added FormHttpMessageConverter but it was still throwing me an error for the JSON Converter.

    Your help would be appreciated.

    Thanks,
    Salman

    ReplyDelete
  2. Hey Salman,

    I haven't tried that so I'm unable to give you any hints at the moment.

    If you are able to provide the source code I might be able to take a look.

    Best,
    Michal

    ReplyDelete
  3. Hi Michal,

    Removing the @RequestBody annotation to receive the request from a jsp worked for me.

    I just wanted to test my service using the browser but, now I am using the RestTemplate, so that helps.

    I have ran into a couple of issues with the RestTemplate and JSON converter as well but, it won't be apt to ask questions related to that over here.

    And apologies for not mentioning this earlier. Thanks a lot for putting this series of posts here. Much appreciated.

    -Salman

    ReplyDelete
  4. Dear Salman,

    I'm glad you managed to figure it out. There are some issues with @RequestBody and other annotations from Spring 3 MVC indeed, you might have stumbled upon one of them (a few are scattered around this series of articles).

    One thing I wanted to say is that for testing REST you might want to try HTTP4E, it's really helpful. I think it's only available as an Eclipse plugin and there is a free of charge trial version.

    Good luck,
    Michal

    ReplyDelete
  5. Hi Michal,

    Thank you for your tutorial. I had some difficulties sending JSON data to my REST endpoint. I kept on receiving HTTP 415 errors when sending the data.

    After I switched spring version to 3.0.5.RELEASE I found a clear error in my jetty console: One of my DTO's did not have a parameterless constructor. After fixing this I could send data without any problems.

    Thanks!

    Tim

    ReplyDelete