段时间,同事分享了一下 swagger-ui,于是自己尝试了一下。大致的使用过程这里记录一下:

1.添加依赖

<!--swagger-ui-->
<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获取的包,第二是官方给出的一个ui界面

2.启用注解或者使用配置类的方式

  • 注解方式:启动类上加 @EnableSwagger2 注解和 @ComponentScan(basePackages = {"com.**"})  ,basePackages 取值 是你要扫描的具体的包名
  • 配置类方式 。下面只是提供其中一种方式:
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import springfox.documentation.builders.ApiInfoBuilder;
    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;
    
    @Configuration
    @EnableSwagger2
    public class SwaggerConfig {
      // 这里是用来配置是否启用swagger 的。
    @Value("${swagger.enable: true}")
    private boolean enableSwagger; public static final String SWAGGER_SCAN_BASE_PACKAGE = "com.test.web.controller";
    public static final String VERSION = "1.0.0"; ApiInfo apiInfo() {
    return new ApiInfoBuilder()
    .title("Swagger API")
    .description("This is to show api description")
    .contact(new Contact("rh", "www.baidu.com", "ruhui@keking.cn"))
    .license("Apache 2.0")
    .licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html")
    .termsOfServiceUrl("")
    .version(VERSION)
    .build();
    } @Bean
    public Docket customImplementation(){
    return new Docket(DocumentationType.SWAGGER_2)
    .select()
    .apis(RequestHandlerSelectors.basePackage(SWAGGER_SCAN_BASE_PACKAGE))
    .build()
    .apiInfo(apiInfo());
    }
    }

3.使用相关注解对项目里的方法或者类进行标识

@Api:用在类上,说明该类的作用
@ApiOperation:用在方法上,说明方法的作用
@ApiImplicitParams:用在方法上包含一组参数说明
@ApiImplicitParam:用在@ApiImplicitParams注解中,指定一个请求参数的各个方面
paramType:参数放在哪个地方
header-->请求参数的获取:@RequestHeader
query-->请求参数的获取:@RequestParam
path(用于restful接口)-->请求参数的获取:@PathVariable
body(不常用)
form(不常用)
name:参数名
dataType:参数类型
required:参数是否必须传
value:参数的意思
defaultValue:参数的默认值
@ApiResponses:用于表示一组响应
@ApiResponse:用在@ApiResponses中,一般用于表达一个错误的响应信息
code:数字,例如400
message:信息,例如"请求参数没填好"
response:抛出异常的类
@ApiModel:描述一个Model的信息(这种一般用在post创建的时候,使用@RequestBody这样的场景,请求参数无法使用@ApiImplicitParam注解进行描述的时候)
@ApiModelProperty:描述一个model的属性 注解有很多,下面列举一个在方法上使用的示例:
  @ApiOperation(value = "根据Id获取用户信息", httpMethod = "GET")
@ApiImplicitParam(paramType = "query", name = "id", required = true, value = "用户id", defaultValue = "1")
@RequestMapping(value = "/get", method = {RequestMethod.GET})
public User userList(Integer id){
List<User> userList = new ArrayList<>();
for (int i=1; i<= 5; i++){
User user = new User(i, "用户" + i);
userList.add(user);
}
return userList.get(id -1);
}

  

4.启动项目,打开地址 http://服务名:端口/swagger-ui.html#/. 如我的服务启动后打开地址:http://localhost:8080/swagger-ui.html#/

5.配置不同环境是否启用  swagger

  1. 请看第2步中:使用配置类方式。对 enable 属性进行赋值就行

     @Bean
    public Docket customImplementation(){
    return new Docket(DocumentationType.SWAGGER_2)
    .select()
    .apis(RequestHandlerSelectors.basePackage(SWAGGER_SCAN_BASE_PACKAGE))
    .build()
    .apiInfo(apiInfo());
    }

      

  2. 通过 profile 方式。 在  SwaggerConfig  类上使用 Profile 注解,指定 profile 为 dev、uat,然后配置文件中指定 spring.profiles.active = dev 或者 uat。这个时候就启用了。
@Configuration
@EnableSwagger2
@Profile({"dev","uat"}) // 这里可以用来配置哪个环境启用swagger -- 对应属性spring.profiles.active
public class SwaggerConfig {
}
 

