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文 ...
随机推荐
- HDU1254:推箱子(bfs+dfs)
传送门 题意 给出一副图 0.空地1.墙2.箱子3.目的地4.人所在的位置 问最少几步能将箱子推到目的地 分析 这道题难度略大(菜鸡),首先用vis[bx][by][mx][my]记录当箱子(bx,b ...
- 记一次线上环境的内存溢出(java.lang.OutOfMemoryError)
事故背景 今天客户说风控项目有个别用户查询不到数据不是报错就是一直卡在那里,我就去那个接口看了下. 一看项目日志今天的都几个g了,平常也就几百兆吧,很明显出了问题. 请求接口后使用命令tail -f ...
- windows API普通函数跟回调函数有何区别
通俗点讲:1.普通函数(假设我们都是函数)你卖电脑,我买电脑,我给你钱(调用你)后,你给我电脑(得到返回值).这种情况下,我给钱后就不能走开,必须等你把电脑给我,否则你交货的时候可能找不到人.2.回调 ...
- Selenium定位多个iframe嵌套中的元素
在公司boss系统中,经常会遇到多层iframe嵌套的情况,导致无法定位最里面那层iframe的元素. 其实很简单,只要一层层定位iframe,定位到你想要的那层iframe即可: 如果操作完需要返回 ...
- Qt事件系统之三:键盘事件
QKeyEvent类用来描述一个键盘事件.当键盘按键被按下或者被释放时,键盘事件便会被发送给拥有键盘输人焦点的部件. QKeyEvent的key()函数可以获取具体的按键,对于Qt中给定的所有按键,可 ...
- hdu 1044 Collect More Jewels
题意: 一个n*m的迷宫,在t时刻后就会坍塌,问:在逃出来的前提下,能带出来多少价值的宝藏. 其中: ’*‘:代表墙壁: '.':代表道路: '@':代表起始位置: '<':代表出口: 'A'~ ...
- Bryce1010的linux课程设计
1.设计目的 2.软件环境 3.要求 4.需求分析 5.总体设计 6.详细设计 7.调试与测试 8.总结 思路整理: 1.如果要开始编译着手的准备 SQLite数据库的安装 gtk+的安装 (.... ...
- Windows环境下修改Oracle实例监听IP地址
Windows环境下修改Oracle实例监听IP地址. 配置文件路径:<ORACLE_HOME>\NETWORK\ADMIN 如:C:\Oracle11gR2\product\11.2.0 ...
- Optimizing Downloads for Efficient Network Access
Optimizing Downloads for Efficient Network Access Previous Next 1.This lesson teaches you to Unders ...
- 1475 建设国家 DP
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1475 这题转化过来就是,给定n个点,每个点都有一个过期时间,一个价值.现 ...