Spring boot集成Swagger2实现自动生成API文档

本文介绍了如何在Spring Boot项目中集成Swagger2,通过添加必要的jar包,配置Swagger2配置类以及在Controller层添加注解,实现自动生成API文档。详细步骤包括添加依赖、创建配置类和启用Swagger2。完成配置后,可以通过指定URL访问生成的API文档。

spring boot集成swagger2实现自动生成API文档,集成配置步骤(gradle管理jar包方式)如下:

  1. 添加swagger2的jar包
    compile group: ‘io.springfox’, name: ‘springfox-swagger2’, version: ‘2.8.0’
    compile group: ‘io.springfox’, name: ‘springfox-swagger-ui’, version: ‘2.8.0’

  2. 添加swagger2配置类

    package com.easydatalink.token.config;

    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import springfox.documentation.builders.ApiInfoBuilder;
    import springfox.documentation.builders.PathSelectors;
    import springfox.documentation.builders.RequestHandlerSelectors;
    import springfox.documentation.service.ApiInfo;
    import springfox.documentation.service.Contact;
    import springfox.documentation.spi.DocumentationType;
    import springfox.documentation.spring.web.plugins.Docket;
    import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**

  • Swagger Api配置
  • @author wangXX
  • @version Created:2019年3月25日

*/
@Configuration
#@EnableSwagger2
public class Swagger2Config{
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2).forCodeGeneration(true)
.select()
.apis(RequestHandlerSelectors.any())
//过滤生成链接
.paths(PathSelectors.any())
.build()
.apiInfo(apiInfo());
}

//api接口作者相关信息
private ApiInfo apiInfo() {
    Contact contact = new Contact("wang", "", "pingfan_wzc@163.com");
    ApiInfo apiInfo = new ApiInfoBuilder()
            .license("Token管理API")
            .title("Token管理API")
            .description("接口文档")
            .contact(contact)
            .version("1.0")
            .build();
    return apiInfo;
}

}

  1. controller层添加swagger注解
    在这里插入图片描述
  2. spring boot启动类中添加@EnableSwagger2注解开启swagger,并配置api扫描路径要在扫描目录下,否则不能生成api
    在这里插入图片描述
  3. 访问地址:http://172.18.9.112:8885/swagger-ui.html#/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值