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文 ...
随机推荐
- Ruby 数式匹配器
str = "x^2 + 12317 +X^2 - Length" str = " x ^ 2 + y ...
- Luogu P1134 阶乘问题 【数学/乱搞】 By cellur925
输入输出格式 输入格式: 仅一行包含一个正整数 NN . 输出格式: 一个整数,表示最右边的非零位的值. 输入输出样例 输入样例#1: 12 输出样例#1: 6 说明 USACO Training S ...
- bzoj1303[CQOI2008]中位数图 / 乱搞
题目描述 给出1~n的一个排列,统计该排列有多少个长度为奇数的连续子序列的中位数是b.中位数是指把所有元素从小到大排列后,位于中间的数. 输入输出格式 输入格式: 第一行为两个正整数n和b,第二行为1 ...
- C++默认函数与函数重载
一.默认参数 在C++中,可以为参数指定默认值.在函数调用时没有指定与形参相对应的实参时, 就自动使用默认参数. 默认参数的语法与使用:(1)在函数声明或定义时,直接对参数赋值.这就是默认参数: (2 ...
- DFS Codeforces Round #306 (Div. 2) B. Preparing Olympiad
题目传送门 /* DFS: 排序后一个一个出发往后找,找到>r为止,比赛写了return : */ #include <cstdio> #include <iostream&g ...
- FileStream和BinaryReader,BinaryWriter,StreamReader,StreamWriter的区别
FileStream对于在文件系统上读取和写入文件非常有用,FileStream缓存输入和输出,以获得更好的性能.FileStream对象表示在磁盘或网络路径上指向文件的流.这个类提供了在文件中读写字 ...
- Spirng MVC +Velocity 表单绑定命令对象
通常,表单中的数据在提交之后可以通过Spring MVC的@RequestParam注解在控制器函数的参数列表中中提取出来,但是一旦表单数据过多的话,参数列表将会变得非常长,最好的解决方案是将表单中的 ...
- [转]C#Linq中的Union All/Union/Intersect和Top/Bottom和Paging和SqlMethods,skip,take,takewhile,skipwhile,编译查询等
本文转自:http://www.cnblogs.com/suizhikuo/p/3791799.html 我们继续讲解LINQ to SQL语句,这篇我们来讨论Union All/Union/Inte ...
- 调用wsdl接口,参数是xml格式
1.最近太累了,好困.闲话少许直奔主题吧.上代码 try{ String wsurl = "http://172.16.16.236:9999/xxx/ws/WSService?wsdl&q ...
- Mysql函数、语句
一:日期函数: 日期函数: SELECT CURDATE(); # 2018-07-07 SELECT CURTIME(); # 11:28:24 SELECT NOW(); # 2018-07-07 ...