要使用Knife4j,需要进行以下配置步骤:
在pom.xml文件中添加依赖:
<!-- Knife4j -->
<dependency><groupId>com.github.xiaoymin</groupId><artifactId>knife4j-spring-boot-starter</artifactId><version>3.0.2</version>
</dependency>
在Spring Boot的配置类上添加注解@EnableKnife4j
,例如:
@Configuration
@EnableKnife4j
public class SwaggerConfig {// 配置内容
}
配置Swagger相关参数,可以在SwaggerConfig类中添加如下配置:
@Configuration
@EnableKnife4j
public class SwaggerConfig {@Beanpublic Docket createRestApi() {return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select().apis(RequestHandlerSelectors.basePackage("com.example.controller")).paths(PathSelectors.any()).build();}private ApiInfo apiInfo() {return new ApiInfoBuilder().title("API文档").description("Knife4j API文档").version("1.0").build();}
}
启动Spring Boot应用程序,访问http://localhost:8080/doc.html即可查看生成的API文档。
注意:以上步骤是最基本的配置,可以根据实际需要进行更多的配置。更多的配置选项可以参考Knife4j的官方文档。