15、Spring Boot 2.x 集成 Swagger UI
1.15、Spring Boot 2.x 集成 Swagger UI
- 完整源码: Spring-Boot-Demos
1.15.1 pom文件添加swagger包
<swagger2.version>2.8.0</swagger2.version>
……
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${swagger2.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${swagger2.version}</version>
</dependency>
1.15.2 添加Swagger2Config配置文件
package com.qm.products.prodmp.web.config;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.ArrayList;
import java.util.List;
/**
* @Description swagger ui 配置文件
* @Author hw
* @Date 2018/11/21 11:52
* @Version 1.0
*/
@Configuration
@ConditionalOnProperty(prefix = "swagger", value = {"enable"}, havingValue = "true") // 配置文件配置是否启动swagger配置文件
@EnableSwagger2 // 启用Swagger2
public class Swagger2Config {
@Bean
public Docket createRestApi() {
ParameterBuilder tokenParameter = new ParameterBuilder();
List<Parameter> params = new ArrayList<Parameter>();
params.add(tokenParameter.name("Authorization").description("授权Token").modelRef(new ModelRef("string")).parameterType("header").required(false).build());
return new Docket(DocumentationType.SWAGGER_2)
.globalOperationParameters(params)
.apiInfo(apiInfo())
.select()
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Spring Boot 2.x RESTful APIs")
.version("1.0")
.build();
}
}
1.15.3 Application类加入@EnableSwagger2注解开启swagger
@EnableSwagger2
@SpringBootApplication
public class SpringBootRestfulApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootRestfulApplication.class, args);
}
}
1.15.4 使用。
- 方法
@GetMapping("abnormal-pre")
@ApiImplicitParams({
@ApiImplicitParam(name = "Authorization", value = "token", required = true, dataType = "string", paramType = "header")
})
public BaseRestResponse getAbnormalPre(@RequestParam String startDate, @RequestParam String endDate) {
List<AbnormalPreDTO> abnormalPreDTOS = onlineService.getAbnormalPre(startDate, endDate);
return BaseRestResponse.ok(abnormalPreDTOS);
}
@ApiOperation(value = "模糊搜索本公司用户 用户名/邮箱")
public BaseRestResponse fuzzySearchUsers(@RequestParam String param) {
- class
@RestController
@Api(tags = "Admin权限资源管理")
@RequestMapping("authority")
public class AuthorityController {
1.15.5 UI
访问 : http://localhost:8080/swagger-ui.html,就可以看到效果了。
15、Spring Boot 2.x 集成 Swagger UI的更多相关文章
- Spring boot 多模块项目 + Swagger 让你的API可视化
Spring boot 多模块项目 + Swagger 让你的API可视化 前言 手写 Api 文档的几个痛点: 文档需要更新的时候,需要再次发送一份给前端,也就是文档更新交流不及时. 接口返回结果不 ...
- Spring Boot项目简单上手+swagger配置+项目发布(可能是史上最详细的)
Spring Boot项目简单上手+swagger配置 1.项目实践 项目结构图 项目整体分为四部分:1.source code 2.sql-mapper 3.application.properti ...
- 9、Spring Boot 2.x 集成 Thymeleaf
1.9 Spring Boot 2.x 集成 Thymeleaf 完整源码: Spring-Boot-Demos 1.9.1 在pom中引入依赖 <dependency> <grou ...
- 在Abp中集成Swagger UI功能
在Abp中集成Swagger UI功能 1.安装Swashbuckle.Core包 通过NuGet将Swashbuckle.Core包安装到WebApi项目(或Web项目)中. 2.为WebApi方法 ...
- spring boot / cloud (三) 集成springfox-swagger2构建在线API文档
spring boot / cloud (三) 集成springfox-swagger2构建在线API文档 前言 不能同步更新API文档会有什么问题? 理想情况下,为所开发的服务编写接口文档,能提高与 ...
- Spring Boot HikariCP 一 ——集成多数据源
其实这里介绍的东西主要是参考的另外一篇文章,数据库读写分离的. 参考文章就把链接贴出来,里面有那位的代码,简单明了https://gitee.com/comven/dynamic-datasource ...
- Spring Boot系列——如何集成Log4j2
上篇<Spring Boot系列--日志配置>介绍了Spring Boot如何进行日志配置,日志系统用的是Spring Boot默认的LogBack. 事实上,除了使用默认的LogBack ...
- 【ELK】4.spring boot 2.X集成ES spring-data-ES 进行CRUD操作 完整版+kibana管理ES的index操作
spring boot 2.X集成ES 进行CRUD操作 完整版 内容包括: ============================================================ ...
- 14、Spring Boot 2.x 集成 Druid 数据源
14.Spring Boot 2.x 集成 Druid 数据源 完整源码: Spring-Boot-Demos
随机推荐
- linux下jar命令(运行jar包)
直接用java -jar xxx.jar,当退出或关闭shell时,程序就会停止掉. 普通方式启动应用: java -jar jarPackageName.jar 1>system.log ...
- s5p6818 Overview
S5P6818: 64bit Octa-Core, High Performance, Advanced 3D Graphics, Full-HD Multimedia Video, A53 Core ...
- 面试经典算法:快速排序Golang实现
Golang快速排序 定义 快速排序由C. A. R. Hoare在1962年提出.快速排序是对冒泡排序的一种改进,采用了一种分治的策略. 基本思想 通过一趟排序将要排序的数据分割成独立的两部分,其中 ...
- 01背包变种 第k解问题 hdu 2639
先说说普通01包的状态问题吧 普通的01背包,在状态转移的过程中为了求出最优解,一定是遍历了所有的情况 然后再求的最优解.那么对于第k最优解问题,我们只需要再加一个维度,用来记录每一个状态k优解的状态 ...
- (四)Spring Boot之配置文件-多环境配置
一.Properties多环境配置 1. application.properties配置激活选项 spring.profiles.active=dev 2.添加其他配置文件 3.结果 applica ...
- 【Transact-SQL】让人快遗忘的游标
原文:[Transact-SQL]让人快遗忘的游标 最初学SQL Server的时候,当学到游标的时候,突然有了一种亲切感,因为这种通过一个while循环,一条一条的处理数据的方式,很像学过的过程式语 ...
- Java Web 深入分析(3) CDN
CDN (Content Delivery NetWork) 内容分发网络,它是构筑在现有互联网基础上的一种先进的流量分配网络.区别于镜像,相当于是 CDN = 镜像(mirror) + 缓存(Cac ...
- Python练习_高阶函数_day11
1,写函数,传入n个数,返回字典{‘max’:最大值,’min’:最小值} 例如:min_max(2,5,7,8,4) 返回:{‘max’:8,’min’:2}(此题用到max(),min()内置函数 ...
- (详细)JAVA使用JDBC连接MySQL数据库(2)- MySQL Connectors
欢迎任何形式的转载,但请务必注明出处. 本节内容 mysql connectors介绍 下载安装 在java中配置 点击进入官网下载 一.mysql connectors介绍 mysql connec ...
- linux串口命令
proc # cat /proc/tty/driver/serial serinfo:1.0 driver revision: 0: uart:16550A port:000003F8 irq:4 t ...