crud操作_使用HTTP方法:CRUD操作

crud操作

Now we will see how to use the HTTP methods to perform the basic crud operations in the registration app.

现在,我们将看到如何使用HTTP方法在注册应用程序中执行基本的Crud操作。

Suppose we want to GET the data of the student with student id 10. The resource URI for this operation will be /myApp/students/10.

假设我们要GET学生ID为10的学生的数据。此操作的资源URI为/myApp/students/10

And if we want to DELETE the data of the student with the student id 10. The resource URI for this operation will be /myApp/students/10

如果我们想DELETE学生ID为10的学生的数据,则此操作的资源URI为/myApp/students/10

Now how will the server identify that which request is for the GET operation and which request is for the DELETE operation for the same URI. The answer is, by checking the HTTP method parameter. The method for the first operation should be GET and for the second operation, it should be DELETE.

现在,服务器将如何针对同一URI识别出哪个请求是针对GET操作的,哪个请求是针对DELETE操作的。 答案是,通过检查HTTP方法参数。 第一个操作的方法应该是GET ,第二个操作的方法应该是DELETE

The common HTTP methods are:

常见的HTTP方法是:

  • GET

    GET

  • POST

    POST

  • PUT

    PUT

  • DELETE

    DELETE

There are some other methods that are rarely used like HEAD and OPTIONS. These HTTP methods have specific meaning and you typically use the right method for the right operation. Also while surfing the Internet, like when you are browsing a webpage you are actually using these methods automatically. When you type a URL in the address bar of your browser, the browser automatically issues a GET request to the browser. When u submit an HTML Form, the browser uses the POST request to do so. The idea is to use the right method depending on the operation.

还有一些其他很少使用的方法,例如HEADOPTIONS 。 这些HTTP方法具有特定的含义,您通常使用正确的方法进行正确的操作。 同样在浏览Internet时,例如当您浏览网页时,实际上实际上是在使用这些方法。 在浏览器的地址栏中键入URL时,浏览器会自动向浏览器发出GET请求。 当您提交HTML表单时,浏览器会使用POST请求进行提交。 想法是根据操作使用正确的方法。

When we talk about REST application, you do not name methods like, getStudents. You make a GET request for the /myApp/students/ URI.

当我们谈论REST应用程序时,您不会命名诸如getStudents之类的方法。 您对/myApp/students/ URI进行GET请求。

我们的注册申请的用例 (Use Cases for our Registration Application)

There are certain use cases of Resgistration Application like:

注册应用程序有某些用例,例如:

获取学生/课程信息 (Getting the Student/Course Information)

This should be getting the resource URI.

这应该获取资源URI。

GET   /myApp/students/      // This will give the list of all the students in the system.
GET  /myApp/students/1     // This will give the student with the roll number 1.

Similarly we can get the course information

同样,我们可以获得课程信息

GET  /myApp/courses/         // This wil return the list of all the courses of the students.
GET /myApp/registrations/   // this will give all the registrations information.

删除学生/课程信息 (Deleting the Student/Course Information)

To delete any student or course,

要删除任何学生或课程,

DELETE /myApp/students/1      // This will delete the student record with the roll number 1.
DELETE /myApp/couses/10      // This will delete the course with course id 10.

更新学生/课程信息 (Updating the Student/Course Information)

To update student or course information,

要更新学生或课程信息,

PUT /myApp/students/1   //This will update the student record with the roll number 1.
PUT /myApp/courses/10  //This will update the course record with course id 10.

The data to be updated, will go in the body of the HTTP POST request.

要更新的数据将放在HTTP POST请求的正文中。

插入学生/课程信息 (Inserting the Student/Course Information)

This is really important as now we have to create a new entry for the student or course information.

这非常重要,因为现在我们必须为学生或课程信息创建一个新条目。

The request in this case should be sent to the collection URI and not to the single resource, for example:

在这种情况下,该请求应发送到集合URI,而不是单个资源,例如:

POST  /myApp/students/20    //This is incorrect
POST /myApp/students/      //This is correct

So for creating a new resource we should always send the request to the collection URI and leave the responsibility of creating the roll no. or course id to the application.

因此,对于创建新资源,我们应该始终将请求发送到集合URI,并保留创建滚动编号的责任。 或应用程序的课程ID。

So when we do post request, POST /myApp/students/ with the request body, the application will create a new resource and generate a new roll number and return this to the user as response.

因此,当我们使用请求正文发布请求POST /myApp/students/时,该应用程序将创建一个新资源并生成一个新的编号,并将其作为响应返回给用户。

As of now, we have only learnt, how to operate on a single resource. But what if we information about all of the students, courses and registrations. This is a Collection URI.

到目前为止,我们仅学习了如何在单个资源上进行操作。 但是,如果我们提供有关所有学生,课程和注册的信息,该怎么办? 这是一个集合URI。

For Example:

例如:

GET /myApp/students/           //This will return all the records of the students.
GET /myApp/courses/            //This will return all the records of the courses.
GET /myApp/registrations/     //This will return all the records of all the registrations.

So in this lesson we have identified the basic HTTP methods which can meet our requirements when designing an API as these HTTP methods map to the CRUD operations.

因此,在本课程中,我们确定了设计API时可以满足我们要求的基本HTTP方法,因为这些HTTP方法映射到CRUD操作。

翻译自: https://www.studytonight.com/rest-web-service/using-http-methods

crud操作

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值