SpringBoot集成Swagger

准备工作

第一步,搭建好我们的SpringBoot应用,可以通过Spring的官方网站下载应用模板,这里不做介绍。

在POM文件中添加依赖

    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.6.1</version>
    </dependency>

    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.6.1</version>
    </dependency>

上面的文件是api扫描,下面是提供的api界面。

在应用启动类中增加如下配置,代码实现如下:

/**
 * SwaggerConfig 
 * Swagger配置类
 *
 * @author tsuixh.
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("org.tsui.whatihavedone"))
                .paths(PathSelectors.any())
                .build();
    }

    /**
     * 用于配置API的信息
     * 
     * @return api信息
     */
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Swagger 2 integration")
                .description("How to integrate swagger2 into spring boot application.")
                .version("1.0")
                .build();
    }
}

然后启动SpringBoot应用,访问:[域名]/[根路径]/swagger-ui.html,笔者这里的请求路径为:http://localhost:8080/swagger-ui.html 就可以看到swagger的页面了,这里我没有在应用中创建任何api,所以这里没有显示。后续只需要在basePackage下面创建controller,就可以在这上面方便的进行调试了。
Swagger-ui界面
以上为如何通过配置来集成Swagger,有问题欢迎在评论中提出。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值