Knife4j tutorial

Knife4j tutorial

Introduction: Knife4j is a Swagger-based API document generation and management tool, which provides a beautiful and easy-to-use interface to display and manage API interface documents. Knife4j has the following features:

  1. Integrated Swagger: Based on Swagger, Knife4j can directly parse Swagger annotations to generate API documents without additional configuration and documentation.

  2. Friendly interface: Knife4j provides a beautiful and easy-to-use interface, which can easily view and test the API interface. It supports the classification, grouping, sorting and other functions of the interface, making the organization and browsing of documents more convenient.

  3. Interface test: Knife4j has a built-in interface test function, which can directly test and debug the interface on the document interface without using other tools.

  4. Parameter verification: Knife4j supports verification of interface parameters, and can define parameter types, formats, verification rules, etc., to improve the security and reliability of the interface.

  5. Compatibility: Knife4j is compatible with the Swagger 2.0 specification and can be seamlessly integrated with commonly used Java frameworks such as Spring Boot and Spring MVC.

For the tutorial on using Knife4j, you can follow these steps:

  1. Introduce dependencies: Add Knife4j-related dependencies to your project. For specific dependency configurations, please refer to Knife4j's official documentation.

  2. Configure Swagger annotations: Add Swagger annotations to your API interface classes and methods to describe interface information, parameters, return values, etc.

  3. Start the project: To start your project, visit Knife4j's interface documentation page, which is accessible by path by default /doc.html.

  4. View and Test Interface: On Knife4j's documentation page, you can view and test your API interface. You can quickly locate the required interface through classification, search, etc., and click the interface to view detailed interface information and examples.

Before learning knife4j, you need to master the knowledge of Swagger, refer to this article: Swagger tutorial

On the basis of the above article, replace the dependencies.

Replace the Swagger dependency with

<!--引入swagger-->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.2</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.9.2</version>
</dependency>
<dependency>
    <groupId>io.swagger</groupId>
    <artifactId>swagger-annotations</artifactId>
    <version>1.5.22</version>
</dependency>
<dependency>
    <groupId>io.swagger</groupId>
    <artifactId>swagger-models</artifactId>
    <version>1.5.22</version>
</dependency>

knife4j dependencies

<dependency>
    <groupId>com.github.xiaoymin</groupId>
    <artifactId>knife4j-spring-boot-starter</artifactId>
    <version>3.0.3</version>
</dependency>

insert image description here
At the same time, you need to release this doc.html in this configuration file
insert image description here

After visiting the following page, there will be http://localhost:8080/doc.html

insert image description here

Guess you like

Origin blog.csdn.net/qq_51447496/article/details/132544667