【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- ...
随机推荐
- 实用---生命游戏 Java
本程序由四个类组成: 其中Init_data,用于初始化各个活细胞的状态judge_state,用于判断下一代的细胞状态,并进行更新.set_color,用于给GUI界面中各个细胞涂色set_fram ...
- 扩展阿里p3c实现自定义代码规范检查
前段时间fastjson报出了漏洞,只要打开setAutoType特性就会存在风险,自己测试环境的一个项目被揪出来了-_-!.虽然改动很小,但就是觉得憋屈.fastjson还是挺好的,想着禁用的话太 ...
- 谢宝友: 手把手教你给Linux内核发patch
本文系转载,著作权归作者所有. 商业转载请联系作者获得授权,非商业转载请注明出处. 作者: 谢宝友 来源: 微信公众号 linux阅码场 (id: linuxdev) 本文简介 本文一步一 ...
- 面试官:"准备用HashMap存1w条数据,构造时传10000还会触发扩容吗?"
// 预计存入 1w 条数据,初始化赋值 10000,避免 resize. HashMap<String,String> map = new HashMap<>(10000) ...
- human_pose_estimation_demo的进一步研究
一.demo能力 OpenVINO提供了范例(human_pose_estimation_demo),能够在CPU上以较快速度识别出多人 -iE:/OpenVINO_modelZoo/head-pos ...
- 【Bug】解决 java.sql.SQLSyntaxErrorException 异常
java.sql.SQLSyntaxErrorException: You have an error in your SQL syntax 错误 错误详情: Caused by: java.sql. ...
- justjavac(迷渡)知乎live--<<前端工程师的入门与进阶>>听讲总结
知乎听讲总结 知乎live----jjc<前端工程师的入门进阶> git地址 内容 前端的基础知识,计算机专业基础知识感觉还行.前端后台都有做过,现在觉得自己要深入.但是只看框架源码和自己 ...
- C++ 11标准
C++11,也称为C++0x.为目前C++编程语言的最新正式标准(ISO/IEC 14882:2011).它将取代第二版标准ISO/IEC 14882:2003(第一版ISO/IEC 14882:19 ...
- Redis(十一)缓存设计
一.缓存的收益和成本 左侧为客户端直接调用存储层的架构,右侧为比较典型的缓存层+存储层架构, 缓存加入后带来的收益如下: 加速读写:因为缓存通常都是全内存的(例如Redis.Memcache),而存储 ...
- django-Views之常见的几种错误视图代码(三)
1.404 page not found(找不到对应的页面) 2.500 server error(服务器错误) 3.400 bad request(无效的请求) 4.403 HTTP forbidd ...