In this post we will understand how to implement POST
(Create), PUT
(Update) and DELETE
(Remove) operations in the REST web service.
在本文中,我们将了解如何在REST Web服务中实现POST
(创建), PUT
(更新)和DELETE
(删除)操作。
@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/.
下面的这些方法加入到Students
类,并与注释@POST
, @PUT
和@DELETE
注解。 这里要注意的重要一点是@POST
方法是在{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.
因此,应用程序有责任生成滚动号并创建新记录。 rollno将不会在POST请求中发送。
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.
还有一点需要注意的是,浏览器只能发送GET
请求。 因此,要发送POST,PUT和DELETE请求,我们需要安装REST客户端。 我们将在下一课中安装其余客户端,并发送来自REST客户端的相应请求。
翻译自: https://www.studytonight.com/rest-web-service/post-put-and-delete