【SpringBoot | Swagger】SpringBoot整合Swagger
SpringBoot整合Swagger
1. 什么是Swagger
Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。简单说就是项目跑起来了,你的接口可视化展现出来。
2. 怎么在SpringBoot中使用Swagger
1. 配置pom.xml
这里我们导入swagger的相关内容,并额外导入了一个开源的显示界面包,左右布局符合国人界面使用习惯
<!-- 自动生成API文档 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!-- Swagger UI插件 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<!-- 开源的api接口显示界面,左右布局 -->
<dependency>
<groupId>com.github.caspar-chen</groupId>
<artifactId>swagger-ui-layer</artifactId>
<version>1.1.3</version>
</dependency>
2. 编写Swagger配置类
package com.xxx.xxx.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* 配置Swagger2
*
* @author axiang
*/
@Configuration
@EnableSwagger2
public class SwaggerConfig {
/**
* 关闭 swagger,从配置文件中读取内容,这里这么做是将生产环境和开发环境的配置区分开来,毕竟你不会想在实际生产环境中开放接口文档给其他人的
* @Value 注解的作用可查看百度,这里不多做解释
*/
@Value("${xxx.swagger.enable}")
private boolean enableSwagger;
private String title = "xxx项目接口文档";
private String description = "xxx项目接口文档,Spring boot版";
private String version = "1.0.0";
private String termsOfServiceUrl = "";
private String license = "";
private String licenseUrl = "";
@Bean
public Docket api() {
// 允许开启swagger
if (enableSwagger) {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
// 扫描Controller所在的包
.apis(RequestHandlerSelectors.basePackage("xxx.xxx.controller"))
.paths(PathSelectors.any())
.build();
} else {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.paths(PathSelectors.none())
.build();
}
}
private ApiInfo apiInfo() {
// 具体内容是什么功能请百度相关文档
return new ApiInfoBuilder()
.title(title)
.description(description)
.version(version)
.termsOfServiceUrl(termsOfServiceUrl)
.license(license)
.licenseUrl(licenseUrl)
.build();
}
}
3. 使用
由于我们采用了第三方界面包,所以可以通过两个路径访问
请注意,第三方界面包依然调用的swagger的接口,所以关闭swagger后第三方界面包不能正常使用哦
# 原装正品请访问
http://[IP地址]:[端口号]/[项目路径]/swagger-ui.html
# 左右布局请访问
http://[IP地址]:[端口号]/[项目路径]/docs.html
【SpringBoot | Swagger】SpringBoot整合Swagger的更多相关文章
- SpringBoot学习之整合Swagger
Swagger介绍 1.什么是Swagger 作为后端程序开发,我们多多少少写过几个后台接口项目,不管是编写手机端接口,还是目前比较火热的前后端分离项目,前端与后端都是由不同的工程师进行开发,那么这之 ...
- 从零开始的SpringBoot项目 ( 五 ) 整合 Swagger 实现在线API文档的功能
综合概述 spring-boot作为当前最为流行的Java web开发脚手架,越来越多的开发者选择用其来构建企业级的RESTFul API接口.这些接口不但会服务于传统的web端(b/s),也会服务于 ...
- 从零开始的SpringBoot项目 ( 六 ) 整合 MybatisPlus 实现代码自动生成
1.添加依赖 <!-- MySQL数据库 --> <dependency> <groupId>mysql</groupId> <artifactI ...
- SpringBoot整合Swagger测试api构建
@Author:SimpleWu 什么是Swagger? Swagger是什么:THE WORLD'S MOST POPULAR API TOOLING 根据官网的介绍: Swagger Inspec ...
- SpringBoot 整合swagger
springBoot 整合swagger 1.pom.xml 配置 <dependency> <groupId>io.springfox</groupId> < ...
- SpringBoot整合Swagger和Actuator
前言 本篇文章主要介绍的是SpringBoot整合Swagger(API文档生成框架)和SpringBoot整合Actuator(项目监控)使用教程. SpringBoot整合Swagger 说明:如 ...
- 玩转 SpringBoot 2 快速整合 | 丝袜哥(Swagger)
概述 首先让我引用 Swagger 官方的介绍: Design is the foundation of your API development. Swagger makes API design ...
- SpringBoot整合Swagger实战
源码地址:https://github.com/laolunsi/spring-boot-examples 目前SpringBoot常被用于开发Java Web应用,特别是前后端分离项目.为方便前后端 ...
- SpringBoot + Vue + ElementUI 实现后台管理系统模板 -- 后端篇(一): 搭建基本环境、整合 Swagger、MyBatisPlus、JSR303 以及国际化操作
相关 (1) 相关博文地址: SpringBoot + Vue + ElementUI 实现后台管理系统模板 -- 前端篇(一):搭建基本环境:https://www.cnblogs.com/l-y- ...
随机推荐
- 深入理解Transformer及其源码解读
深度学习广泛应用于各个领域.基于transformer的预训练模型(gpt/bertd等)基本已统治NLP深度学习领域,可见transformer的重要性.本文结合<Attention is a ...
- std::multimap
标准库还定义了一个 multimap 容器,它与 map 类似,所不同的是它允许重复键. 成员函数 insert() make_pair() 辅助函数来完成此任务. find(k) 返回指向第一个与键 ...
- 将windows项目移植到linux上
提要:由于项目使用java开发,移植中没有什么编译问题(移植很快,但小问题多) 1.移植过程中遇到的问题: (1).由于项目中使用了 1024以下的端口号,导致网络通信一直出错 原因:因为Linux要 ...
- Redis(十三)Python客户端redis-py
一.安装redis-py的方法 使用pip install安装redis-py C:\Users\BigJun>pip3 install redis Collecting redis Downl ...
- ArangoDB简单实例介绍
数据介绍: 2008美国国内航班数据 airports.csv flights.csv 数据下载地址:https://www.arangodb.com/graphcourse_demodata_ara ...
- 一篇文章让你彻底理解java中抽象类和接口
目录 1.我所理解的抽象类 2.我所理解的接口 3.抽象类和接口本质区别 相信大家都有这种感觉:抽象类与接口这两者有太多相似的地方,又有太多不同的地方.往往这二者可以让初学者摸不着头脑,无论是在实际编 ...
- 原生js实现上拉加载
原生js实现上拉加载其实超级简单,把原理整明白了你也会,再也不用去引一个mescroll啦~ 好了,废话不多说,开始进入正题:上拉加载是怎么去做的,原理就是监听滚动条滑到页面底部,然后就去做一次请求数 ...
- li列表循环滚动的简单方法,无需插件,简单方法搞定
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 学习笔记57_WCF基础
参考书籍<WCF揭秘> 参考博客园“xfrog” 1.做一个接口,例如: 2.使用一个类,例如:FirstSrvice这个类,来实现这个接口. 3.建立WCF的 宿主 程序: 4.配 ...
- [Java]Java类和对象内存分配详解
描述 代码说明: 一.当Person p1 = new Person();第一次被调用时需要做两件事: 1.先判断类加载器是否加载过Person类,如果没有则加载到Person类型到方法区 2.在堆中 ...