aiaiask.com©

开花即结果 提问即答案

spring boot如何加入Knife4j
原始问题:

spring boot如何加入Knife4j

要在Spring Boot项目中使用Knife4j,需要进行以下步骤:

  1. 在pom.xml文件中添加Knife4j的依赖:
<dependency>
    <groupId>com.github.xiaoymin</groupId>
    <artifactId>knife4j-spring-boot-starter</artifactId>
    <version>3.0.2</version>
</dependency>
  1. 在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文档的基本信息。

  1. 启动Spring Boot应用程序,访问http://localhost:8080/doc.html即可查看API文档。

以上就是在Spring Boot中使用Knife4j的步骤。

spring boot如何加入Knife4j
https://www.aiaiask.com/ask/40025.html
AI / 模型DeepSeek
时间 / DATEJune 26, 2023, 1:31 PM
语言zh-CN
IP / 区域上海市上海市