我使用的 SpringBoot、knife4j 版本jar包如下所示:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.5.RELEASE</version>
<relativePath/>
</parent>
<!-- 引入 knife4j组件, swagger-bootstrap-ui框架. -->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>2.0.7</version>
</dependency>
下文中 Knife4jConfiguration.java 文件,其代码是我直接从 springboot-demo 项目中直接拷贝过来放到 MyBatis-SpringBoot 项目中。
springboot-demo项目Controller层包路径为: com.moon.controller ,MyBatis-SpringBoot 项目Controller层包路径为:com.moon.mybatis.controller 。 由于我忘记修改 Knife4jConfiguration.java 文件中 "RequestHandlerSelectors.basePackage(...)" 包路径,导致项目运行后,Swagger文档中没有接口信息。
Controller层包路径修改正确后,运行项目,Swagger文档中接口信息正常展示。
Knife4jConfiguration.java 文件
package com.moon.mybatis.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMethod;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.ResponseMessageBuilder;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.ResponseMessage;
import springfox.documentation.service.Tag;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;
import java.util.ArrayList;
import java.util.List;
/**
* 自定义版 Knife4j 组件配置文件.
*/
@Configuration
@EnableSwagger2WebMvc
public class Knife4jConfiguration {
@Bean(value = "defaultApi3")
public Docket defaultApi2() {
List<ResponseMessage> responseMessageList = new ArrayList<>();
responseMessageList.add(new ResponseMessageBuilder().code(200).message("Success").build());
responseMessageList.add(new ResponseMessageBuilder().code(400).message("参数错误").build());
responseMessageList.add(new ResponseMessageBuilder().code(401).message("没有认证").build());
responseMessageList.add(new ResponseMessageBuilder().code(403).message("没有访问权限").build());
responseMessageList.add(new ResponseMessageBuilder().code(404).message("找不到资源").build());
responseMessageList.add(new ResponseMessageBuilder().code(500).message("服务器内部错误").build());
ApiInfo apiInfoBuilder = new ApiInfoBuilder().title("Knife4j swagger-bootstrap-ui RESTful APIs")
.description("Knife4j swagger-bootstrap-ui RESTful APIs ~~~~~")
.termsOfServiceUrl("http://www.xxx.com")
.contact(new Contact("张三", "http://www.moon.com", "xx@qq.com"))
.version("1.0")
.build();
Docket docket = new Docket(DocumentationType.SWAGGER_2)
.useDefaultResponseMessages(false)
.globalResponseMessage(RequestMethod.GET, responseMessageList)
.globalResponseMessage(RequestMethod.POST, responseMessageList)
.globalResponseMessage(RequestMethod.PUT, responseMessageList)
.globalResponseMessage(RequestMethod.DELETE, responseMessageList)
.apiInfo(apiInfoBuilder)
.tags(new Tag("baidu", "百度接口API"), getTags())
//分组名称
.groupName("2.X版本")
.enable(true)
.select()
//这里指定Controller扫描包路径
// .apis(RequestHandlerSelectors.basePackage("com.moon.controller"))
.apis(RequestHandlerSelectors.basePackage("com.moon.mybatis.controller"))
.paths(PathSelectors.any())
.build();
return docket;
}
private Tag[] getTags() {
return new Tag[] {
new Tag("alibaba", "阿里巴巴API接口"),
new Tag("tencent", "腾讯API接口")
};
}
}
程序运行后,Swagger接口文档信息展示如下所示:
Swagger 接口文档访问地址为:http://localhost:8080/doc.html#/home
本案例项目Demo地址为: Mybatis框架学习 。
如果大家觉得我的项目还行,可以加一个 Start ,感谢各位老铁了。