参考地址

 

说明

以下配置是基于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. 环境配置 | 安装Jupyter Notebook及jupyter_contrib_nbextensions库实现代码自动补全

    一.Jupyter Notebook的安装与启动 安装Jupyter Notebook pip3 install jupyter 启动 jupyter notebook 输入命令后会自动弹出浏览器窗口 ...

  2. Vue中v-show和v-if的使用以及区别

    个人博客 地址:http://www.wenhaofan.com/article/20190321143330 v-if 1.v-if 根据条件渲染,它会确保在切换过程中条件块内的组件销毁和重建    ...

  3. MFC使用LoadBitmap方法加载位图文件失败解决方案(转)

    用如下方法在原项目中使用LoadBitmap方法加载已有的位图资源作为背景没有问题,但放在别的项目中总是加载不出来,该函数返回NULL HBITMAP hBitmap=LoadBitmap((HINS ...

  4. zabbix4.4.2安装部署

    1.下载阿里的源,执行 wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo 2 ...

  5. python笔记20(面向对象课程二)

    今日内容 类成员 成员修饰符 内容回顾 & 补充 三大特性 封装 函数封装到类 数据封装到对象 * class Foo: def __init__(self,name,age): self.n ...

  6. Ant风格表达式

    1. ?:匹配任意一个字符 *   :匹配0或者任意数量的字符 **  :匹配0或更多的目录

  7. 2019-08-07 纪中NOIP模拟B组

    T1 [JZOJ1385] 直角三角形 题目描述 二维平面坐标系中有N个位置不同的点. 从N个点选择3个点,问有多少选法使得这3个点形成直角三角形. 数据范围 $3 \leq N \leq 1500$ ...

  8. bzoj 1483

    Description N个布丁摆成一行,进行M次操作.每次将某个颜色的布丁全部变成另一种颜色的,然后再询问当前一共有多少段颜色.例如颜色分别为1,2,2,1的四个布丁一共有3段颜色. Input 第 ...

  9. Application Server was not connected before run configuration stop, reason: Unable to ping server at localhost:1099

    方法:把catalina.bat 文件中set JAVA_OPTS= -Xmx1024M -Xms512M -XX:MaxPermSize=256m这行去掉,具体看下面两篇博客 https://blo ...

  10. linq和隐式类型var

    隐式类型 var 强类型,声明的时候必须给变量赋值,编译器会根据值来确定其类型.只能出现在局部变量或脚本代码中. 使用范围: 简单类型:如int string等 复杂类型:如数组.类等 逻辑语句:fo ...