SpringBoot整合Knife4j和swagger2接口文档

SpringBoot整合Knife4j和swagger2接口文档,第1张

配置Knife4j

1.pom.xml文件引入依赖

<!--knife4j-->
<!-- Knife4jSwagger2不能同时引用 -->
<dependency>
    <groupId>com.github.xiaoymin</groupId>
    <artifactId>knife4j-spring-boot-starter</artifactId>
    <version>2.0.8</version>
</dependency>

2.配置类Knife4jConfig.ava

@Configuration
@EnableSwagger2WebMvc
@EnableKnife4j
@ConfigurationProperties(prefix = "swagger.config")
public class Knife4jConfig {

    @Bean(value = "API接口文档")
    public Docket defaultApi2() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(new ApiInfoBuilder()
                        .title("API接口文档")
                        .description("# swagger-bootstrap-ui-demo RESTful APIs")
                        .termsOfServiceUrl("http://localhost:8888/swagger-ui.html")
                        .version("v2.0")
                        .build())
                //分组名称
                .groupName("2.X版本")
                .select()
                //这里指定Controller扫描包路径
                .apis(RequestHandlerSelectors.basePackage("com.zky.music.controller"))
                .paths(PathSelectors.any())
                .build();
    }
}

3.controller接口注解
类注解:
@Api(tags = "随便填 ")
方法注解:
@ApiOperation(value = "随便填 ")
即可使用,地址:localhost:8080/doc.html
其余注解不在赘述

配置Swagger2

1.pom.xml文件引入依赖

<!--Swagger2-->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.2</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.9.2</version>
</dependency>
 

2.配置类SwaggerConfig .java

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    Docket docket(){
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.zky.music.controller"))
                .paths(PathSelectors.any())
                .build().apiInfo(new ApiInfoBuilder()
                        .description("API文档")
                        .contact(new Contact("yuan",
                                "https://github.com/MJUniversity/xxxxxxx",
                                "2584278161@qq.com"))
                        .version("v1.0")
                        .title("API文档")
                        .license("Apache2.0")
                        .licenseUrl("http://www.apache.org/licenses/LICENSE-2.0")
                        .build());
    }
}

3.controller接口注解
类注解:
@Api(tags = " 随便填")
方法注解:
@ApiOperation(value = "随便填 ")
即可使用,地址:localhost:8080/swagger-ui.html
其余注解不在赘述

很明显,knife4j更好用

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/758002.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-01
下一篇 2022-05-01

发表评论

登录后才能评论

评论列表(0条)

保存