In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces the relevant knowledge of "the method of Spring Boot developing RESTful interface and http protocol state". The editor shows you the operation process through an actual case, and the operation method is simple, fast and practical. I hope this article "how to develop RESTful interface and http protocol state by Spring Boot" can help you solve the problem.
First, the benefits of RESTful style API
API (Application Programming Interface), as its name implies, is a set of programming interface specifications in which clients and servers communicate with each other through requests and responses. REST (Representational State Transfer) describes state transfer, which determines the form and rules of the interface. RESTful is an API design style based on http method, not a new technology.
If you look at Url, you will know what resources you want.
If you look at http method, you will know what to do with resources.
If you look at http status code, you can see what the result is.
It provides a widely applicable specification for interface development, which reduces the tongue cost of interface communication for the front-end and back-end interaction, which reflects that the agreement is greater than the configuration. Through the following design, let's understand these three sentences.
Of course, not all interfaces can be expressed in the form of REST. In practical work, flexible use, the purpose of using RESTful style is to provide a unified standard for everyone, avoid unnecessary waste of communication costs, and form a general style. It's like everyone knows that holding out your thumb means "you're great", and most people understand it because you know the style and habit. But do not rule out the thumb in some areas to show other meaning, it is not suitable to use!
2. The design style of RESTful API is 2.1. RESTful is resource-oriented (noun)
When REST exposes resources through URI, it stresses that verbs do not appear in URI. For example:
Interface URI that does not conform to REST conforms to REST interface URI function GET / api/getDogs/ {id} GET / api/dogs/ {id} get a puppy GET / api/getDogsGET / api/dogs get all puppies GET / api/addDogsPOST / api/dogs add a puppy GET / api/editDogs/ {id} PUT / api/dogs/ {id} modify a puppy GET / api/deleteDogs/ {id} DELETE / api/dogs/ {id} delete a puppy 2.2, Use the HTTP method to represent the operation of the resource (verb)
GET: get and read resources
POST: adding resources
PUT: modifying resources
DELETE: deleting resources
In fact, these four verbs actually correspond to the four operations of addition, deletion, modification and search, which uses the HTTP verb to express the operation on the resource.
2.3. HTTP status code
Reflect the result of the action through HTTP status code. Do not customize it.
200 OK 400 Bad Request 500 Internal Server Error
In the interaction between APP and API, the result cannot escape these three states:
Everything is executed correctly as expected.-success.
Some errors have occurred in APP-client error (for example, verifying the user's input ID card, the result is a military officer's certificate, that is, the client input error)
Some errors have occurred in API-server-side errors (various coding bug or self-caused exceptions within the service)
These three states correspond to the above status code one by one. If you think these three states, the classification results are too broad, there are a lot of http-status code. It is recommended to follow the principle of KISS (Keep It Stupid and Simple). The above three status codes can cover more than 99% of the scenarios. These three status codes are remembered by everyone, and they are not often used, not necessarily if they are too many.
2.4. Get methods and query parameters should not change the data
It's up to POST, PUT, DELETE to change the data.
2.5. Use plural nouns
/ dogs instead of / dog
2.6. The expression of complex resource relationship
GET / cars/711/drivers/ returns all drivers who have used car number 711
GET / cars/711/drivers/4 returns driver No. 4 who has used car number 711.
2.7. Advanced usage: HATEOAS
HATEOAS:Hypermedia as the Engine of Application State hypermedia serves as the engine of application state.
It is best for RESTful API to be HATEOAS, that is, to provide links in the returned results to other API methods, so that the user knows what to do next without looking up the document. For example, when a user makes a request to the root of api.example.com, he or she will get such a document.
{"link": {"rel": "collection https://www.example.com/zoos"," href ":" https://api.example.com/zoos", "title": "List of zoos", "type": "application/vnd.yourformat+json"}}
The above code indicates that there is a link attribute in the document, and the user will know what API or API to call next by reading this attribute.
2.8. Representation of resource filtering, sorting, selection, and paging
2.9. Versioning your API
Mandatory addition of API version statement, do not release no version of API. Such as: / api/v1/blog
Open for extension and closed for modification: that is to say, after the development of a version of the interface is tested and launched, we generally do not modify the interface. If there are new requirements, we will develop a new interface for functional extension. The purpose of this is that when your new interface comes online, it will not affect users who use the old interface. If the purpose of the new interface is to replace the old interface, do not modify the original interface in the v1 version, but develop the v2 interface and declare that the v1 interface is obsolete!
This is the end of the introduction on "the method of Spring Boot developing RESTful interface and http protocol status". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.