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. tomcat在windows下安装

    1.下载地址:https://tomcat.apache.org/download-90.cgi Binary是编译好的,可以直接使用的版本: tar.gz,解压即可用: Source是源代码版本,需 ...

  2. training set, validation set, test set的区别

    training set: 用来训练模型 validation set : 用来做model selection test set : 用来评估所选出来的model的实际性能 我们知道,在做模型训练之 ...

  3. C\C++中strcat()函数

    转载:https://blog.csdn.net/smf0504/article/details/52055971 C\C++中strcat()函数                           ...

  4. matlab中fspecial Create predefined 2-D filter以及中值滤波均值滤波以及高斯滤波

    来源: 1.https://ww2.mathworks.cn/help/images/ref/fspecial.html?searchHighlight=fspecial&s_tid=doc_ ...

  5. 刷LeetCode的简易姿势

    近期抽空刷了刷LeetCode,算是补补课. 由于不是很习惯直接在网页上Coding&Debug,所以还是在本地环境下进行编码调试,觉得基本OK后再在网页上提交. 主要采用Python3进行提 ...

  6. 转C++了

    积极响应"某王"的号召,联赛之后转C辣!(好吧,其实是它拿着一把西瓜刀顶在我背后逼我转的)

  7. Xnip Mac上方便好用的截图工具

    Xnip Mac上方便好用的截图工具 标注 Xnip 拥有齐全的标注功能,您可以对截取的图片进行标注,在标注的同时还能重新调整截图大小. 查看标注操作 GIF 滚动截图 Xnip 的滚动截图功能可以让 ...

  8. 笔记本电脑为什么有时候不用按FN+F1~12也可以控制音量、亮度全部等等

    对于经常要使用F1~F12的用户就很烦,比如编写前端代码的时候想直接按F12检查代码就是不行. 如何取消快捷键? 问题原因: 1.电脑默认使用了快捷键. 2.电脑按了FN+ESC锁定,只限于戴尔的电脑 ...

  9. kafka配置文件详解

    kafka的配置分为 broker.producter.consumer三个不同的配置 一 .BROKER 的全局配置最为核心的三个配置 broker.id.log.dir.zookeeper.con ...

  10. 解决:npm install ERR! Unexpected end of JSON input

    npm ERR! Unexpected end of JSON input npm i -g npm@5 npm install --registry=https://registry.npm.tao ...