“This is the seventh day of my participation in the First Challenge 2022.
๐จ๐ Author: Bug Bacteria
โ๏ธ blog: CSDN, Nuggets, etc
๐ public account: Magic House of the Circle of the Apes
๐ซ special statement: original is not easy, reprint please attach the original source link and this article statement, thank you for your cooperation.
๐ Copyright notice: part of the text or pictures in the article may come from the Internet or Baidu Encyclopedia, if there is infringement, please contact bug bacteria processing.
Hi, family. I’m the bug. Here I go again. Today we are going to talk about something, OK, and we will continue the Series of articles on SpringBoot. Hope to help more beginners quickly start!
In the process of reviewing articles, if you think the articles are helpful to you at all, please don’t be too mean with your likes and bravely light up the articles ๐. Your likes (collect โญ๏ธ+ pay attention to ๐จ port + message board) are the best encouragement and support for bugs on my creation path. Time does not abandon ๐๐ปโ๏ธ, creation stopped ๐, refueling ๐ป
One, foreword
Since we explained swagger configuration in the last issue, we will not repeat it in this issue. If you are a new partner in this issue, please finish the teaching of my article in the last issue first, so as to avoid detours, ok?
Now, let’s get down to business. Is everybody ready?
How to generate Swagger document
Last time, we could access Swagger online document normally, but the document is empty, right? Next, I will teach you how to automatically generate relevant interfaces into the document, and there is a good function point that supports online testing, equivalent to Postman interface testing. I just wanted to know if you were in love.
The following is a demonstration of the use of a few API, it is done, is not a sense of Jio is very simple.
1. Configure the controller
First we’ll use two annotations @api and @apiOperation; I will introduce the use and function of specific annotations below. Here, I will directly use them.
/** * User management dispenser */ @RestController@RequestMapping("/user") @api (tags = "user management module ",description =" user management module ") // Modify the entire class, Public class UserController {@autoWired private UserService UserService; @GetMapping("/get-users") public List<UserEntity> getUserList() { return userService.getUsers(); }}Copy the code
Restart the project, and something magical happens. Take a look at the screenshot below:
Let’s use @apiOperation to describe the interface as follows:
@getMapping ("/get-users") @apiOperation (value = "db1; notes =" db1; notes = "db1 List<UserEntity> getUserList() { return userService.getUsers(); }Copy the code
Restart the project again, please see:
The text description you just configured appears, which is convenient to remind the front-end development of the detailed description of the interface.
Then you click debug below, click Send, that is:
The result returns:
Doesn’t it look like Postman? Yes, online debugging is also integrated. Both for yourself and the front-end development is extremely convenient, after all, the test interface is very convenient ah, do not need to postman to enter the full access path and other related interface call information, everything becomes very simple. Do you have any? Simply send the request.
2. Configure the entity parameter structure
Let’s do another parameter body configuration and see what happens?
@data @apiModel (value = "QueryUserInfoModel ",description =" QueryUserInfoModel ")public class QueryUserInfoModel {@apiModelProperty (" gender "); private String sex; @apimodelproperty (" className ") private String className; @apiModelProperty (" userIds ") private List<Integer> userIds; }Copy the code
Then combined with the above mentioned, the interface is described.
@postmapping ("/ getuser-by-ids ") @apiOperation (value = "Query user information based on user ids",notes = Public List<UserInfoVo> getUserById(@requestBody QueryUserInfoModel Model){return userMapper.getUsersByIds(model.getUserIds()); }Copy the code
Restarting the project again, we can see that the parameter bodies are described; Doesn’t it look good. This makes it more transparent for non-developers to use, knowing what each parameter means at a glance, and eliminating the need to configure additional documentation for auxiliary testing. It is very convenient for the front-end and testing for non-professionals, so quickly integrate it into your own projects and use it.
The required field in the screenshot above is also derived from the @apiModelProperty annotation. The required field is false by default. If you want to tell the front end and test interface personnel that this parameter must be filled in, You simply set the required attribute to true, which means that this parameter is required. Otherwise, it will definitely not be called through the interface.
For example, the following code shows that the gender field is a mandatory parameter.
@APIModelProperty (value = "gender ", Required = true)Copy the code
As mentioned above, I have made the field of gender. Do you know what will happen when you call the interface through swagger file assuming no transmission? All smart people know that the field is empty and the interface call failed.
Swagger call returns:
And then we’re going to fill in the gender field with a random value, and then we’re going to call it, and we’ll see at this point.
As you can see, the interface call succeeded and the data query returned.
This is obviously due to the fact that you set the required = true attribute to trigger the alert, leaving it blank to indicate that the field is optional. Generally, the interface is defined, if the parameter is mandatory, add this field, to remind the relevant personnel interface call, this parameter is mandatory, do not fill in the interface can not be called. That’s basically the usage case.
There are a lot of API how to use, looking forward to friends to explore, here to show you the basic several commonly used, the rest depends on your consciousness and enthusiasm.
. .
OK, that’s all for this episode. If you have any questions, feel free to comment in the comments section. See you next time.
Three, the past popular recommendation
- How to integrate Swagger2 with Springboot
- Springboot: How to integrate Swagger2 online Interface files
- Springboot series (12) : How to code the configuration of sending email reminders
- Springboot series (12) : how to code a simple email send
- Springboot series (12) : how to code the picture with attachment mail
- Springboot series (12) : how to achieve the code to send a large copy of the mail – attached complete code
- . .
If you want to learn more, you can pay attention to the bug bug column “SpringBoot Zero-based Introduction”, from scratch, from zero to one! Hope I can help you.
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
โ๏ธ Be who you want to be, there is no time limit, you can start whenever you want,
๐ You can change from now on, you can also stay the same, this thing, there are no rules to speak of, you can live the most wonderful yourself.
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
โ
๐ If this article is helpful to you, please leave a like! (# ^. ^ #);
๐ if you like the article shared by bug fungus, please give bug fungus a point of concern! (เน ‘แด โต เน);
๐ if you have any questions about the article, please also leave a message at the end of the article or add a group [QQ communication group :708072830];
๐ In view of the limited personal experience, all views and technical research points, if you have any objection, please directly reply to participate in the discussion (do not post offensive comments, thank you);
๐ copyright notice: original is not easy, reprint please attach the original source link and this article statement, all rights reserved, piracy will investigate!! thank you