Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to support Restful in SpringBoot

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

How to support Restful in SpringBoot? I believe many inexperienced people are at a loss about it. Therefore, this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

First, the benefits of RESTful style API

RESTful is an API design style based on http method, not a new technology. Method and statuscode from url and http are both historic technologies.

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 standardizes the code development of programmers, reduces the tongue cost of interface communication for front-end and back-end interaction, and is the embodiment of "agreement is greater than configuration". Through the following design, let's understand these three sentences.

II. RESTful style API Design style REST 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/getDogsGET / 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 HTTP Method reflects the operation of the resource (verb)

GET: get resources

POST: adding resources

PUT: modifying resources

DELETE: deleting resources

In fact, these four verbs actually correspond to four operations of addition, deletion, modification and search, which uses HTTP verbs to express operations on resources.

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 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 many more http-statuscode. 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. Everyone remembers the three status codes, but not necessarily if there are more.

Get methods and query parameters should not change the data

It's up to POST, PUT, DELETE to change the data.

Use plural nouns

/ dogs instead of / dog

The expression of complex resource relationship

GET / cars/711/drivers/ returns all drivers using car 711s

GET / cars/711/drivers/4 returns driver number 4 using car 711

Advanced usage: HATEOAS

As the engine of application status, Hypermedia as the Engine of Application State hypermedia RESTful API had better do Hypermedia, or HATEOAS, that is, provide links in the returned results and connect to other API methods, so that users do not check the document and know what to do next. 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 to call next by reading this attribute.

Provide functions such as filtering, sorting, selection and paging for collections

Filtering filtering:

Filter with unique query parameters: GET / cars?color=red returns red carsGET / cars?seats

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report