参考地址

 

说明

以下配置是基于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. 0级搭建类001-RedHat Enterprise Linux 8 安装(RHEL 8) 公开

    项目文档引子系列是根据项目原型,制作的测试实验文档,目的是为了提升项目过程中的实际动手能力,打造精品文档AskScuti. 项目文档引子系列目前不对外发布,仅作为博客记录.如学员在实际工作过程中需提前 ...

  2. 修改url中参数值

    1.一种方法function changeUrlArg(url, arg, val){ var pattern = arg+'=([^&]*)'; var replaceText = arg+ ...

  3. create_function()代码注入

    00x00create_function()函数的简介 适用范围:PHP 4> = 4.0.1,PHP 5,PHP 7 功能:根据传递的参数创建匿名函数,并为其返回唯一名称. 语法: creat ...

  4. AOPS论坛上100+100个积分

    100+10 rare and irresistible integrals I bring you many beautiful integrals that I have collected ov ...

  5. UI自动化测试的Page Object模式

    在UI级的自动化测试框架中,当页面样式改变或者页面元素属性改变,那么代码也要随之进行修改,如何做到高效快速的修改代码来适应这些改变呢,这个时候可以引入Page Object模式,也是页面对象设计模式. ...

  6. 我的翻译--GSMem:通过GSM频率从被物理隔离的计算机上窃取数据

    抽象概念 AG网络是指在物理上与公共互联网断开的网络.虽然近几年人们验证了入侵这类网络系统的可行性,但是从这种网络上获取数据仍然是一个有挑战的任务.在本文中,我们介绍GSMem,它是一个可以在蜂窝数据 ...

  7. SMTS Silicon Design Engineer Location: Beijing, Beijing, CN

    https://jobs.amd.com/job/Beijing-Physical-Design-Engineer-Beij/603603700/?locale=en_US What you do a ...

  8. Codeforces 1295E. Permutation Separation (线段树)

    https://codeforces.com/contest/1295/problem/E 建一颗线段树,叶子结点是花费从1到i所需要花费的前缀和,表示前i个元素全部移动到右边的花费,再维护区间最小值 ...

  9. 洛谷P2158 [SDOI2008]仪仗队 欧拉函数的应用

    https://www.luogu.org/problem/P2158 #include<bits/stdc++.h> #define int long long using namesp ...

  10. Hadoop启动HDFS时DataNode未启动

    在用$HADOOP_HOME/sbin/start-dfs.sh启动HDFS时发现只有NameNode和SecondaryNameNode启动,没有DataNode. 查看logs下的DataNode ...