SpringBoot中部署Swagger2和Swagger-UI
1 Gradle配置
在dependencies中添加以下依赖:
implementation("io.springfox:springfox-swagger2:2.7.0")
implementation("io.springfox:springfox-swagger-ui:2.7.0")
具体的版本可以在https://mvnrepository.com/artifact/io.springfox中查看到
2 添加Swagger2配置类
package com.learning.test; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
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; @Configuration
@EnableWebMvc
@EnableSwagger2
public class Swagger2Configuration implements WebMvcConfigurer { //swagger2的配置文件,这里可以配置swagger2的一些基本的内容,比如扫描的包等等
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.learning.test.controller"))
.paths(PathSelectors.any())
.build();
} // 构建api文档的详细信息
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
// 页面标题
.title("API接口说明")
// 创建人
.contact(new Contact("lasdaybg", "", ""))
// 版本号
.version("")
// 描述
.description("")
.build();
} @Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/"); registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
3 Controller示例
package com.learning.test.controller; import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController; import com.learning.test.model.MyObject; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation; @RestController
@RequestMapping(value = "/test")
@Api("测试接口")
public class TestController {
@RequestMapping(method = RequestMethod.GET)
@ApiOperation(value = "查询对象")
@ApiImplicitParams({
@ApiImplicitParam(name = "param1", value = "入参1"),
@ApiImplicitParam(name = "param2", value = "入参2")})
public MyObject get(@RequestParam(required = false) String param1,
@RequestParam(required = false) String param2) {
return new MyObject();
} @RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
@ApiOperation(value = "创建对象")
public void create(@RequestBody(required = false) MyObject myObject) {}
} MyObject的类声明如下:
package com.learning.test.model; import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data; @Data
@ApiModel(value = "MyObject", description = "数据模型")
public class MyObject { @ApiModelProperty(value = "名称")
private String name; @ApiModelProperty(value = "参数1")
private Integer param1; @ApiModelProperty(value = "参数2")
private Boolean param2;
}
这里用到了几类注解:
@Api
用在类上,说明这个是Swagger的资源
@ApiOperation
用在方法上,对方法功能做一个说明
@ApiImplicitParams,ApiImplicitParam
用在方法上,对方法的入参进行说明
@ApiModel
用在模型对象上,对对象的属性等进行说明
另外,这里用到了lombok,具体用法请自行百度。
4 查看swagger文档
启动程序,在浏览器中输入http://localhost:8080/swagger-ui.html,即可看到swagger的文档说明
SpringBoot中部署Swagger2和Swagger-UI的更多相关文章
- 在springboot中使用swagger2
1.在springboot中使用swagger的话,首先在pom文件中引入依赖 <!-- https://mvnrepository.com/artifact/io.springfox/spri ...
- SSM项目 以及 springboot 中引入swagger2的方法
swagger2是一个非常好用的接口文档,在开发的过程中方便前后端接口的交接. 下面我们就来讲讲在使用java时,分别在SSM框架,以及springboot+mybatis框架中引入swagger2的 ...
- SpringBoot中集成Swagger2
1.依赖jar <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-s ...
- SpringBoot 中使用 Swagger2 出现 whitelabel page error 解决方法
今天使用Swagger最新版,在pom.xml引入 <dependency> <groupId>io.springfox</groupId> <artifac ...
- 如何让接口文档自动生成,SpringBoot中Swagger的使用
目录 一.在SpringBoot项目中配置Swagger2 1.pom.xml中对Swagger2的依赖 2.编写配置类启用Swagger 3.配置实体类的文档 4.配置接口的文档 5.访问文档 二. ...
- ASP.NET Core 在 Swagger UI 中显示自定义的 Header Token
Swagger 是个好东西,对于前后端分离的网站来说,不仅是提高前后端开发人员沟通效率的利器,也大大方便了后端人员测试 API.有时候,API 中可能需要在 Header 中设置认证参数,比如 aut ...
- 在Abp中集成Swagger UI功能
在Abp中集成Swagger UI功能 1.安装Swashbuckle.Core包 通过NuGet将Swashbuckle.Core包安装到WebApi项目(或Web项目)中. 2.为WebApi方法 ...
- SpringBoot应用部署到Tomcat中无法启动问题
SpringBoot应用部署到Tomcat中无法启动问题 背景 最近公司在做一些内部的小型Web应用时, 为了提高开发效率决定使用SpringBoot, 这货自带Servlet容器, 你在开发We ...
- SpringBoot中使用springfox+swagger2书写API文档
随着前后端的分离,借口文档变的尤其重要,springfox是通过注解的形式自动生成API文档,利用它,可以很方便的书写restful API,swagger主要用于展示springfox生成的API文 ...
随机推荐
- UVa 11440 Help Tomisu (数论欧拉函数)
题意:给一个 n,m,统计 2 和 n!之间有多少个整数x,使得x的所有素因子都大于M. 析:首先我们能知道的是 所有素数因子都大于 m 造价于 和m!互质,然后能得到 gcd(k mod m!, m ...
- python 面向对象二 类和实例
一.类和实例 面向对象最重要的概念就是类(Class)和实例(Instance),必须牢记类是抽象的模板,比如Student类,而实例是根据类创建出来的一个个具体的“对象”,每个对象都拥有相同的方法, ...
- SGU 176 Flow construction【有上下界最小流】
正好考到了所以翻一些题来做--猛然发现搞了半个月的网络流却没做两道上下界(不过这种题好像是比较少233) 首先建立超级源汇ss,tt,没限制的边照常连,对于有限制的边(u,v,mn,mx),连接(u, ...
- poj 2396 Budget【有上下界的网络流】
第一步:建立无源汇有上下界的网络模型 每行 i 作为一个点并连边(s, i, Ri, Ri),每列 j 作为一个点并连边(j, t, Cj, Cj),设 Uij, Lij 分别表示第 i 行第 j 列 ...
- P2093 [国家集训队]JZPFAR(KDTree)
传送门 类似于p4357 不过因为距离相等的时候要优先选择序号小的,所以要重载一下运算符 //minamoto #include<bits/stdc++.h> #define R regi ...
- iOS 切割圆角图片、图片文件格式判断
1.切割圆角图片 // 性能不好,适合圆角图形数量比较少的情况 UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMak ...
- JD商家后台管理的细节
1: 宝贝主图和滚动图都是800px,只有刚好这么多时才能得到显示,否则不会显示. 2:宝贝描述图只支持750px, 只有这么多时才能得到显示, 刚开始不知道, 上传图片上去后, 发现始终无法显示, ...
- ORACLE 如何查看存储过程的定义
ORACLE 如何查看存储过程的定义 相关的数据字典 USER_SOURCE 用户的存储过程.函数的源代码字典 DBA_SOURCE 整个系统所有用户的存储过程.函数的源代码字典 ALL_SOUR ...
- [书目20150303]软件工程的本质:运用SEMAT内核
译者序Robert Martin作序Bertrand Meyer作序Richard Soley作序前言致谢第一部分 内核思想解释第1章 简要介绍如何使用内核1.1 为什么开发优秀软件具有很 ...
- Oracle 的备份和恢复
Oracle数据库有三种标准的备份方法,它们分别是导出/导入(EXP/IMP).热备份和冷备 份.导出备件是一种逻辑备份,冷备份和热备份是物理备份. 一. 导出/导入(Export/Import) 利 ...