【SpringBoot】Spring Boot 集成SwaggerAPI
Spring Boot 集成SwaggerAPI
学习Spring Boot框架使用Swagger构建RESTful API。
简单记录 - Spring Boot+Spring Cloud+Vue+Element项目实战
Spring Boot作为当前最为流行的Java Web开发脚手架,越来越多的开发者选择用其来构建企业级的RESTFul API接口。
使用Swagger集成文档具有以下几个优势:
功能丰富:支持多种注解,自动生成接口文档界面,支持在界面测试API接口功能。
及时更新:开发过程中花一点写注释的时间,就可以及时地更新API文档,省心省力。
整合简单:通过添加pom依赖和简单配置,内嵌于应用中就可同时发布API接口文档界面,不需要部署独立服务。
官方网站:https://swagger.io/
官方文档:https://swagger.io/docs/
Swagger
那什么是Swagger呢?
Swagger(官网地址:https://swagger.io/)

Swagger是一个规范和完整的框架,用于生成、描述、调用和可视化RESTful风格的Web服务。总体目标是使客户端和文件系统作为服务器,以同样的速度来更新。文件的方法、参数和模型紧密集成到服务器端的代码中,允许API始终保持同步。Swagger让部署管理和使用功能强大的API从未如此简单。
API Developmentfor Everyone
Simplify API development for users, teams, and enterprises with the Swagger open source and professional toolset. Find out how Swagger can help you design and document your APIs at scale.
在Spring Boot中如何使用呢???
在Spring Boot项目中使用Swagger其实很简单,大致分为以下三步
(1)加入Swagger依赖。
(2)加入Swagger文档配置。
(3)使用Swagger注解编写API文档和API实体模板。
创建一个Spring Boot项目 school
添加依赖
在pom文件内添加Maven依赖,这里选择2.9.2版本。
pom.xml
<!-- swagger -->
<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>
添加数据源配置
在src/main/resources下,创建application.yml文件,添加8080端口。
server:
port: 8080
配置类 config
新建config包,并在其下添加Swagger配置类。
SwaggerConfig.java
package com.awen.school.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* Swagger配置
* @author liu Awen
*/
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi(){
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
.apis(RequestHandlerSelectors.any()).paths(PathSelectors.any()).build();
}
private ApiInfo apiInfo(){
return new ApiInfoBuilder().build();
}
}

控制类 controller
新建controller包,并在其下添加HelloController控制类,添加一个hello接口,
HelloController.java
package com.awen.school.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping(value="/hello")
public Object hello() {
return "Hello School Applications!";
}
}
接口测试
启动应用,在浏览器中访问http://localhost:8080/hello,可以看到服务已经调用成功了。

页面测试
在浏览器中访问http://localhost:8080/swagger-ui.html#/,我们就可以看到Swagger的接口文档页面了,还可以选择接口进行测试,如图所示。

单击展开hello接口,单击右侧的try it out→execute,

发现接口成功返回“Hello School Applications!”信息,如图所示。

