I am practicing building RESTFUL API's using the RESTEasy implementation. But I am still a little bit confused about what should I return according to the HTTP method. This is the way that I am doing it for the moment:
@GETreturns an Entity@POSTreturns an HTTP response with the actual entity created@PUTreturns an HTTP response with the actual entity updated@DELETEreturns an HTTP response with the actual entity deleted
Here is an example of my controller for illustration.
@GET
@Path("/{bookId}")
@Produces(MediaType.APPLICATION_JSON_VALUE)
public Book getBookById(@PathParam("bookId") Integer bookId) {
return bookService.getBookById(bookId);
}
@POST
@Consumes(MediaType.APPLICATION_JSON_VALUE)
@Produces(MediaType.APPLICATION_JSON_VALUE)
public Response inertBook(@Valid Book book) {
return bookService.insertBook(book);
}
@DELETE
@Path("/{bookId}")
public Response deleteBook(@PathParam("bookId") Integer bookId) {
return bookService.deleteBook(bookId);
}
@PUT
@Path("/{bookId}")
@Consumes(MediaType.APPLICATION_JSON_VALUE)
@Produces(MediaType.APPLICATION_JSON_VALUE)
public Response updateBook(@PathParam("bookId") Integer bookId, @Valid Book book) {
return bookService.updateBook(bookId, book);
}