In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
After completing the above configuration, we can actually produce the content of the document, but such a document is mainly aimed at the request itself, and the description is mainly generated by functions and other names, which is not user-friendly. We usually need to add some instructions to enrich the document content. As shown below, we add notes to API with @ ApiOperation annotations and parameters through @ ApiImplicitParams and @ ApiImplicitParam annotations.
@ RestController@RequestMapping (value= "/ users") / / configure here so that the following mappings are all under / users, removing public class UserController {static Map users = Collections.synchronizedMap (new HashMap ()); @ ApiOperation (value= "get user list", notes= ") @ RequestMapping (value= {"}, method=RequestMethod.GET) public List getUserList () {List r = new ArrayList (users.values ()); return r } @ ApiOperation (value= "create user", notes= "create user from User object") @ ApiImplicitParam (name = "user", value= "user detail entity user", required = true, dataType = "User") @ RequestMapping (value= ", method=RequestMethod.POST) public String postUser (@ RequestBody User user) {users.put (user.getId (), user); return" success " } @ ApiOperation (value= "get user details", notes= "get user details according to url's id") @ ApiImplicitParam (name = "id", value= "user ID", required = true, dataType = "Long") @ RequestMapping (value= "/ {id}", method=RequestMethod.GET) public User getUser (@ PathVariable Long id) {return users.get (id) } @ ApiOperation (value= "update user details", notes= "specifies the update object according to the id of url And update the user details according to the passed user information ") @ ApiImplicitParams ({@ ApiImplicitParam (name =" id ", value=" user ID ", required = true, dataType =" Long "), @ ApiImplicitParam (name =" user ", value=" user detail user ", required = true, dataType =" User ") @ RequestMapping (value=" / {id} ", method=RequestMethod.PUT) public String putUser (@ PathVariable Long id) @ RequestBody User user) {User u = users.get (id) U.setName (user.getName ()); u.setAge (user.getAge ()); users.put (id, u); return "success" } @ ApiOperation (value= "delete user", notes= "specify deletion object according to url's id") @ ApiImplicitParam (name = "id", value= "user ID", required = true, dataType = "Long") @ RequestMapping (value= "/ {id}", method=RequestMethod.DELETE) public String deleteUser (@ PathVariable Long id) {users.remove (id); return "success";}}
Complete the above code addition, start the Spring Boot program, and visit: http://localhost:8080/swagger-ui.html
. You can see the RESTful API page shown earlier. We can click on the specific API request. Taking the / users request of POST type as an example, we can find the Notes information we configured in the above code and the description information of the parameter user, as shown in the following figure.
API document access and debugging
In the page requested above, we see that the Value of user is an input box. Yes, in addition to viewing the interface function, Swagger also provides debugging and testing functions. We can click on the Model Schema on the right side of the above figure (yellow area: it indicates the data structure of User). At this time, there is a template for user object in Value. We just need to modify it slightly and click "Try it out!" below. Button, you can complete a request call!
At this point, you can also use several GET requests to verify that the previous POST request is correct.
Compared with the work of documenting these interfaces, the additional configuration is very small and concise, and the intrusion of the original code is also within tolerance. Therefore, while building RESTful API, it is a good choice to add swagger to manage API documents.
The company recently upgraded its e-commerce system to convert all e-commerce functions into a distributed micro-service model.
To understand the springcloud architecture, please add: three, six, two, four, seven, two, 59
Source code source
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.