Swagger 介绍

Swagger UI 允许任何人(无论是开发团队还是最终用户)都可以可视化 API 资源并与之交互,而无需任何实现逻辑。

Swagger API 文档是根据 OpenAPI(以前称为 Swagger)规范自动生成的,可简化后端实现和客户端的使用。

Swagger 依赖

        <dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-bean-validators</artifactId>
<version>2.8.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>1.5.21</version>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-models</artifactId>
<version>1.5.21</version>
</dependency>

SpringBoot 集成 Swagger 配置类

package com.example.apitestplatform.config;

import com.google.common.collect.Lists;
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.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2; /**
* Swagger 文档配置类
*/
@Configuration
@EnableSwagger2
public class SwaggerConfig { @Bean
public Docket docket() {
ParameterBuilder builder=new ParameterBuilder();
builder.parameterType("header")
.name("token")
.description("token值")
.required(true)
.modelRef(new ModelRef("string")); // 在swagger文档里展示header return new Docket(DocumentationType.SWAGGER_2)
.groupName("ApiDemo")
.apiInfo(apiInfo())
.globalOperationParameters(Lists.newArrayList(builder.build()))
.select() // 选择生成策略
.apis(RequestHandlerSelectors.basePackage("com.example.apitestplatform.controller")) // 选择生成文档的类(忽略该行则不做过滤)
.paths(PathSelectors.any())
.build();
} // 定义接口文档基本信息
private ApiInfo apiInfo(){
return new ApiInfoBuilder()
.title("ApiDemo 系统") // 接口文档标题
.description("ApiDemo 接口文档") // 接口文档描述
.contact(new Contact("xiaoming", "", "103@qq.com")) // 作者联系方式
.version("1.0") // 接口文档版本
.build();
}
}

常用注解

  • @Api(tags="API 类标题")
  • @ApiOperation(value="API 方法标题")
  • @ApiModel(value="实体类标题", description="实体类描述")
  • @ApiModelProperty(value="实体属性描述",example="属性示例取值", required=true)

示例:

  • 接口类:
package com.example.apitestplatform.controller;

import com.example.apitestplatform.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*; @Api(tags="Swagger Demo 类")
@RestController
@RequestMapping(value="demo") // 类中所有接口地址的前缀
public class DemoController { @Value("${user.hobby}")
private String hobby; @ApiOperation(value="Swagger Demo get方法")
// @RequestMapping(value="loginGet", method= RequestMethod.GET)
@GetMapping("loginGet")
public String loginGet() {
return hobby;
} // @RequestMapping(value="loginPost", method= RequestMethod.POST)
@PostMapping("loginPost") // 简便写法
public String loginPost(@RequestBody User user) { // 如果没用 @RequestBody,则获取结果为 null
System.out.println("username : "+user.getUsername());
System.out.println("password : "+user.getPassword());
return "登录成功:"+user.getUsername();
} // 访问:http://localhost:8080/demo/userId/1/2
// @RequestMapping(value="userId/{userId}/{id}", method=RequestMethod.GET)
@GetMapping("getUser/{userid}/{id}")
public String loginUser1(@PathVariable("userid") Integer userid, @PathVariable("id") Integer id) {
System.out.println("userid : "+userid);
System.out.println("id : "+id);
return "userid: "+userid+" id: "+id;
} // 访问:http://localhost:8080/demo/getUser?userid=1&id=2
// 访问:http://localhost:8080/demo/getUser?user=1&id=2,则 userid 值为 null
@GetMapping("getUser")
public String loginUser2(@RequestParam(value="userid", required=false) Integer userid, // required=false:参数非必须传
@RequestParam("id") Integer id) {
System.out.println("userid : "+userid);
System.out.println("id : "+id);
return "userid: "+userid+" id: "+id;
}
}
  • User 类
package com.example.apitestplatform.entity;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data; @ApiModel(value="用户类", description="用户信息")
@Data
public class User { @ApiModelProperty(value="用户名", example="xiaoming", required=true)
private String username;
@ApiModelProperty(value="密码", example="123456", required=true)
private String password;
}

