In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly shows you "how to use SpringBoot to create Rest interface API", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to use SpringBoot to create Rest interface API" this article.
The first is a brief introduction to REST:
REST stands for Representational State Transfer. Is an architectural style, design style rather than standard, can be used to design Web services, can be used from a variety of clients.
In REST, resources are specified by URI, and the addition, deletion, modification and query of resources can be realized through the get,post,put,delete method provided by HTTP.
With Rest, caching can be used more efficiently to improve response speed, while communication session state is maintained by the client. Let different servers handle different requests in a series of requests to improve the scalability of the server.
1. Preparatory work
Based on the basic design of REST, which is an operation controlled by a set of verbs
Create operation: HTTP POST should be used
Query operation: HTTP GET should be used
Update operation: HTTP PUT should be used
Delete operation: HTTP DELETE should be used
As a REST services developer or client, you should adhere to the above standards.
Environmental tools for the project
SpringBoot 2.0.1.RELEASE
Gradle 4.7
IDEA 2018.2
MySQL5.7
2. Project structure diagram
3. Start
Here is a way to explain Restful
For / user/api HTTP GET to request to get all users
For / user/api HTTP POST to create a user
For / user/api/1 HTTP GET request to get a user with an id of 1
Update for / user/api/1 HTTP PUT request
Delete the user with id 1 for the / user/api/1 HTTP DELETE request
HTTP GET request / user/api query all
URL: http://localhost:8080/user/api
TTP GET request / user/api/65 is queried according to id
URL: http://localhost:8080/user/api/65
HTTP POST request / user/api create user
URL: http://localhost:8080/user/api
HTTP PUT request / user/api/65 to update user information
URL: http://localhost:8080/user/api/65
HTTP DELETE request / user/api/85 to delete a user with an id of 85
URL: http://localhost:8080/user/api/85
4. Business layer and dao layer code
UserService.java interface
The code for PageResultBean and ResultBean is in GitHub.
Https://github.com/cuifuan/springboot-demo
Through the above code, if you have already mastered the above code, I believe you have a general grasp of REST. Nowadays, front-end Client emerges one after another, and back-end interfaces may come from different platforms. At this time, you need to request a batch of interfaces, while RESTful-style api makes people know what to do from the way of the request and the address, and know the result by returning the code status.
The direct convenience of using RESTful:
Previous interface
Delete / user/delete
Add / user/create
Single query / user/queryById
Query all / user/queryAll
Update / user/update
After using RESTful to design API / user/api a URL address to solve the problem, you no longer have to waste your tongue with the front end, and the GET request is idempotent. What is idempotent? To put it simply, the return effect of multiple requests is the same. For example, when GET requests a resource, no matter how many times it is requested, it will not create changes to the data and other operations. PUT is also used to update data. No matter how many times the request is executed, it will have the same effect.
Because PUT requests URL to completely create or replace resources at the client-defined URL, PUT is idempotent. DELETE requests are also idempotent and are used to delete operations. In fact, REST is equivalent to a style specification.
Note, GET request please do not use in delete operations, you have to ask me why not, you want to do that, in fact, the entire CRUD operation you can also use GET to complete, , this is just a development design style.
Finally, Spring Boot is a brand-new development framework under the Spring family, which is mainly designed to simplify the process of creating and developing Spring applications. It provides features such as automatic configuration and start-up dependence, which frees developers from a large number of XML configurations. Spring Boot is committed to becoming a leader in the booming field of rapid application development (rapid application development).
The above is all the content of the article "how to use SpringBoot to create Rest interface API". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.