1.swagger,可以这么理解swagger是接口规范。Rest Api 传递参数的除了get请求外,put post,需要传递json。或者就是直接都通过传递json到后台

这里主要介绍一下springboot后台整合swagger的使用步骤。如果要查看swagger(OpenApi)的规范,可以参考git的官方文件规范。

OpenAPI 3.0规范

springboot 整合swagger的简单基本使用。

第一步:

在pom.xml文件中引入依赖:

        <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>

  

第二步:

添加swagger的配置类

@Configuration
@EnableSwagger2
@ComponentScan(basePackages = { "com.xxx.controller" })//扫描的包路径
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
.paths(PathSelectors.any()).build();
} private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("用户登录")//接口标题
.description("用户登录接口")//接口描述
.version("v1.0")//版本号
.contact(new Contact("name", "url", "email"))//联系人信息
.build();
}
}

 

如果没有添加@ComponentScan(basePackages={})扫描的包路径。也可以通过一下方式实现添加多个扫描包

@Configuration
@EnableSwagger2
public class SwaggerConfiguration { private static final String SPLITOR = ","; //重写basePackage()支持多包扫描
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(basePackage("cn.com.agree.aweb.controller.gateway" + SPLITOR
+ "cn.com.agree.aweb.controller.cluster" + SPLITOR
+ "cn.com.agree.aweb.controller.flow" + SPLITOR
+ "cn.com.agree.aweb.controller.fuse" + SPLITOR
+ "cn.com.agree.aweb.controller.conversion" + SPLITOR
+ "cn.com.agree.aweb.controller.signature" + SPLITOR
+ "cn.com.agree.aweb.controller.api"))
.paths(PathSelectors.any())
.build(); } private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("网关管控接口")
// .description("更多请关注http://www.baidu.com")
// .termsOfServiceUrl("http://www.baidu.com")
// .contact("sunf")
.version("v1.1")
.build();
}
}

  

