Signup/Sign In

POST, PUT and DELETE Http Requests

In this post we will understand how to implement POST (Create), PUT (Update) and DELETE (Remove) operations in the REST web service.

@POST
@Produces(MediaType.APPLICATION_XML)
public String addStudent(Student student)
{
	
    return studentService.addStudent(student);

}

@PUT
@Produces(MediaType.APPLICATION_XML)
@Path("/{rollno}")
public Student updateStudent(@PathParam("rollno")int rollno ,Student student)
{
	student.setRollNo(rollno);
	return studentService.update(student);
}

@DELETE
@Produces(MediaType.APPLICATION_XML)
@Path("/{rollno}")
public String removeStudent(@PathParam("rollno") int rollno)
{
return studentService.delete(rollno);

}

The following methods are added to the Students class and annotated with the @POST, @PUT and @DELETE annotations. The important point to note here is that the @POST method is implemented at the {Root-Path}/Students/.

Therefore, it is the responsibility of the application to generate the roll no and create a new record. The rollno will not be sent in the POST request.

One more important point to note is that, the browser can only send a GET request. So to send the POST, PUT and DELETE request we need to install a REST client. We will install the rest client in the next lesson and send the respective requests from the REST client.