前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Spring Boot & Swagger

Spring Boot & Swagger

作者头像
十毛
发布2019-03-27 15:02:03
6440
发布2019-03-27 15:02:03
举报

本文接着前面的继续,介绍如何快速接入Swagger

两步接入

  • pom.xml
代码语言:javascript
复制
<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>
  • 代码配置
代码语言:javascript
复制
@Configuration
@EnableSwagger2
public class SwaggerConfig {                                    
    @Bean
    public Docket api() { 
        return new Docket(DocumentationType.SWAGGER_2)  
          .select()                                  
          .apis(RequestHandlerSelectors.any())              
          .paths(PathSelectors.any())                          
          .build();                                           
    }
}

展示

  • 数据接口 http://localhost:8080/swagger-ui.html
  • UI http://localhost:8080/v2/api-docs

Swagger文档展示

已经实现了最简单的接入了


其他配置

  • 限制开发环境展示 使用Spring条件注入特性@Profile("dev")
代码语言:javascript
复制
@Configuration
@EnableSwagger2
@Profile("dev")
public class SwaggerConfig {                                    

测试环境启动命令如下

代码语言:javascript
复制
java -jar boot-web-2.1.2.RELEASE.jar -Dspring.profiles.active=dev
  • 限制部分接口
代码语言:javascript
复制
@Bean
public Docket api() {
    return new Docket(DocumentationType.SWAGGER_2)
            .select()
            .apis(RequestHandlerSelectors.basePackage("com.tenmao.mvc.controller"))
            .paths(PathSelectors.ant("/api/*"))
            .build();
}
  • 自定义文档信息
代码语言:javascript
复制
@Bean
public Docket api() {
    return new Docket(DocumentationType.SWAGGER_2)
            .select()
            .apis(RequestHandlerSelectors.basePackage("com.tenmao.mvc.controller"))
            .paths(PathSelectors.ant("/api/*"))
            .build()
            // 这里有一个调用
            .apiInfo(apiInfo());
}

private ApiInfo apiInfo() {
    return new ApiInfo(
            "十毛API",
            "十毛API文档",
            "1.0",
            "Terms of service",
            new Contact("tenmao", "https://www.jianshu.com/u/518bde83a9bc", "tenmao@example.com"),
            "License of API", "API license URL", Collections.emptyList());
}

SpringBoot入门系列

参考

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019.01.31 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 两步接入
  • 展示
  • 其他配置
  • SpringBoot入门系列
  • 参考
相关产品与服务
云数据库 Redis
腾讯云数据库 Redis(TencentDB for Redis)是腾讯云打造的兼容 Redis 协议的缓存和存储服务。丰富的数据结构能帮助您完成不同类型的业务场景开发。支持主从热备,提供自动容灾切换、数据备份、故障迁移、实例监控、在线扩容、数据回档等全套的数据库服务。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档