原始问题:
spring boot如何加入Knife4j
要在Spring Boot项目中使用Knife4j,需要进行以下步骤:
- 在pom.xml文件中添加Knife4j的依赖:
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>3.0.2</version>
</dependency>- 在Spring Boot的配置类中添加Knife4j的配置:
@Configuration
@EnableSwagger2Doc
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.demo.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Spring Boot中使用Swagger2构建RESTful APIs")
.description("更多Spring Boot相关文章请关注:http://www.xxx.com/")
.termsOfServiceUrl("http://www.xxx.com/")
.contact("xxx")
.version("1.0")
.build();
}
}其中,@EnableSwagger2Doc注解用于启用Knife4j,createRestApi()方法用于创建Docket对象,apiInfo()方法用于设置API文档的基本信息。
- 启动Spring Boot应用程序,访问
http://localhost:8080/doc.html即可查看API文档。
以上就是在Spring Boot中使用Knife4j的步骤。
Prev:科举制的积极作用