1、pom
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.1</version> <relativePath/> <!-- lookup parent from repository --> </parent>
<knife4j.version>2.0.9</knife4j.version>
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>${knife4j.version}</version>
</dependency>
2、配置yml,springboot2.6以下不需要配置
spring:
mvc:
pathmatch:
matching-strategy: ant_path_matcher #springboot 2.6x 以上, knife4j配置
3、配置类
@Configuration
@EnableSwagger2WebMvc
public class Knife4jConfig {
@Bean(value = "defaultApi2")
public Docket defaultApi2() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(new ApiInfoBuilder()
.title("Demo RESTful APIs")
.description("# APIs")
.termsOfServiceUrl("http://localhost/")
.contact(new Contact("author", "url", "xxx@163.com"))
.version("1.0")
.build())
//分组名称
.groupName("1.0版本")
.select()
//这里指定Controller扫描包路径
.apis(RequestHandlerSelectors.basePackage("com.example.demo.controller"))
.paths(PathSelectors.any())
.build();
}
}