Configuration and annotation analysis of Knife4j framework

Knife4j is an online API documentation framework based on Swagger 2. 

In Spring Boot, when using this framework, you need to: 

- Add dependencies 
- Enable enhanced mode in the configuration file (`application.properties`) 
- Write configuration classes (the code is relatively fixed, CV is recommended)
About the dependent code:
<!-- Knife4j Spring Boot:在线API -->
<dependency>
    <groupId>com.github.xiaoymin</groupId>
    <artifactId>knife4j-spring-boot-starter</artifactId>
    <version>2.0.9</version>
</dependency>
For enabling enhanced mode, add in `application.properties`:
knife4j.enable=true

Regarding the configuration class, create `config.Knife4jConfiguration` under the root package of the project, the code is as follows: **Note: Please check the value of the `basePackage` property! **

 
 

 

**Note: The above code is applicable to versions below Spring Boot 2.6 (excluding 2.6)! ** 

After completion, restart the project, open the browser, and access Knife4j's API documentation through http://localhost:9080/doc.html.

Regarding the Knife4j framework, a series of annotations are provided to facilitate the display of API documents, including:
1. @Api: Add it to the controller class and configure its `tags` attribute to specify the module name. In the specified module name, you can use the number number as the prefix of the name, and then multiple management modules will be listed in the order of number show 
2. @ApiOperation: add it to the method of processing the request in the controller class, and configure its `value` attribute to specify the name of the business interface

3.

@ApiOperationSupport: Add it to the method of processing requests in the controller class, configure its `order` attribute to specify the sort number of the business interface, and finally, multiple business interfaces in the same module will be arranged in ascending order of this number

4.

@ApiModelProperty: Added to the properties of the POJO class, configure its `value` attribute to specify the name (description) of the request parameter, and configure its `required` attribute to specify "whether this request parameter must be submitted" (only used For display, without inspection function), configure its `example` attribute to specify an "example"

5.

@ApiImplicitParam: Add to the method of processing the request in the controller class, configure its `name` attribute, specify the variable name of the parameter of the method, configure its `value` attribute, specify the description of this parameter, configure its `required` attribute, Specify whether this parameter must be submitted, configure its `dataType` attribute, and specify the data type of this parameter

6.

@ApiImplicitParams: Add to the method of processing the request in the controller class. When there are multiple parameters to configure, use this annotation, and the value of this annotation is an array of `@ApiImplicitParam`

7.

@ApiIgnore: added to the parameters of the method that handles the request. When a parameter does not need to be displayed in the API document, you need to add this annotation to the parameter, such as `HttpServletRequest`, `HttpSession`, etc.

Guess you like

Origin blog.csdn.net/weixin_71583566/article/details/126634522