Swagger是什么?

Swagger 是一个规范且完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。
  Swagger 的目标是对 REST API 定义一个标准且和语言无关的接口,可以让人和计算机拥有无须访问源码、文档或网络流量监测就可以发现和理解服务的能力。当通过Swagger 进行正确定义,用户可以理解远程服务并使用最少实现逻辑与远程服务进行交互。与为底层编程所实现的接口类似,Swagger 消除了调用服务时可能会有的猜测。

Swagger 的优势

  1. 支持 API 自动生成同步的在线文档:使用 Swagger 后可以直接通过代码生成文档,不再需要自己手动编写接口文档了,对程序员来说非常方便,可以节约写文档的时间去学习新技术。

  2.提供 Web 页面在线测试 API:光有文档还不够,Swagger 生成的文档还支持在线测试。参数和格式都定好了,直接在界面上输入参数对应的值即可在线测试接

在SpringBoot中集成Swagger是目前的项目主流,这里就展示一个Demo.

使用教程

1. 使用IDEA创建一个SpringBoot项目

2. 添加依赖(这些依赖中不仅仅是Swagger的依赖,还有一些常用的依赖,一并列出)

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency> <!--Swagger 依赖开始-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.10.0</version>
</dependency>
<!--Swagger 依赖结束-->
</dependencies>

3. 创建SwaggerConfig.java(Swagger的配置类)

@Configuration  //必须存在
@EnableSwagger2 // 必须存在
// 必须存在 扫描的API Controller包
@ComponentScan(basePackages = {"com.yhl.test.swagger.controller" })
public class SwaggerConfig {
@Bean
public Docket customDocket(){
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo());
}
public ApiInfo apiInfo(){
Contact contact = new Contact("YHL","http://www.ly058.cn/","email");
return new ApiInfoBuilder()
.title("测试Swagger API")
.description("API接口")
.contact(contact)
.version("1.0.1")
.build();
}
}

4.创建测试的entity类(@Data是lombok的标签,在这里替代getter和setter方法)

@Data
public class User {
private String username;
private String password; public User(String username, String password){
this.username = username;
this.password = password;
}
}

5. 创建测试的controller类(为了方便,没有连接数据库,使用list替代)

@RestController
@Api(value = "用户模块", description = "用户接口信息")
public class UserController {
//模拟数据库
public static List<User> users = new ArrayList<User>(); static {
users.add(new User("张三", "123456"));
users.add(new User("李四", "123456"));
} //获取用户列表
@ApiOperation(value = "获取用户列表", notes = "获取所有用户的列表")
@GetMapping("/users")
public Object users() {
Map<String, Object> map = new HashMap<>();
map.put("users", users);
return map;
} @ApiOperation(value = "获取单个用户", notes = "根据ID查询某个用户的信息")
@ApiImplicitParam(value = "用户ID", paramType = "path")
@GetMapping("/user/{id}")
public User getUserById(@PathVariable("id") int id) {
return users.get(id);
} @ApiOperation(value = "添加用户", notes = "根据传入的用户信息添加用户")
@ApiImplicitParam(value = "用户对象", paramType = "query")
@PostMapping("/user")
public Object addUser(User user) {
return users.add(user); } @ApiOperation(value = "删除用户", notes = "根据传入的用户ID删除用户")
@ApiImplicitParam(value = "用户ID",paramType = "path")
@DeleteMapping("/user/{id}")
public Object delete(@PathVariable("id") int id) {
return users.remove(id);
} }

6. 结果,访问http://localhost:8080/swagger-ui.html

SpringBoot + Swagger Demo的更多相关文章

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

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

  2. Swagger详解(SpringBoot+Swagger集成)(转)

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/ai_miracle/article/de ...

  3. 【转】Swagger详解(SpringBoot+Swagger集成)

    Swagger-API文档接口引擎Swagger是什么 Swagger是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 Web 服务.总体目标是使客户端和文件系统作为服务器 ...

  4. SpringBoot+Swagger整合API

    SpringBoot+Swagger整合API Swagger:整合规范的api,有界面的操作,测试 1.在pom.xml加入swagger依赖 <!--整合Swagger2配置类--> ...

  5. activeMQ的spring、springboot的DEMO

    一.activeMQ实现spring的demo 1:pom.xml文件 <dependencies> <dependency> <groupId>junit< ...

  6. springboot+swagger集成

    一.swagger介绍 Swagger 是一款RESTFUL接口的文档在线自动生成+功能测试功能软件.本文简单介绍了在项目中集成swagger的方法和一些常见问题.如果想深入分析项目源码,了解更多内容 ...

  7. SpringBoot 入门 Demo

    SpringBoot   入门 Demo Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从 ...

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

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

  9. 第四章 springboot + swagger(转载)

    此篇博客转发自:http://www.cnblogs.com/java-zhao/p/5348113.html swagger用于定义API文档. 好处: 前后端分离开发 API文档非常明确 测试的时 ...

随机推荐

  1. PHP代码审计学习(1)

    全局变量与超全局变量 $GLOBALS $GLOBALS 是PHP的一个超级全局变量组,在一个PHP脚本的全部作用域中都可以访问,$GLOBALS 是一个包含了全部变量的全局组合数组.变量的名字就是数 ...

  2. Spring及tomcat初始化日志

    Tomcat StandardContext初始化过程 //org.apache.catalina.core.StandardContext#startInternal // 子容器启动 for (C ...

  3. linux_基础调优

    1. 配置授时服务,使用阿里云的授时服务 echo -e "# update time\n*/5 * * * * /usr/sbin/ntpdate time1.aliyun.com &am ...

  4. c#数据处理总结(分组、交并差与递归)

    前言:最近项目比较忙,完全没有时间写下总结笔记,今天抽出时间来写下笔记,供写后台的你来做数据处理后台代码编写的参考. 一.分组 var GroupForList = numberList.GroupB ...

  5. MeteoInfoLab脚本示例:计算温度平流

    需要温度和风场U/V分量格点数据,计算中主要用到cdiff函数,结果用GrADS验证一致.脚本程序: print 'Open data files...' f_air = addfile('D:/Te ...

  6. 这里有40条提升编程技能小妙招!还有TIOBE 7月份的编程语言排行榜

    如何提高编程技能?恐怕很多开发者思考过这个问题.最近,拥有将近 15 年开发经验的软件工程师 Kesk -*- 写了一篇博客,列举了 40 条对其职业生涯有所帮助的事项.   或许,通过以下 40 个 ...

  7. Vagrant系列(一)----win10搭建Vagrant+VirtualBox环境_

      一.Vagrant是什么?     vagrant是一个操作虚拟机的工具.是一个基于Ruby的工具,用于创建和部署虚拟化开发环境.    通过命令和配置文件来管理虚拟机,很快就能完成一套开发环境的 ...

  8. spring boot:多模块项目生成jar包(spring boot 2.3.3)

    一,多模块项目的优点: 1,为什么要使用多模块项目? 相比传统的单体工程,使用Maven的多模块配置, 有如下优点: 帮助项目划分模块,鼓励重用, 防止POM变得过于庞大, 方便某个模块的构建,而不用 ...

  9. centos8上添加sudoer用户

    一,检查服务器是否已安装sudo的rpm包? 1,查询rpm包列表 [root@yjweb ~]# rpm -qa | grep sudo libsss_sudo-2.0.0-43.el8_0.3.x ...

  10. centos8平台使用pstree查看进程树

    一,pstree用途 Linux pstree命令将所有行程以树状图显示,树状图将会以 pid (如果有指定) 或是以 systemd 这个基本行程为根 (root) 说明:centos6及更旧版本为 ...