SpringBoot整合Swagger2的完整过程记录
前言
springBoot作为微服务首选框架,为其他服务提供大量的接口服务。接口对接方需要实时最近的接口文档。
swagger可以通过代码和注释自动为web项目生成在线文档,这里使用swagger。
当 SpringBoot 代码中 SpringMVC 使用自动化配置类 WebMvcAutoConfiguration 时,其整合 Swagger2 的方法如下。
如果 SpringMVC 的配置过程使用了 WebMvcConfigurationSupport;则如下的整合方法不适合。
一、Spring Boot Web 整合 Swagger2 过程
Spring Boot Web 项目整合 Swagger2 主要有两个过程:
- 添加 Swagger2 相关依赖。
- 配置 Swagger2 配置类。
1.1、添加 Swagger2 相关依赖
首先要对 Spring Boot Web 的项目,添加 Swagger2 相关的依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<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>
1.2、配置 Swagger2 配置类
@Configuration
@EnableSwagger2
public class Swagger {
//创建 Docket 的Bean
@Bean
public Docket docket(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
//select() 函数返回一个 ApiSelectorBuilder实例用来控制哪些接口暴露给 Swagger 来展现
.select()
//要扫描的包
.apis(RequestHandlerSelectors.basePackage("com.example.controller"))
//选择API路径
.paths(PathSelectors.any())
.build();
}
//创建文档的基本信息
public ApiInfo apiInfo(){
return new ApiInfoBuilder()
.title("Swagger UI 的标题")
.description("用restful风格写接口")
.termsOfServiceUrl("")
.version("1.0")
.build();
}
}
二、配置 Swagger2 接口常用注解
2.1、@Api 请求类说明
写在controller类定义上方,用于说明类的作用。
@Api(value = "Swagger Test Control",
description = "演示Swagger用法的Control类",
tags = "Swagger Test Control Tag")
2.2、@ApiOperation 方法的说明
写在REST接口上方,用于说明方法的作用。
@ApiOperation( value="创建用户", notes="根据User对象创建用户")
2.3、@ApiImplicitParams 和 @ApiImplicitParam 方法参数说明
@ApiImplicitParams:用在请求的方法上,包含一组参数说明
@ApiImplicitParam:对单个参数的说明
name:参数名
value:参数的汉字说明、解释
required:参数是否必须传
paramType:参数放在哪个地方
· header --> 请求参数的获取:@RequestHeader
· query --> 请求参数的获取:@RequestParam
· path(用于restful接口)--> 请求参数的获取:@PathVariable
· body(请求体)--> @RequestBody User user
· form(普通表单提交)
dataType:参数类型,默认String,其它值dataType="int"
defaultValue:参数的默认值
--------------------------------------------------------------------
@ApiImplicitParams({
@ApiImplicitParam(name = "id", value = "ID", dataType = "Long"),
@ApiImplicitParam(name = "user", value = "用户", dataType = "User")
})
2.4、@ApiResponses 和 @ApiResponse 方法返回值的说明
@ApiResponses:方法返回对象的说明
@ApiResponse:每个参数的说明
code:数字,例如400
message:信息,例如"请求参数没填好"
response:抛出异常的类
-------------------------------------------------------------------
@ApiResponses({
@ApiResponse(code = 400, message = "权限不足"),
@ApiResponse(code = 500, message = "服务器内部异常") }
)
2.5、@ApiModel 和 @ApiModelProperty
@ApiModel 用于JavaBean 上面,表示一个JavaBean。这种一般用在post创建的时候,使用 @RequestBody 这样的场景,请求参数无法使用 @ApiImplicitParam 注解进行描述的时候。
@ApiModelProperty 用对象接收参数时,描述对象的一个字段。
@ApiModel( description = "学生")
public class Student {
@ApiModelProperty(value = "主键id")
private String id;
@ApiModelProperty(value = "名称", required = true)
private String name;
@ApiModelProperty(value = "年龄", required = true)
private int age;
}
2.6、其他注解
@ApiIgnore :使用该注解忽略这个API,不对这个接口生成文档。
@ApiError:发生错误返回的信息
总结
到此这篇关于SpringBoot整合Swagger2的文章就介绍到这了,更多相关SpringBoot整合Swagger2内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
华为鸿蒙系统应用开发工具 DevEco Studio的安装和使用图文教程
HUAWEI DevEco Studio 是华为消费者业务为开发者提供的集成开发环境(IDE),旨在帮助开发者快捷、方便、高效地使用华为EMUI开放能力。这篇文章主要介绍了华为鸿蒙系统应用开发工具 DevEco Studio的安装和使用图文教程,需要的朋友可以参考下2021-04-04
Java NIO Selector用法详解【含多人聊天室实例】
这篇文章主要介绍了Java NIO Selector用法,结合实例形式分析了Java NIO Selector基本功能、原理与使用方法,并结合了多人聊天室实例加以详细说明,需要的朋友可以参考下2019-11-11
Centos6.5下Jdk+Tomcat+Mysql环境安装图文教程
这篇文章主要为大家详细介绍了Centos6.5系统下Jdk+Tomcat+Mysql环境安装过程,感兴趣的小伙伴们可以参考一下2016-05-05


最新评论