Knife4j的前身是swagger-bootstrap-ui,前身swagger-bootstrap-ui是一个纯swagger-ui的ui皮肤项目。项目正式更名为knife4j,取名knife4j是希望她能像一把匕首一样小巧,轻量,并且功能强悍,更名也是希望把她做成一个为Swagger接口文档服务的通用性解决方案,不仅仅只是专注于前端Ui前端。
官网文档地址:Knife4j · 集Swagger2及OpenAPI3为一体的增强解决方案. | Knife4j
knife4j版本:3.0.3
SpringBoot版本:2.5.6
引入pom依赖
<dependency><groupId>com.github.xiaoymin</groupId><artifactId>knife4j-spring-boot-starter</artifactId><version>3.0.3</version></dependency>
Knife4jConfig配置类
非必须
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.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;@Configuration
@EnableOpenApi
public class Knife4jConfig {@Beanpublic Docket docket() {return new Docket(DocumentationType.OAS_30).useDefaultResponseMessages(false).apiInfo(apiInfo()).select().apis(RequestHandlerSelectors.basePackage("com.packingdata.account.controller")).paths(PathSelectors.any()).build();}private ApiInfo apiInfo() {return new ApiInfoBuilder().description("knife4j在线API接口文档").contact(new Contact("Packingdata", "http://www.packingdata.com", "admin@packingdata.com")).version("v1.0.0").title("knife4j在线API接口文档").build();}
}
编写测试接口
import com.packingdata.account.service.PackingdataMenuService;import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;/*** description 菜单表字段控制器** @author szc* @date 2022/05/13*/
@Api(tags={"菜单相关接口"})
@RestController
@RequestMapping("/menu")
public class PackingdataMenuController{@Autowiredprivate PackingdataMenuService packingdataMenuService;@ApiOperation("测试的接口")@GetMapping("/hello")public String hello() {return "hello";}
}
预览在线接口
http://localhost:8080/doc.html