springboot 使用 swagger2的更多相关文章

  1. SpringBoot(七):SpringBoot整合Swagger2

    原文地址:https://blog.csdn.net/saytime/article/details/74937664 手写Api文档的几个痛点: 文档需要更新的时候,需要再次发送一份给前端,也就是文 ...

  2. springboot新增swagger2配置

    转自http://www.cnblogs.com/jtlgb/p/8532433.html SpringBoot整合Swagger2 相信各位在公司写API文档数量应该不少,当然如果你还处在自己一个人 ...

  3. SpringBoot之Swagger2

    SpringBoot利用Swagger2只需配置少量的注解信息便能方便地构建强大的API文档. 1.添加maven依赖 2.创建Swagger2配置类 3.在API添加文档内容 4.访问http:// ...

  4. 【转载】SpringBoot系列——Swagger2

    微服务学习二:springboot与swagger2的集成:https://www.cnblogs.com/fengli9998/p/7522973.html 注:需要在启动类加 @EnableSwa ...

  5. SpringBoot使用Swagger2实现Restful API

    很多时候,我们需要创建一个接口项目用来数据调转,其中不包含任何业务逻辑,比如我们公司.这时我们就需要实现一个具有Restful API的接口项目. 本文介绍springboot使用swagger2实现 ...

  6. SpringBoot整合Swagger2

    相信各位在公司写API文档数量应该不少,当然如果你还处在自己一个人开发前后台的年代,当我没说,如今为了前后台更好的对接,还是为了以后交接方便,都有要求写API文档. 手写Api文档的几个痛点: 文档需 ...

  7. 微服务学习二:springboot与swagger2的集成

    现在测试都提倡自动化测试,那我们作为后台的开发人员,也得进步下啊,以前用postman来测试后台接口,那个麻烦啊,一个字母输错就导致测试失败,现在swagger的出现可谓是拯救了这些开发人员,便捷之处 ...

  8. SpringBoot集成Swagger2实现Restful(类型转换错误解决办法)

    1.pom.xml增加依赖包 <dependency> <groupId>io.springfox</groupId> <artifactId>spri ...

  9. springboot与swagger2的集成

    springboot与swagger2的集成 1.出现的背景 随着互联网技术的发展,现在的网站架构基本都由原来的后端渲染变成了:前端渲染.先后端分离的形态,而前端和后端的唯一联系,变成了API接口: ...

  10. SpringBoot与Swagger2整合

    一.Swagger简介与优势 相信各位在公司写API文档数量应该不少,当然如果你还处在自己一个人开发前后台的年代,当我没说,如今为了前后台更好的对接,还为了以后交接方便,都有要求写API文档. Swa ...

随机推荐

  1. “HtmlAgilityPack”已拥有为“System.Net.Http”定义的依赖项的解决方案

    #事故现场 在vs2013上用nuget安装 HtmlAgilityPack -Version 1.8.9时,报错如下: Install-Package : “HtmlAgilityPack”已拥有为 ...

  2. Mac 终端美化方法

    美化终端,主要是主题,字体,命令行提示3个方面. 主题 使用的主题是Solarized Dark主题. 安装主题: git clone git://github.com/altercation/sol ...

  3. BeautifulSoup解析模块

    简介: Beautiful Soup 是一个可以从HTML或XML文件中提取数据的Python库.它能够通过你喜欢的转换器实现惯用的文档导航,查找,修改文档的方式. 使用 from bs4 impor ...

  4. 第十五节:深入理解async和await的作用及各种适用场景和用法

    一. 同步VS异步 1.   同步 VS 异步 VS 多线程 同步方法:调用时需要等待返回结果,才可以继续往下执行业务 异步方法:调用时无须等待返回结果,可以继续往下执行业务 开启新线程:在主线程之外 ...

  5. free命令查看内存信息

    free介绍 FREE(1) Linux User’s Manual FREE(1) NAME free - Display amount of free and used memory in the ...

  6. [物理学与PDEs]第4章习题4 一维理想反应流体力学方程组的守恒律形式及其 R.H. 条件

    写出在忽略粘性与热传导性, 即设 $\mu=\mu'=\kappa=0$ 的情况, 在 Euler 坐标系下具守恒律形式的一维反应流动力学方程组. 由此求出在解的强间断线上应满足的 R.H. 条件 ( ...

  7. Vue Material

    Material Design是什么? https://www.zhihu.com/topic/20005114/top-answers 我们挑战自我,为用户创造了崭新的视觉设计语言.与此同时,新的设 ...

  8. CF1139E Maximize Mex

    题目地址:CF1139E Maximize Mex 这其实是一个二分图匹配匈牙利算法的巧妙运用 考虑倒序回答 则由原来的删除改为添加 把 potential 值作为左部,则一共有编号为 \(0~m\) ...

  9. SQL Server2016安装

    VS2017已经发布10多天了,这几天正好要重新做系统.所以想着把SQL Server和VS都做一次升级.VS2017只需要下载一个安装包就可以进行在线安装.但是SQL Server2016安装时会碰 ...

  10. 【原创】大叔经验分享(16)Context namespace element 'component-scan' and its parser class [org.springframework.context.annotation.ComponentScanBeanDefinitionParser] are only available on JDK 1.5 and higher

    今天尝试运行一个古老的工程,配置好之后编译通过,结果运行时报错: org.springframework.beans.factory.BeanDefinitionStoreException: Une ...