参考地址

 

说明

以下配置是基于spring-boot项目。
 

注解

- @Api()用于类;
  表示标识这个类是swagger的资源
 
- @ApiOperation()用于方法;
  表示一个http请求的操作
 
- @ApiParam()用于方法,参数,字段说明;
  表示对参数的添加元数据(说明或是否必填等)
 
- @ApiModel()用于类
  表示对类进行说明,用于参数用实体类接收
 
- @ApiModelProperty()用于方法,字段
  表示对model属性的说明或者数据操作更改
 
- @ApiIgnore()用于类,方法,方法参数
  表示这个方法或者类被忽略
 
- @ApiImplicitParam() 用于方法
  表示单独的请求参数
 
- @ApiImplicitParams() 用于方法
  包含多个 @ApiImplicitParam
 

实践

@Api() - 用于类;表示标识这个类是swagger的资源
  tags–表示说明
  value–也是说明,可以使用tags替代
  但是tags如果有多个值,会生成多个list
  1. @Api(value="用户controller",tags={"用户操作接口"})
  2. @RestController
  3. public class UserController {
  4.  
  5. }
@ApiOperation() - 用于方法;表示一个http请求的操作
  value用于方法描述
  notes用于提示内容
  tags可以重新分组(视情况而用)
 
@ApiParam() - 用于方法,参数,字段说明;表示对参数的添加元数据(说明或是否必填等)
  name–参数名
  value–参数说明
  required–是否必填
  1. @Api(value="UserController",tags={"用户接口"})
  2. @RestController
  3. public class UserController {
  4. @ApiOperation(value="获取用户信息",tags={"获取用户信息"},notes="注意")
  5. @GetMapping("/getUserInfo")
  6. public User getUserInfo(@ApiParam(name="id",value="用户id",required=true) Long id,@ApiParam(name="username",value="用户名") String username) {
  7. User user = userService.getUserInfo();
  8.  
  9. return user;
  10. }
  11. }
 
@ApiModel() - 用于类 ;表示对类进行说明,用于参数用实体类接收
  value–表示对象名
  description–描述
   
@ApiModelProperty() - 用于方法,字段; 表示对model属性的说明或者数据操作更改
  value–字段说明
  name–重写属性名字
  dataType–重写属性类型
  required–是否必填
  example–举例说明
  hidden–隐藏
  1. @ApiModel(value="user",description="用户对象")
  2. @Data
  3. public class User implements Serializable{
  4. private static final long serialVersionUID = 1L;
  5. @ApiModelProperty(value="用户名",name="username",example="xingguo")
  6. private String username;
  7. @ApiModelProperty(value="状态",name="state",required=true)
  8. private Integer state;
  9. private String password;
  10. private String nickName;
  11. private Integer isDeleted;
  12.  
  13. @ApiModelProperty(value="ids",hidden=true)
  14. private String[] ids;
  15. private List<String> idList;
  16. }
  1. @ApiOperation("修改用户信息")
  2. @PostMapping("/updateUserInfo")
  3. public int updateUserInfo(@RequestBody @ApiParam(name="用户对象",value="json格式",required=true) User user){
  4. int num = userService.updateUserInfo(user);
  5. return num;
  6. }
@ApiIgnore() - 用于类或者方法上,可以不被swagger显示在页面上。
 
@ApiImplicitParam() - 用于方法,表示单独的请求参数
@ApiImplicitParams() - 用于方法,包含多个 @ApiImplicitParam
  name–参数ming
  value–参数说明
  dataType–数据类型
  paramType–参数类型
  example–举例说明
  1. @ApiOperation("查询测试")
  2. @GetMapping("select")
  3. //@ApiImplicitParam(name="name",value="用户名",dataType="String", paramType = "query")
  4. @ApiImplicitParams({
  5. @ApiImplicitParam(name="name",value="用户名",dataType="string", paramType = "query",example="xingguo"),
  6. @ApiImplicitParam(name="id",value="用户id",dataType="long", paramType = "query")})
  7. public void select(){
  8.  
  9. }

pom依赖

  1. <dependency>
  2. <groupId>io.springfox</groupId>
  3. <artifactId>springfox-swagger2</artifactId>
  4. <version>2.9.2</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>io.springfox</groupId>
  8. <artifactId>springfox-swagger-ui</artifactId>
  9. <version>2.9.2</version>
  10. </dependency>
  11. <!-- 下面这个界面更好看,更好用-->
  12. <dependency>
  13. <groupId>com.github.xiaoymin</groupId>
  14. <artifactId>swagger-bootstrap-ui</artifactId>
  15. <version>1.9.5</version>
  16. </dependency>

具体配置