可以通过Swagger来测试接口了。
常用注解
swagger 通过注解接口生成文档,包括接口名,请求方法,参数,返回信息等
@Api: 修饰整个类,用于controller类上
@ApiOperation: 描述一个接口,用户controller方法上
@ApiParam: 单个参数描述
@ApiModel: 用来对象接收参数,即返回对象
@ApiModelProperty: 对象接收参数时,描述对象的字段
@ApiResponse: Http响应其中的描述,在ApiResonse中
@ApiResponses: Http响应所有的描述,用在
@ApiIgnore: 忽略这个API
@ApiError: 发生错误的返回信息
@ApiImplicitParam: 一个请求参数
@ApiImplicitParam: 多个请求参数
更多说明参考 Swagger 使用手册
资料参考
(1): Spring Boot+Spring Cloud+Vue+Element项目实战:手把手教你开发权限管理系统/徐丽健著.—北京:清华大学出版社,2019
(2):Spring Boot + Spring Cloud 实现权限管理系统 后端篇(六):集成 Swagger API
(3):Spring实战(第5版)作者:[美]克雷格·沃斯译者:张卫滨出版社:人民邮电出版社出版时间:2020-02
(4):Spring Boot 2实战之旅/杨洋著.—北京:清华大学出版社,2019
【SpringBoot】Spring Boot 集成SwaggerAPI的更多相关文章
- SpringBoot系列:Spring Boot集成Spring Cache,使用EhCache
前面的章节,讲解了Spring Boot集成Spring Cache,Spring Cache已经完成了多种Cache的实现,包括EhCache.RedisCache.ConcurrentMapCac ...
- SpringBoot系列:Spring Boot集成Spring Cache,使用RedisCache
前面的章节,讲解了Spring Boot集成Spring Cache,Spring Cache已经完成了多种Cache的实现,包括EhCache.RedisCache.ConcurrentMapCac ...
- SpringBoot(十一): Spring Boot集成Redis
1.在 pom.xml 中配置相关的 jar 依赖: <!-- 加载 spring boot redis 包 --> <dependency> <groupId>o ...
- Spring boot集成swagger2
一.Swagger2是什么? Swagger 是一款RESTFUL接口的文档在线自动生成+功能测试功能软件. Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格 ...
- (转)Spring Boot(十八):使用 Spring Boot 集成 FastDFS
http://www.ityouknow.com/springboot/2018/01/16/spring-boot-fastdfs.html 上篇文章介绍了如何使用 Spring Boot 上传文件 ...
- Spring Boot 集成Shiro和CAS
Spring Boot 集成Shiro和CAS 标签: springshirocas 2016-01-17 23:03 35765人阅读 评论(22) 收藏 举报 分类: Spring(42) 版 ...
- Spring Boot集成MyBatis开发Web项目
1.Maven构建Spring Boot 创建Maven Web工程,引入spring-boot-starter-parent依赖 <project xmlns="http://mav ...
- 玩转Spring Boot 集成Dubbo
玩转Spring Boot 集成Dubbo 使用Spring Boot 与Dubbo集成,这里我之前尝试了使用注解的方式,简单的使用注解注册服务其实是没有问题的,但是当你涉及到使用注解的时候在服务里面 ...
- Spring Boot 集成 GRPC
代码地址如下:http://www.demodashi.com/demo/14110.html 一.背景 Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring ...
随机推荐
- 36个JS特效教程,学完即精通
6个JS特效教程,学完即精通 JavaScript特效教程,学完你就能写任何特效.本课程将JavaScript.BOM.DOM.jQuery和Ajax课程中的各种网页特效提取出了再进行汇总.内容涵 ...
- C# 海量数据瞬间插入到数据库的方法
C# 海量数据瞬间插入到数据库的方法 当我们在数据库中进行大量的数据追加时,是不是经常因为数据量过大而苦恼呢?而所谓的海量数据,一般也是上万级的数据,比如我们要添加一百万条数据,应该如何提高它的效率呢 ...
- IOS UITableView 加载未知宽高图片的解决方案
在开发中遇到了UITableView列表 UITableViewCell装载图片但不知Image的宽高 问题. 在解决该问题的时候,首先想到的是异步加载图片 采用第三方框架SDWebImage 实现对 ...
- 我是如何用go-zero 实现一个中台系统的
最近发现golang社区里出了一个新星的微服务框架,来自好未来,光看这个名字,就很有奔头,之前,也只是玩过go-micro,其实真正的还没有在项目中运用过,只是觉得 微服务,grpc 这些很高大尚,还 ...
- npm的下载与安装
1.Node (1)什么是Node.js Node.js 就是运行在服务端的 JavaScript.Node.js 是一个基于Chrome JavaScript 运行时建立的一个平台. (2)Node ...
- matplotlib的学习6-annotation的标注
import matplotlib.pyplot as plt import numpy as np ''' 当图线中某些特殊地方需要标注时,我们可以使用 annotation. matplotlib ...
- vue element ui 上传 请求接口
在页面上 http-request: 覆盖默认的上传行为,可以自定义上传的实现 <el-upload class="avatar-uploader" action=&qu ...
- 转载:从输入 URL 到页面加载完的过程中都发生了什么事情?
原帖地址:http://www.guokr.com/question/554991/ 1)把URL分割成几个部分:协议.网络地址.资源路径.其中网络地址指示该连接网络上哪一台计算机,可以是域名或者IP ...
- 将XML转换为实体
需求 将XML文件中的数据经过转换后插入到数据库中. 参考 C#实体类和XML的相互转换 https://blog.csdn.net/pan_junbiao/article/details/82938 ...
- Linux中的System V信号量
在进程同步,并发运行时,保证按序地访问共享资源是十分重要的.因此引入了临界区的概念,一次只能有一个线程进入临界区完成他的指令.而信号量(semaphore)的作用,类似于一个交通信号灯,它负责进程协作 ...