After completing the above configuration, can actually produce the document content, but such a document mainly for the request itself, and the description is mainly derived from functions such as naming, not user-friendly, we usually need to add some instructions to enrich the content of the document. As shown below, we add descriptions to the API by @apioperation annotations, and add descriptions to the parameters by @apiimplicitparams, @ApiImplicitParam annotations.
[Java] View plain copy
@RestController
@RequestMapping (value= "/users")//through this configuration so that the following mappings are under/users, can be removed
public class Usercontroller {
Static Map<long, user> users = Collections.synchronizedmap (new Hashmap<long, user> ()); @ApiOperation (value= "Get user list", notes= "") @RequestMapping (value={""}, Method=requestmethod.get) public list<user > Getuserlist () {list<user> r = new Arraylist<user> (Users.values ()); return R; } @ApiOperation (value= "Create user", notes= "create user according to User object") @ApiImplicitParam (name = "User", value = "Customer detailed entity user", required = TR UE, DataType = "user") @RequestMapping (value= "", method=requestmethod.post) public String postuser (@RequestBody the user use R) {users.put (User.getid (), user); Return "Success"; } @ApiOperation (value= "Get user Details", notes= "get user details based on the ID of the URL") @ApiImplicitParam (name = "id", value = "User id", required = t Rue, 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 based on the ID of the URL and updates the user details based on the passed user information") @ApiImplIcitparams ({@ApiImplicitParam (name = "id", value = "User id", required = true, DataType = "Long"), @ApiImp Licitparam (name = "User", Value = "Users detailed entity", required = true, DataType = "user")}) @RequestMapping (value= "/{id}", Me thod=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 delete object based on URL id") @ApiImplicitParam (name = "id", value = "User id", required = true, D Atatype = "Long") @RequestMapping (value= "/{id}", method=requestmethod.delete) public String deleteuser (@PathVariable Long ID) {users.remove (ID); Return "Success"; }
}
Complete the above code additions, start the Spring boot program, visit: http://localhost:8080/swagger-ui.html
。 You can see the page of the RESTful API shown earlier. We can then open 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.
Altalt
API documentation Access and debugging
On the requested page, we see that user's value is an input box? Yes, swagger in addition to the view interface function, but also provides debugging testing function, we can click on the right side of the model Schema (XXX region: It indicates the user's data structure), at this point in the value of the User object template, we only need a slightly modified, click on the below " Try it out! button to complete a request call!
At this point, you can also verify that the previous post request is correct by several get requests.
Compared to the work of writing documents for these interfaces, our added configuration content is very small and streamlined, and the intrusion into the original code is within the scope of tolerance. Therefore, it is a good choice to add swagger to manage API documents while building restful APIs.
Spring Cloud Spring Boot mybatis distributed micro-service Cloud Architecture (10)