(包含分组)
  1. import com.google.common.base.Predicates;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.Configuration;
  4. import springfox.documentation.builders.ApiInfoBuilder;
  5. import springfox.documentation.builders.ParameterBuilder;
  6. import springfox.documentation.builders.PathSelectors;
  7. import springfox.documentation.builders.RequestHandlerSelectors;
  8. import springfox.documentation.schema.ModelRef;
  9. import springfox.documentation.service.ApiInfo;
  10. import springfox.documentation.service.Contact;
  11. import springfox.documentation.service.Parameter;
  12. import springfox.documentation.spi.DocumentationType;
  13. import springfox.documentation.spring.web.plugins.Docket;
  14. import springfox.documentation.swagger2.annotations.EnableSwagger2;
  15.  
  16. import java.util.ArrayList;
  17. import java.util.List;
  18.  
  19. /**
  20. * swagger-api 配置
  21. *
  22. * @author wzm
  23. * @version 1.0.0
  24. * @date 2019/6/15
  25. **/
  26. @Configuration
  27. @EnableSwagger2
  28. @EnableSwaggerBootstrapUI
  29. public class Swagger2 {
  30.  
  31. /**
  32. * http://localhost:8085/fabric-net/swagger-ui.html
  33. * http://localhost:8085/fabric-net/doc.html
  34. */
  35.  
  36. private static final String SWAGGER_SCAN_BUSINESS_PACKAGE = "com.thyc.fabric.controller.business";
  37. private static final String BUSINESS_VERSION = "1.0.0";
  38.  
  39. private static final String SWAGGER_SCAN_FABRIC_PACKAGE = "com.thyc.fabric.controller.fabric";
  40. private static final String FABRIC_VERSION = "1.0.0";
  41.  
  42. @Bean
  43. public Docket createBusinessApi() {
  44. List<Parameter> pars = new ArrayList<>();
  45. ParameterBuilder ticketPar1 = new ParameterBuilder();
  46. ticketPar1.name("Authorization").description("登录令牌")
  47. .modelRef(new ModelRef("string")).parameterType("header")
  48. .required(false).build();
  49. pars.add(ticketPar1.build());
  50. return new Docket(DocumentationType.SWAGGER_2)
  51. .globalOperationParameters(pars)
  52. //分组名不支持中文
  53. .groupName("business")
  54. .apiInfo(apiBusinessInfo())
  55. .pathMapping("/")
  56. .select()
  57. // 对所有api进行监控
  58. .apis(RequestHandlerSelectors.basePackage(SWAGGER_SCAN_BUSINESS_PACKAGE))
  59. // 错误路径不监控
  60. .paths(Predicates.not(PathSelectors.regex("/error.*")))
  61. // 对根下所有路径进行监控
  62. .paths(PathSelectors.regex("/.*"))
  63. .build();
  64. }
  65.  
  66. private ApiInfo apiBusinessInfo() {
  67. Contact contact = new Contact("thyc","thyc.com","thyc@email");
  68. return new ApiInfoBuilder()
  69. //设置文档的标题
  70. .title("Business")
  71. //设置文档的描述->1.Overview
  72. .description("业务模块数据管理")
  73. //设置文档的版本信息-> 1.1 Version information
  74. .termsOfServiceUrl("http://localhost:8085/fabric-net")
  75. .contact(contact)
  76. .version(BUSINESS_VERSION)
  77. .build();
  78. }
  79.  
  80. //------------------------------------------------------------------------------------------------------------------
  81.  
  82. @Bean
  83. public Docket createFabricApi() {
  84. List<Parameter> pars = new ArrayList<Parameter>();
  85. ParameterBuilder ticketPar1 = new ParameterBuilder();
  86. ticketPar1.name("Authorization").description("登录令牌")
  87. .modelRef(new ModelRef("string")).parameterType("header")
  88. .required(false).build();
  89. pars.add(ticketPar1.build());
  90. return new Docket(DocumentationType.SWAGGER_2)
  91. .globalOperationParameters(pars)
  92. //分组名不支持中文
  93. .groupName("fabric")
  94. .apiInfo(apiFabricInfo())
  95. .pathMapping("/")
  96. .select()
  97. // 对所有api进行监控
  98. .apis(RequestHandlerSelectors.basePackage(SWAGGER_SCAN_FABRIC_PACKAGE))
  99. // 错误路径不监控
  100. .paths(Predicates.not(PathSelectors.regex("/error.*")))
  101. // 对根下所有路径进行监控
  102. .paths(PathSelectors.regex("/.*"))
  103. .build();
  104. }
  105.  
  106. private ApiInfo apiFabricInfo() {
  107. Contact contact = new Contact("thyc","thyc.com","thyc@email");
  108. return new ApiInfoBuilder()
  109. //设置文档的标题
  110. .title("Fabric-Network")
  111. //设置文档的描述->1.Overview
  112. .description("超级账本网络信息管理")
  113. //设置文档的版本信息-> 1.1 Version information
  114. .termsOfServiceUrl("http://localhost:8085/fabric-net")
  115. .contact(contact)
  116. .version(FABRIC_VERSION)
  117. .build();
  118. }
  119.  
  120. }

