段时间,同事分享了一下 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. Redis protocol (redis通信协议)

    以前只会用redis -cli客户端, 或者在java程序里使用jedis来作为介质与redis服务器通信.这两天正好在实习中接触了一点这方面的内容. 五一放假就稍微翻了翻redis相关资料. 本篇博 ...

  2. 第三节:SignalR之PersistentConnection模型详解(步骤、用法、分组、跨域、第三方调用)

    一. 承上声明 在上一个章节里,啰里啰嗦写了一堆关于介绍SignalR的“废话”,从这一篇开始往后正式撸代码,这期间不少人(包括 张善友大哥)建议我直接用.Net Core下的SignalR,关于此简 ...

  3. 第一节:从面向对象思想(oo)开发、接口、抽象类以及二者比较

    一. 面向对象思想 1. 面向过程(OP)和面向对象(OO)的区别: (1):面向过程就是排着用最简单的代码一步一步写下去,没有封装,当业务复杂的时候,改动就很麻烦了 (2):面向对象将复杂的业务分离 ...

  4. spring+springMVC+mybatis简单整合

    spring+springMVC+mybatis简单整合, springMVC框架是spring的子项目,所以框架的整合方式为,spring+Mybatis或springMVC+mybatis. 三大 ...

  5. css和css3弹性盒模型实现元素宽度(高度)自适应

    一.css实现左侧宽度固定右侧宽度自适应 1.定位 <!DOCTYPE html> <html lang="en"> <head> <me ...

  6. 用juery的ajax方法调用aspx.cs页面中的webmethod方法

    首先在 aspx.cs文件里建一个公开的静态方法,然后加上WebMethod属性. 如: [WebMethod] public static string GetUserName() { //.... ...

  7. kerbose常用操作

    1.查看有那些用户认证 kadmin.local -q "list_principals" 2.用keytab文件进行认证 kinit -kt /root/keytab/hive. ...

  8. Linux 之 rsyslog+mysql+LogAnalyzer 日志收集系统

     作者:邓聪聪 LogAnalyzer 是一个 syslog 和其他网络事件数据的 Web 前端工具,提供简单易用的日志浏览.搜索和基本分析以及图表显示 由于公司部分项目需求使用日志记录系统,随笔记录 ...

  9. codeforces 893F - Physical Education Lessons 动态开点线段树合并

    https://codeforces.com/contest/893/problem/F 题意: 给一个有根树, 多次查询,每次查询对于$x$i点的子树中,距离$x$小于等于$k$的所有点中权值最小的 ...

  10. 【原创】大数据基础之Mesos(1)简介、安装、使用

    Mesos 1.7.1 官方:http://mesos.apache.org/ 一 简介 Program against your datacenter like it’s a single pool ...