效果示例

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

  1. springboot+swagger接口文档企业实践(下)

    目录 1.引言 2. swagger接口过滤 2.1 按包过滤(package) 2.2 按类注解过滤 2.3 按方法注解过滤 2.4 按分组过滤 2.4.1 定义注解ApiVersion 2.4.2 ...

  2. SpringBoot开发mockserver及生成swagger接口文档

    通过springboot开发mock server,包含get及post接口,用于练习接口自动化及jmeter很方便 当然,也为后面jenkins持续集成做基础(开发push代码后  → jenkin ...

  3. .net core的Swagger接口文档使用教程(二):NSwag

    上一篇介绍了Swashbuckle ,地址:.net core的Swagger接口文档使用教程(一):Swashbuckle 讲的东西还挺多,怎奈微软还推荐了一个NSwag,那就继续写吧! 但是和Sw ...

  4. .Net Core---- WebApi生成Swagger接口文档

    1. Swagger是什么? Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 Web 服务.总体目标是使客户端和文件系统作为服务器以同样的速度来更新.文件 ...

  5. REST-framework快速构建API--生成Swagger接口文档

    一.Swagger概述 1.引言 当接口开发完成,紧接着需要编写接口文档.传统的接口文档使用Word编写,or一些接口文档管理平台进行编写,但此类接口文档维护更新比较麻烦,每次接口有变更,需要手动修改 ...

  6. swagger接口文档

    1 在Visual Studio 中创建一个Asp.NET  WebApi 项目,项目名:Com.App.SysApi(本例创建的是 .net 4.5 框架程序) 2  打开Nuget 包管理软件,查 ...

  7. springboot+swagger接口文档企业实践(上)

    目录 1.引言 2.swagger简介 2.1 swagger 介绍 2.2 springfox.swagger与springboot 3. 使用springboot+swagger构建接口文档 3. ...

  8. Swagger 接口文档规范

    导语: 相信无论是前端还是后端开发,都或多或少地被接口文档折磨过.前端经常抱怨后端给的接口文档与实际情况不一致.后端又觉得编写及维护接口文档会耗费不少精力,经常来不及更新.其实无论是前端调用后端,还是 ...

  9. .netcore 3.1高性能微服务架构:加入swagger接口文档

    本文为原创文章:首发:http://www.zyiz.net/tech/detail-108663.html swagger是什么? Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视 ...

  10. Core3.0使用Swagger接口文档

    前言 此方法为百度搜索结果,原文链接找不到了 步骤 1.引用Nuget Swashbuckle.AspNetCore 2.Startup.cs配置 //注册swagger服务,定义1个或者多个swag ...

随机推荐

  1. php curl发送post get请求

    POST: function curl_post_https($url, $data, $header){ // 模拟提交数据函数 $curl = curl_init(); // 启动一个CURL会话 ...

  2. linux .h文件

    转载请注明来源:https://www.cnblogs.com/hookjc/ c++ #include <sys/types.h>   #include <unistd.h> ...

  3. webpack4 处理CSS

    本篇讲解webpack4中打包css的应用.v4 版本和 v3 版本并没有特别的出入. 教程所示图片使用的是 github 仓库图片,网速过慢的朋友请移步>>> 原文地址. 评论或者 ...

  4. C预备知识_001

    程序由什么构成? 1.对数据的描述:在程序中要指定用到哪些数据以及这些数据的类型和数据的组织形式,其实这就是数据结构(data structure). 2.对操作的描述:即要求计算机就行操作的步骤,也 ...

  5. js获取 url?后面的参数取值

    function GetRequest() {     var url = location.search; //获取url中"?"符后的字串     var theRequest ...

  6. 怎么实时同步java虚拟机与操作系统时区 及JVM启动后再更改操作系统时区或时间也能保持其同步? new date() 时差8个小时的解决方案

    第一种(亲测可以) 在代码当中的Application启动类当中加入代码 @PostConstruct void started() { //时区设置:中国上海 //time.zone: " ...

  7. 《Effective Python》笔记——第2章 函数

    一.函数出错的时候抛异常,而不要返回None pass 二.闭包 书里的例子不好,参考https://www.cnblogs.com/Lin-Yi/p/7305364.html 在一个外函数中定义了一 ...

  8. Jest_JavaScript测试框架

    Jest是一个JavaScript测试框架,由Facebook用来测试所有JavaScript代码,包括React应用程序. 不同级别的自动化测试:单元.集成.组件和功能. 单元测试可以看作是和在组件 ...

  9. 框架5--nginx安装部署 上(web服务)

    目录 框架5--nginx安装部署(web服务) 1.练习 2.昨日问题 3.今日内容 4.什么是web服务 5.web服务器软件 6.部署Nginx 7.平滑增加Nginx模块 8.Nginx的命令 ...

  10. Rust语言开发

    Rust开发 第一个程序 fn main() { println!("Hello, world!"); // 带!号的都是宏 并不是函数 } fn main() { let nam ...