参考地址

 

说明

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

注解

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

实践

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

pom依赖

<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>
<!-- 下面这个界面更好看,更好用-->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.9.5</version>
</dependency>

具体配置

(包含分组)
import com.google.common.base.Predicates;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2; import java.util.ArrayList;
import java.util.List; /**
* swagger-api 配置
*
* @author wzm
* @version 1.0.0
* @date 2019/6/15
**/
@Configuration
@EnableSwagger2
@EnableSwaggerBootstrapUI
public class Swagger2 { /**
* http://localhost:8085/fabric-net/swagger-ui.html
* http://localhost:8085/fabric-net/doc.html
*/ private static final String SWAGGER_SCAN_BUSINESS_PACKAGE = "com.thyc.fabric.controller.business";
private static final String BUSINESS_VERSION = "1.0.0"; private static final String SWAGGER_SCAN_FABRIC_PACKAGE = "com.thyc.fabric.controller.fabric";
private static final String FABRIC_VERSION = "1.0.0"; @Bean
public Docket createBusinessApi() {
List<Parameter> pars = new ArrayList<>();
ParameterBuilder ticketPar1 = new ParameterBuilder();
ticketPar1.name("Authorization").description("登录令牌")
.modelRef(new ModelRef("string")).parameterType("header")
.required(false).build();
pars.add(ticketPar1.build());
return new Docket(DocumentationType.SWAGGER_2)
.globalOperationParameters(pars)
//分组名不支持中文
.groupName("business")
.apiInfo(apiBusinessInfo())
.pathMapping("/")
.select()
// 对所有api进行监控
.apis(RequestHandlerSelectors.basePackage(SWAGGER_SCAN_BUSINESS_PACKAGE))
// 错误路径不监控
.paths(Predicates.not(PathSelectors.regex("/error.*")))
// 对根下所有路径进行监控
.paths(PathSelectors.regex("/.*"))
.build();
} private ApiInfo apiBusinessInfo() {
Contact contact = new Contact("thyc","thyc.com","thyc@email");
return new ApiInfoBuilder()
//设置文档的标题
.title("Business")
//设置文档的描述->1.Overview
.description("业务模块数据管理")
//设置文档的版本信息-> 1.1 Version information
.termsOfServiceUrl("http://localhost:8085/fabric-net")
.contact(contact)
.version(BUSINESS_VERSION)
.build();
} //------------------------------------------------------------------------------------------------------------------ @Bean
public Docket createFabricApi() {
List<Parameter> pars = new ArrayList<Parameter>();
ParameterBuilder ticketPar1 = new ParameterBuilder();
ticketPar1.name("Authorization").description("登录令牌")
.modelRef(new ModelRef("string")).parameterType("header")
.required(false).build();
pars.add(ticketPar1.build());
return new Docket(DocumentationType.SWAGGER_2)
.globalOperationParameters(pars)
//分组名不支持中文
.groupName("fabric")
.apiInfo(apiFabricInfo())
.pathMapping("/")
.select()
// 对所有api进行监控
.apis(RequestHandlerSelectors.basePackage(SWAGGER_SCAN_FABRIC_PACKAGE))
// 错误路径不监控
.paths(Predicates.not(PathSelectors.regex("/error.*")))
// 对根下所有路径进行监控
.paths(PathSelectors.regex("/.*"))
.build();
} private ApiInfo apiFabricInfo() {
Contact contact = new Contact("thyc","thyc.com","thyc@email");
return new ApiInfoBuilder()
//设置文档的标题
.title("Fabric-Network")
//设置文档的描述->1.Overview
.description("超级账本网络信息管理")
//设置文档的版本信息-> 1.1 Version information
.termsOfServiceUrl("http://localhost:8085/fabric-net")
.contact(contact)
.version(FABRIC_VERSION)
.build();
} }

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. Android开发 文件读写openFileOutput与openFileInput

    package com.example.androidtest; import java.io.ByteArrayOutputStream; import java.io.FileInputStrea ...

  2. 小白月赛22 A : 操作序列

    A:操作序列 析题得说: 考察点 : 模拟,STL库容器的使用 坑点 : 区间不要搞丢东西 难点 : 这个题比较变态的是我们不知道每次输入每行是一个数还是两个数,就需要进行判断, 怎么判断呢?用 sc ...

  3. ansi sql 语法 切换为 oracle 语法

        语句粘贴到 工作表 打开查询构建器 勾选 创建oracle连接 over     sql dev 的语法设置调整,否则表别名会右对齐   下面是 转换后的结果,是不是看得舒服多了

  4. Ignatius and the Princess IV HDU - 1029 基础dp

    #include<iostream> #include<cstring> #include<cmath> #include<cstdio> #inclu ...

  5. Python-Django学习笔记(三)-Model模型的编写以及Oracle数据库的配置

    Django使用的 MTV 设计模式(Models.Templates.Views) 因此本节将围绕这三部分并按照这个顺序来创建第一个页面 模型层models.py 模型是数据唯一而且准确的信息来源. ...

  6. Oracle 数据库,远程访问 ora-12541:TNS:无监听程序

    1.修改网络连接IPV4设置为固定IP IP地址:192.168.100.8子网掩码:255.255.255.0默认网关:192.168.100.1首选DNS:192.168.100.1 2.修改.. ...

  7. PP: Sequence to sequence learning with neural networks

    From google institution; 1. Before this, DNN cannot be used to map sequences to sequences. In this p ...

  8. BK: How to read a book 第四篇

    第四篇 阅读的最终目标 第二十章 阅读的第四个层次:主题阅读 在做主题阅读时的要求: 1. 知道:知道应该读哪些书. 在面对如此庞大的相关资料时,我们要如何决定我们要研究的主题是什么呢. 如何判断属于 ...

  9. MongoDB的安装问题

    Mongo的安装与启动: npm install mongodb -g MongoDB高性能.开源.无模式的文档型数据库,它基于分布式文件存储.介于关系数据库和非关系数据库之间的一种产品.其最大特点: ...

  10. layui table 超出自动换行

    个人博客 地址:http://www.wenhaofan.com/article/20181120180507 layui 的table的的cell默认是超出hidden的,如果希望超出长度自动换行便 ...