Swagger-ui接口文档的更多相关文章

  1. TP框架整合Swagger UI接口文档

    1.下载swagger ui:http://swagger.io/swagger-ui/: 2.在应用目录里新建一个目录xxx:如图 3.解压后把dist目录的所有文件拷贝到新建的目录里面: 4.在新 ...

  2. asp.net core 使用 swagger 生成接口文档

    参考地址:http://www.cnblogs.com/daxnet/p/6181366.html http://www.jianshu.com/p/fa5a9b76f3ed 微软参考文档:https ...

  3. .net core 使用 swagger 生成接口文档

    微软参考文档:https://docs.microsoft.com/en-us/aspnet/core/tutorials/web-api-help-pages-using-swagger?tabs= ...

  4. webapi 利用webapiHelp和swagger生成接口文档

    webapi 利用webapiHelp和swagger生成接口文档.均依赖xml(需允许项目生成注释xml) webapiHelp:微软技术自带,仅含有模块.方法.请求-相应参数的注释. swagge ...

  5. Spring Boot 集成 Swagger 构建接口文档

    在应用开发过程中经常需要对其他应用或者客户端提供 RESTful API 接口,尤其是在版本快速迭代的开发过程中,修改接口的同时还需要同步修改对应的接口文档,这使我们总是做着重复的工作,并且如果忘记修 ...

  6. .net WebApi使用swagger 美化接口文档

    本文将一步步演示如何用swagger美化WebApi接口文档,为接口文档添加接口名称说明,为请求参数和返回数据结构字段含义添加注释说明 一.为WebApi项目安装Swagger 首先我们新建一个Web ...

  7. spring-boot-route(五)整合Swagger生成接口文档

    目前,大多数公司都采用了前后端分离的开发模式,为了解决前后端人员的沟通问题,后端人员在开发接口的时候会选择使用swagger2来生成对应的接口文档,swagger2提供了强大的页面调试功能,这样可以有 ...

  8. 用Swagger生成接口文档

    Swagger简介 在系统设计的时候,各个应用之间往往是通过接口进行交互的.因此接口的定义在整个团队中就变得尤为重要.我们可以把接口的规范用接口描述语言进行描述,然后Swagger可以根据我们定义的接 ...

  9. asp.net core使用Swashbuckle.AspNetCore(swagger)生成接口文档

    asp.net core中使用Swashbuckle.AspNetCore(swagger)生成接口文档 Swashbuckle.AspNetCore:swagger的asp.net core实现 项 ...

  10. 基于swagger进行接口文档的编写

    0. 前言 近期忙于和各个银行的代收接口联调,根据遇到的问题,对之前编写的接口进行了修改,需求收集和设计接口时想到了方方面面,生产环境下还是会遇到意想不到的问题,好在基本的执行逻辑已确定,因此只是对接 ...

随机推荐

  1. 【笔记0-开篇】面试官系统精讲Java源码及大厂真题

    背景 开始阅读 Java 源码的契机,还是在第一年换工作的时候,被大厂的技术面虐的体无完肤,后来总结大厂的面试套路,发现很喜欢问 Java 底层实现,即 Java 源码,于是我花了半年时间,啃下了 J ...

  2. hyper-v虚拟机不能访问外网的解决方案

    直接说解决方案,将虚拟机的一张网卡改为旧版网络适配器即可.具体原因还不可知. 延申一下,一般应该使用的交换机,是“外部”类型即可.

  3. windows系统使用sketch设计的设计稿

    由sketch设计的文件因为没有windows系统的sketch所以无法使用windows系统的电脑打开,那么怎么办呢?可以借助zeplin, 通过sketch(mac电脑)上传到zeplin账号,然 ...

  4. fastadmin弹窗效果表单

    在项目所对应的js文件中的 table.bootstrapTable({ url: $.fn.bootstrapTable.defaults.extend.index_url, pk: 'id', s ...

  5. Hdu2099 整除的尾数

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2099 Problem Description 一个整数,只知道前几位,不知道末二位,被另一个整数除尽了 ...

  6. code码说明

    https://www.cnblogs.com/wanglaowu/p/6229843.html

  7. MyBatis的手动映射与模糊查询

    一.手动映射 当实体类属性与数据库字段名不同时,无法自动映射,导致查询出空值,这时候可以使用手动映射 在select节点添加resultMap属性与resultMap节点建立关系

  8. C++——动态内存分配1

    9.动态内存分配  new 类型名T(初值列表) 其功能是在程序执行期间申请用于存放T类型对象的内存空间,并依初值列表赋以初值,结果值:成功则T类型的指针,指向新分配的内存:失败则为0(null).若 ...

  9. linux分区命令parted的用法

    parted的适用场景 创建操作大于2T的分区 一般情况下,我们都是选择使用fdisk工具来进行分区,但是目前在实际生产环境中使用的磁盘空间越来越大,呈TiB级别增长:而常用的fdisk这个工具对分区 ...

  10. 3ds Max File Format (Part 2: The first inner structures; DllDirectory, ClassDirectory3)

    Now that we understand the outer structure of the file, it's time to look closer to what's inside. T ...