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的更多相关文章

  1. SpringBoot学习之整合Swagger

    Swagger介绍 1.什么是Swagger 作为后端程序开发,我们多多少少写过几个后台接口项目,不管是编写手机端接口,还是目前比较火热的前后端分离项目,前端与后端都是由不同的工程师进行开发,那么这之 ...

  2. 从零开始的SpringBoot项目 ( 五 ) 整合 Swagger 实现在线API文档的功能

    综合概述 spring-boot作为当前最为流行的Java web开发脚手架,越来越多的开发者选择用其来构建企业级的RESTFul API接口.这些接口不但会服务于传统的web端(b/s),也会服务于 ...

  3. 从零开始的SpringBoot项目 ( 六 ) 整合 MybatisPlus 实现代码自动生成

    1.添加依赖 <!-- MySQL数据库 --> <dependency> <groupId>mysql</groupId> <artifactI ...

  4. SpringBoot整合Swagger测试api构建

    @Author:SimpleWu 什么是Swagger? Swagger是什么:THE WORLD'S MOST POPULAR API TOOLING 根据官网的介绍: Swagger Inspec ...

  5. SpringBoot 整合swagger

    springBoot 整合swagger 1.pom.xml 配置 <dependency> <groupId>io.springfox</groupId> < ...

  6. SpringBoot整合Swagger和Actuator

    前言 本篇文章主要介绍的是SpringBoot整合Swagger(API文档生成框架)和SpringBoot整合Actuator(项目监控)使用教程. SpringBoot整合Swagger 说明:如 ...

  7. 玩转 SpringBoot 2 快速整合 | 丝袜哥(Swagger)

    概述 首先让我引用 Swagger 官方的介绍: Design is the foundation of your API development. Swagger makes API design ...

  8. SpringBoot整合Swagger实战

    源码地址:https://github.com/laolunsi/spring-boot-examples 目前SpringBoot常被用于开发Java Web应用,特别是前后端分离项目.为方便前后端 ...

  9. SpringBoot + Vue + ElementUI 实现后台管理系统模板 -- 后端篇(一): 搭建基本环境、整合 Swagger、MyBatisPlus、JSR303 以及国际化操作

    相关 (1) 相关博文地址: SpringBoot + Vue + ElementUI 实现后台管理系统模板 -- 前端篇(一):搭建基本环境:https://www.cnblogs.com/l-y- ...

随机推荐

  1. opencv::Brisk检测与匹配

    Brisk(Binary Robust Invariant Scalable Keypoints)特征介绍 构建尺度空间 特征点检测 FAST9-16寻找特征点 特征点定位 关键点描述子

  2. config.xml

    ASP.NET应用程序的配置信息都存放于Web.config配置文件中,Web.config配置文件是基于XML格式的文件类型,由于XML文件的可伸缩性,使得ASP.NET应用配置变得灵活.高效.容易 ...

  3. 解决html连续字符或数字换行的问题

    word-break: break-all; word-wrap:break-word; 强制换行

  4. 小白学 Python(12):基础数据结构(字典)(上)

    人生苦短,我选Python 前文传送门 小白学 Python(1):开篇 小白学 Python(2):基础数据类型(上) 小白学 Python(3):基础数据类型(下) 小白学 Python(4):变 ...

  5. selenium学习-拖拽页面元素

    一.ActionChains包 模拟鼠标的操作要首先引入ActionChains的包 from selenium.webdriver.common.action_chains import Actio ...

  6. .NET手撸2048小游戏

    .NET手撸2048小游戏 2048是一款益智小游戏,得益于其规则简单,又和2的倍数有关,因此广为人知,特别是广受程序员的喜爱. 本文将再次使用我自制的"准游戏引擎"FlysEng ...

  7. SSM简易版

    技术准备 Java: 基础知识 框架: Spring,SpringMVC,Mybatis 数据库: Mysq 开发工具: Eclipse,Maven 项目结构 数据库设计 创建数据库:student ...

  8. 面经-科大讯飞AI研究院

    面试时间:2019.06.27 电话面试 面试岗位:计算机视觉算法工程师/一面 面试时长:45分钟 面试内容: 自我介绍 简历中选择一个项目介绍-视频召回 问及项目中的语音.人脸.标题.模态缺失相关细 ...

  9. NOIP模(ka)拟(chang)测试30 考试报告

    应得分:300 实得分:210 毒瘤卡常出题人,卡掉90分! T1 Return 开个副本数组sort一下,unique去重就可以啦.时间复杂度$ O(nlog2(n)) $ T2 One 其实就是约 ...

  10. python之装饰器的两种写法

    上一篇文章介绍了 装饰器的概念.现在讲一下在程序中怎么来写装饰器.上代码: def X(fun): def Y(b): print(b) fun() return Y def test(): prin ...