Detailed tutorial on using knife interface document generator

Posted by liro on Sat, 18 Dec 2021 21:27:54 +0100

knife Gitee address: https://gitee.com/xiaoym/knife4j

1, Introduction to interface generator knife

In previous projects, swagger has been used to generate background interface documents, which is easy to use, at least more convenient than writing interface documents in word and debugging interfaces in postman. Swagger provides a set of front-end pages, but annotations need to be added to the code, such as @ Api @ApiOperation. The coupling degree is relatively high, but it is very convenient to use. For me with obsessive-compulsive disorder, the swagger UI page is extremely ugly, which gives me the feeling that it is particularly messy, and there is no way to save commonly used parameters, which is very inconvenient to use.

This article introduces knife, an enhanced version of swagger, developed by the same author. The UI enhancement package mainly includes two core functions: documentation and online debugging

  • Document description: according to Swagger's specification description, list the description of the interface document in detail, including interface address, type, request example, request parameter, response example, response parameter, response code and other information. Using knife, you can see the usage of the interface at a glance according to the document description.
  • Online debugging: it provides the powerful function of online interface joint debugging, automatically parses the current interface parameters, and includes form verification. Calling parameters can return interface response content, headers, Curl request command instance, response time, response status code and other information to help developers debug online without having to test whether the interface is correct through other testing tools. Introduction and powerful. You can also cache request parameters and set header or query global parameters and values.

2, Renderings


3, SpringBoot integrates knife

3.1 pom.xml file dependency

Because it is an enhanced UI package of springfox swagger, the basic functions still depend on swagger, and the jar package of springfox swagger must be introduced. Then introduce knife4j spring UI to replace the original swagger UI.

<dependency>
  <groupId>io.springfox</groupId>
  <artifactId>springfox-swagger2</artifactId>
  <version>2.9.2</version>
</dependency>

<dependency>
  <groupId>com.github.xiaoymin</groupId>
  <artifactId>knife4j-spring-ui</artifactId>
  <version>3.0.2</version>
</dependency>

3.2 write Swagger2Config configuration file

package com.rich.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.AuthorizationScope;
import springfox.documentation.service.Contact;
import springfox.documentation.service.SecurityReference;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * Author: Rich
 * Date: 2021/8/3 9:59
 * Description: Swagger2 Configuration class
 */
@Configuration
@EnableSwagger2
@EnableKnife4j
public class Swagger2Config {

	@Bean
	public Docket createRestApi() {
		return new Docket(DocumentationType.SWAGGER_2)
				.host("http:localhost:8081")
				.apiInfo(apiInfo())
				.select()
				.apis(RequestHandlerSelectors.basePackage("com.rich.controller"))
				.paths(PathSelectors.any())
				.build()
	}

	public ApiInfo apiInfo(){
		return new ApiInfoBuilder()
				.title("Rich's Blog Interface documentation")
				.description("Rich's Blog Interface documentation")
				.contact(new Contact("Rich","http:localhost:8081/doc.html","501049950@qq.com"))
				.version("1.0")
				.build();
	}

}

The default access address of knife is http: / / ${host}: ${port} / doc html

3.3 precautions

Access doc. In SpringBoot HTML or swagger UI The solution of HTML 404
Configuring Interceptors

@Configuration
public class IntercpetorConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // Set swagger static resource access
        registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}

More interface documentation

yapi

Topics: Java RESTful