REST POST method with Example – RESTful Web Services Tutorial

2 years ago Lalit Bhagtani 0

In REST POST is a method level annotation, this annotation indicates that the following method will respond to the HTTP POST request only. It is used to create or update a resource. The actual function performed by the POST annotated method is determined by the server side implementation and is usually dependent on the requested URI. The entity enclosed in the sent request is accepted as a new subordinate of the resource identified by the requested URI, for example, if resource is identified as a book then posted entity can be a chapters.

POST operation are not idempotent and it’s responses are not cacheable, unless the response includes appropriate Cache-Control or Expires header fields.

Let’s try to understand it with an example.

REST POST method Example :-

In this example of REST POST, we will hit this URL <base URL>/books/1 with name of the chapter in the body of the request. Consume annotation is used to bind message in the body of the request to the method argument chapterName.


public class PostMethodExample {

    public Response getAllBooks(@PathParam("id") String bookId,String chapterName){
	return Response.status(200)
		       .entity("Book id is : " + bookId + " and chapter name is : " + chapterName).build();

Result :-


References :-

  1. @POST Java Docs

That’s all for REST POST with Example. If you liked it, please share your thoughts in comments section and share it with others too.