第三步则是在Controller类里加入swagger注解,这样对应的接口可以在项目启动后通过url路径(http://localhost:8080/swagger-ui.html)访问到。

@ApiOperation(value = "用户登录",tags = {"用户管理"})
@ApiImplicitParams({ @ApiImplicitParam(name = "userName", value = "用户名", paramType = "body",required=true),
@ApiImplicitParam(name = "password", value = "密码", paramType = "body",required=true) })
@RequestMapping(value = "/login", method = RequestMethod.POST)
public RestResult<String> apiMethod(
@Valid @RequestBody LoginRequestDTO loginRequestDTO, Errors errors,
HttpServletRequest request) throws Exception {
//业务处理
return null;
}

  

请求参数在路径上的注解PathVariable使用,示例如下:

 @ApiOperation("根据id刪除后端服务")
@DeleteMapping("/v1.1/{id}")
@ApiImplicitParam(name = "id", value = "后端服务id", paramType = "path", dataType = "string", required = true)
@OperationLog(name = "删除后端服务")
public Object deleteServerById(@PathVariable("id") String id, @RequestParam("api_id") String apiId) {
return apiServiceService.deleteServerById(id, apiId);
}

 

以上三步骤就是简单的swagger基本使用步骤。

访问swaggerUi后,页面如下:点开对应的接口,可以直接在浏览器进行测试接口是否可用。

swagger 以及swaggerUI使用的步骤的更多相关文章

  1. 【swagger】 swagger-ui的升级版swagger-bootstrap-ui

    swagger-bootstrap-ui是基于swagger-ui做了一些优化拓展: swagger-ui的界面: swagger-bootstrap-ui界面: 相比于原生的swagger-ui , ...

  2. Springboot集成Swagger操作步骤

    特别提示:本人博客部分有参考网络其他博客,但均是本人亲手编写过并验证通过.如发现博客有错误,请及时提出以免误导其他人,谢谢!欢迎转载,但记得标明文章出处:http://www.cnblogs.com/ ...

  3. 使用swagger实现web api在线接口文档

    一.前言 通常我们的项目会包含许多对外的接口,这些接口都需要文档化,标准的接口描述文档需要描述接口的地址.参数.返回值.备注等等:像我们以前的做法是写在word/excel,通常是按模块划分,例如一个 ...

  4. Spring Boot 项目实战(三)集成 Swagger 及 JavaMelody

    一.前言 上篇介绍了 Logback 的集成过程,总体已经达到了基本可用的项目结构.本篇主要介绍两个常用工具,接口文档工具 Swagger .项目监控工具 JavaMelody 的集成步骤. 二.Sw ...

  5. 使用swagger实现web api在线接口文档(转载)

    一.前言 通常我们的项目会包含许多对外的接口,这些接口都需要文档化,标准的接口描述文档需要描述接口的地址.参数.返回值.备注等等:像我们以前的做法是写在word/excel,通常是按模块划分,例如一个 ...

  6. Spring MVC中使用Swagger生成API文档和完整项目示例Demo,swagger-server-api(二十)

    一:Swagger介绍 Swagger是当前最好用的Restful API文档生成的开源项目,通过swagger-spring项目 实现了与SpingMVC框架的无缝集成功能,方便生成spring r ...

  7. Spring MVC中使用Swagger生成API文档和完整项目示例Demo,swagger-server-api

    本文作者:小雷FansUnion-一个有创业和投资经验的资深程序员-全球最大中文IT社区CSDN知名博主-排名第119   实际项目中非常需要写文档,提高Java服务端和Web前端以及移动端的对接效率 ...

  8. 最新版Swagger 3升级指南和新功能体验!

    Swagger 3.0 发布已经有一段时间了,它于 2020.7 月 发布,但目前市面上使用的主流版本还是 Swagger 2.X 版本和少量的 1.X 版本,然而作为一名合格的程序员怎么能不折腾新技 ...

  9. springboot集成swagger实战(基础版)

    1. 前言说明 本文主要介绍springboot整合swagger的全过程,从开始的swagger到Knife4j的进阶之路:Knife4j是swagger-bootstarp-ui的升级版,包括一些 ...

随机推荐

  1. Halcon系列(1) 菜鸟入门

    官方网站怎么使用HDevelop  :https://www.mvtec.com/products/halcon/halcon-tour

  2. python将当前时间加上7天

    datetime.datetime.now() + datetime.timedelta(days = 7)).strftime("%Y-%m-%d"

  3. Oracle不同版本中序列的注意点

    <span style="font-size:14px;">create table manager ( userid NUMBER(10), username VAR ...

  4. 三十二、http与www服务介绍

    一.用户访问百度(www.baidu.com) 用户访问在url中输入地址后,首先会访问本地的缓存和hosts文件,如果没有,会访问本地DNS,在就是根域和顶级域名等,在前面已经说过了,这里不再赘述. ...

  5. java通过jdbc插入中文到mysql显示乱码(问号或者乱码)

    对于很多初学者来说,中文字符编码不相同的问题,是一个很烦躁的问题!! 因为很多时候,我们并不知道,到底是哪一层出现了问题? 在这里稍微做个总结~也怕自己今后忘了!! 其实也就三层: 1.前端页面 2. ...

  6. AttributeError: 'bytes' object has no attribute 'hex'

    python3.5之前bytes数据没有hex()属性 需要使用 ''.join(map(lambda x:('' if len(hex(x))>=4 else '/x0')+hex(x)[2: ...

  7. MySQL数据库简单操作

    title date tags layout MySQL简单操作 2018-07-16 Linux post 登录mysql mysql -h 主机名 -u 用户名 -p 查看所有数据库 show d ...

  8. 《C Prime Plus》第十节笔记

    数组和指针 10.1 数组 10.1.1 初始化数组 标量变量:只储存单个值的变量 创建只读数组,应该用const声明和初始化数组 const int days[] = {1,2,3,5}; 省略方括 ...

  9. [LC] 125. Valid Palindrome

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  10. [2015普及组-D]推销员 思维que

    题:https://www.cometoj.com/problem/0221 #include<iostream> #include<cstring> #include< ...