【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- ...
随机推荐
- “无处不在” 的系统核心服务 —— ActivityManagerService 启动流程解析
本文基于 Android 9.0 , 代码仓库地址 : android_9.0.0_r45 系列文章目录: Java 世界的盘古和女娲 -- Zygote Zygote 家的大儿子 -- System ...
- 使用C++代码打印数字正方形
使用C++代码打印数字正方形 作为一名初学者,最近在跟着网课学习C++程序设计基础.在学习过程中遇到了一些习题,我根据自己的理解和思路写了一些代码实现,算是对自己学习过程的一个记录,也希望可以对别人有 ...
- 别说家庭组开不了,一定成功的方法|win7家庭组无法开启|win7如何共享打印机
两台WIN7要开启家庭组并且共享打印机 这两台电脑必须打开以下服务:dnscache(任务管理器 简写).fdrespub(任务管理器 简写).(接下来下了都是服务里面的)SSDP Discovery ...
- CSAPP:代码优化【矩阵读写】
转载请注明出处:https://www.cnblogs.com/ustca/p/11790314.html 写程序最主要的目标就是使它在所有可能的情况下都正确工作,另一方面,在很多情况下,让程序运行得 ...
- 详细讲解IPython
ipython是一个python的交互式shell,比默认的python shell好用得多,支持变量自动补全,自动缩进,支持bash shell命令,内置了许多很有用的功能和函数.学习ipython ...
- 【java基础】接口是否能有实现类?
接口是否能有实现方法 我的回答: 当然可以 java8以后就允许接口有实现方法: default修饰的方法 static修饰的方法 /** * 能用lambda的情况,接口里面只有一个未实现的方法 * ...
- 定制linux镜像并自动化安装
最近碰到个需求:要在内网环境安装centos6.5系统并搭建服务,但由于自动部署脚本里安装依赖包使用的是yum安装,而服务器无法连接外网,实施人员也不会本地yum源搭建….. 本来想法是打算把需要的依 ...
- how2heap 源码及输出
备个份,慢慢写总结 1 first_fit #include <stdio.h> #include <stdlib.h> #include <string.h> i ...
- Linux 下的 redis安装
官网下载链接:https://redis.io/download redis安装流程,记录自己的实践,分享给需要的人. 1.选择Stable(5.0)下的Download 5.0.0 链接进行下载 ( ...
- 爬虫之selenium爬取京东商品信息
import json import time from selenium import webdriver """ 发送请求 1.1生成driver对象 2.1窗口最大 ...