springboot整合swagger
特别说明:本次项目整合基于idea进行的,如果使用Eclipse可能操作会略有不同,不过总的来说不影响。
springboot整合mybatis-plus durid数据库连接池
springboot整合之logback日志配置
springboot整合pagehelper分页
springboot整合本地缓存
springboot整合redis redisson
springboot整合elasticsearch
springboot整合rabbitMq
springboot整合canal实现缓存一致性
springboot整合springSecurity(前后端不分离版本)
在开始之前先说一下自己的理解。我觉得既然要使用,首先就要知道为什么要用?该不该用?如果说我们的项目就俩人开发的一个小项目,又没有前后端分离,而且有比较急着上线,那我觉得这种真是没有必要用swagger。这里只是举个例子,就是告诉大家没有必要为了swagger而swagger。
我认为swagger更适合前后端分离情况,或者需要协同开发给别人提供接口测试的时候使用。在这种情况下能够提高协同的效率。如果一项技术的引进不能提高开发效率,或者对项目本身没有什么提升,我觉得引入的意义就不大。而且引入很多没用的文件对内存也是影响,有时候甚至会影响我们程序的性能。
如果确定适合,那就整合进来。但是千万注意一点。这个只能是测试环境使用,千万不要放到正式环境。这个一定要注意!!!关于怎么设置正式环境不可用会在本次整合中给出示范。
我在项目开发中用过两种不同风格的swagger UI,这里会都展示一下,具体用那种大家可以根据需要来选择就好了。
一、pom文件引入
因为跟之前的几次的整合相比这次对版本进行了统一的依赖管理,所以直接贴上了所有的整合pom,如果有小伙伴是按照之前一步一步来的,这里直接替换pom就好了。
-
-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
-
<modelVersion>4.0.0</modelVersion>
-
<!--spring-boot 依赖版本-->
-
<parent>
-
<groupId>org.springframework.boot</groupId>
-
<artifactId>spring-boot-starter-parent</artifactId>
-
<version>2.3.12.RELEASE</version>
-
</parent>
-
-
<!--当前项目坐标-->
-
<groupId>com.example</groupId>
-
<artifactId>springboot-demo</artifactId>
-
<version>0.0.1-SNAPSHOT</version>
-
<name>springboot-demo</name>
-
<description>springboot-demo</description>
-
-
<!--版本依赖管理-->
-
<properties>
-
<java.version>8</java.version>
-
<druid.version>1.2.15</druid.version>
-
<mybatis-plus.version>3.4.2</mybatis-plus.version>
-
<swagger.version>2.9.2</swagger.version>
-
</properties>
-
-
-
-
<dependencies>
-
<!--spring-boot-starter 依赖-->
-
<dependency>
-
<groupId>org.springframework.boot</groupId>
-
<artifactId>spring-boot-starter</artifactId>
-
</dependency>
-
-
<!--spring-boot-starter-web 依赖-->
-
<dependency>
-
<groupId>org.springframework.boot</groupId>
-
<artifactId>spring-boot-starter-web</artifactId>
-
</dependency>
-
-
<!--热启动依赖-->
-
<dependency>
-
<groupId>org.springframework.boot</groupId>
-
<artifactId>spring-boot-devtools</artifactId>
-
<scope>runtime</scope>
-
<optional>true</optional>
-
</dependency>
-
-
<!--lombok 依赖-->
-
<dependency>
-
<groupId>org.projectlombok</groupId>
-
<artifactId>lombok</artifactId>
-
<optional>true</optional>
-
</dependency>
-
-
<!--spring-boot-starter-test 依赖-->
-
<dependency>
-
<groupId>org.springframework.boot</groupId>
-
<artifactId>spring-boot-starter-test</artifactId>
-
<scope>test</scope>
-
</dependency>
-
-
<!--mysql 链接驱动-->
-
<dependency>
-
<groupId>mysql</groupId>
-
<artifactId>mysql-connector-java</artifactId>
-
</dependency>
-
-
<!--druid数据库连接池-->
-
<dependency>
-
<groupId>com.alibaba</groupId>
-
<artifactId>druid-spring-boot-starter</artifactId>
-
<version>${druid.version}</version>
-
</dependency>
-
-
<!--mybatis-plus-->
-
<dependency>
-
<groupId>com.baomidou</groupId>
-
<artifactId>mybatis-plus-boot-starter</artifactId>
-
<version>${mybatis-plus.version}</version>
-
</dependency>
-
-
<!--swagger-->
-
<dependency>
-
<groupId>io.springfox</groupId>
-
<artifactId>springfox-swagger2</artifactId>
-
<version>${swagger.version}</version>
-
</dependency>
-
-
<dependency>
-
<groupId>io.springfox</groupId>
-
<artifactId>springfox-swagger-ui</artifactId>
-
<version>${swagger.version}</version>
-
</dependency>
-
-
<!--swagger.ui-->
-
<dependency>
-
<groupId>com.github.xiaoymin</groupId>
-
<artifactId>swagger-bootstrap-ui</artifactId>
-
<version>1.9.6</version>
-
</dependency>
-
-
</dependencies>
-
-
<build>
-
<plugins>
-
<plugin>
-
<groupId>org.springframework.boot</groupId>
-
<artifactId>spring-boot-maven-plugin</artifactId>
-
<configuration>
-
<excludes>
-
<exclude>
-
<groupId>org.projectlombok</groupId>
-
<artifactId>lombok</artifactId>
-
</exclude>
-
</excludes>
-
</configuration>
-
</plugin>
-
</plugins>
-
</build>
-
-
</project>
二、创建swagger配置类
首先把位置说明一下
-
package com.example.springbootdemo.config;
-
-
import org.springframework.context.annotation.Bean;
-
import org.springframework.context.annotation.Configuration;
-
import org.springframework.context.annotation.Profile;
-
import org.springframework.web.bind.annotation.RestController;
-
import springfox.documentation.builders.ApiInfoBuilder;
-
import springfox.documentation.builders.RequestHandlerSelectors;
-
import springfox.documentation.service.ApiInfo;
-
import springfox.documentation.service.Contact;
-
import springfox.documentation.spi.DocumentationType;
-
import springfox.documentation.spring.web.plugins.Docket;
-
import springfox.documentation.swagger2.annotations.EnableSwagger2;
-
-
-
-
-
public class SwaggerConfig {
-
/**
-
* 创建API应用 apiInfo() 增加API相关信息
-
* 通过select()函数返回一个ApiSelectorBuilder实例,用来控制哪些接口暴露给Swagger来展现,
-
* 本例采用指定扫描的包路径来定义指定要建立API的目录。
-
*/
-
-
public Docket createRestApi() {
-
-
return new Docket(DocumentationType.SWAGGER_2)
-
//接口说明信息
-
.apiInfo(apiInfo())
-
// 选择那些路径和api会生成document
-
.pathMapping("/").select()
-
// 对所有api进行监控
-
.apis(RequestHandlerSelectors.any())
-
.apis(RequestHandlerSelectors.withClassAnnotation(RestController.class)).build();
-
}
-
-
-
/**
-
* 创建该API的基本信息(这些基本信息会展现在文档页面中)
-
*
-
* @return
-
*/
-
private ApiInfo apiInfo() {
-
return new ApiInfoBuilder()
-
.title("测试接口文档")
-
.contact(new Contact("William","https://www.百度.com","William940115@163.com"))
-
.termsOfServiceUrl("https://www.百度.com")
-
.description("测试接口文档")
-
.version("1.0")
-
.build();
-
}
-
-
}
说明:
@Configuration 将当前配置类交给spring管理 @EnableSwagger2 开启swagger注解,此注解也可以添加到spring boot启动类 @Profile("dev") 开启只有dev环境生效,此外swagger也提供了基于环境的可见性设置,这里就不作详细介绍了,如果感兴趣的可以自己研究一下,也很简单。
三、创建测试类
因为测试,我们先临时写一个测试类,主要就是测试我们的swagger是否整合成功。
-
package com.example.springbootdemo.controller;
-
-
import com.example.springbootdemo.entity.User;
-
import com.example.springbootdemo.service.UserService;
-
import io.swagger.annotations.Api;
-
import io.swagger.annotations.ApiOperation;
-
import org.springframework.beans.factory.annotation.Autowired;
-
import org.springframework.web.bind.annotation.GetMapping;
-
import org.springframework.web.bind.annotation.PathVariable;
-
import org.springframework.web.bind.annotation.RequestMapping;
-
import org.springframework.web.bind.annotation.RestController;
-
-
/**
-
* @description: TestController <br>
-
* @date: 2022/12/17 0017 上午 10:06 <br>
-
* @author: William <br>
-
* @version: 1.0 <br>
-
*/
-
-
-
-
public class TestController {
-
-
-
private UserService userService;
-
-
-
-
-
public String getUserById({ Integer id)
-
//参数校验
-
if(id == null || id<=0){
-
return "参数非法";
-
}
-
User user = userService.getById(id);
-
return user.toString();
-
}
-
-
}
四、启动测试
首先给大家看一下我比较喜欢的一种,输入http://localhost:8080/doc.html进行访问
各人觉得很不错的一个UI页面,看起来很舒服。而且支持离线导出,相当给力。个人觉得比swagger自带的UI要好用一点。
然后就是另一种:输入http://localhost:8080/swagger-ui.html
这种是我们比较常见的。我以前也经常用,后来发现了第一种这种就没用了。
OK,到这里整合就完成了。下面就是常用的一些注解了。
五、swagger常用注解
从官方给的解释,各位学习使用的时候建议直接从这里看,而且都给了例子,很简单。
Name | Description |
---|---|
@Api | Marks a class as a Swagger resource. |
@ApiImplicitParam | Represents a single parameter in an API Operation. |
@ApiImplicitParams | A wrapper to allow a list of multiple ApiImplicitParam objects. |
@ApiModel | Provides additional information about Swagger models. |
@ApiModelProperty | Adds and manipulates data of a model property. |
@ApiOperation | Describes an operation or typically a HTTP method against a specific path. |
@ApiParam | Adds additional meta-data for operation parameters. |
@ApiResponse | Describes a possible response of an operation. |
@ApiResponses | A wrapper to allow a list of multiple ApiResponse objects. |
@Authorization | Declares an authorization scheme to be used on a resource or an operation. |
@AuthorizationScope | Describes an OAuth2 authorization scope. |
好了,到这里我们swagger就整合好了。另外说一下,为了展示两种效果所以我在pom中引入了两个swagger-ui的jar包,后面我会删掉一个,只保留一个。大家可以根据自己需要来。我觉得最好只保留一个就好了。
六、最终pom
我保留的是我比较喜欢的一个,修改以后的pom文件如下:如果跟我一样的可以直接复制到自己的项目。
-
-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
-
<modelVersion>4.0.0</modelVersion>
-
<!--spring-boot 依赖版本-->
-
<parent>
-
<groupId>org.springframework.boot</groupId>
-
<artifactId>spring-boot-starter-parent</artifactId>
-
<version>2.3.12.RELEASE</version>
-
</parent>
-
-
<!--当前项目坐标-->
-
<groupId>com.example</groupId>
-
<artifactId>springboot-demo</artifactId>
-
<version>0.0.1-SNAPSHOT</version>
-
<name>springboot-demo</name>
-
<description>springboot-demo</description>
-
-
<!--版本依赖管理-->
-
<properties>
-
<java.version>8</java.version>
-
<druid.version>1.2.15</druid.version>
-
<mybatis-plus.version>3.4.2</mybatis-plus.version>
-
<swagger.version>2.9.2</swagger.version>
-
<swagger-ui.version>1.9.6</swagger-ui.version>
-
</properties>
-
-
-
-
<dependencies>
-
<!--spring-boot-starter 依赖-->
-
<dependency>
-
<groupId>org.springframework.boot</groupId>
-
<artifactId>spring-boot-starter</artifactId>
-
</dependency>
-
-
<!--spring-boot-starter-web 依赖-->
-
<dependency>
-
<groupId>org.springframework.boot</groupId>
-
<artifactId>spring-boot-starter-web</artifactId>
-
</dependency>
-
-
<!--热启动依赖-->
-
<dependency>
-
<groupId>org.springframework.boot</groupId>
-
<artifactId>spring-boot-devtools</artifactId>
-
<scope>runtime</scope>
-
<optional>true</optional>
-
</dependency>
-
-
<!--lombok 依赖-->
-
<dependency>
-
<groupId>org.projectlombok</groupId>
-
<artifactId>lombok</artifactId>
-
<optional>true</optional>
-
</dependency>
-
-
<!--spring-boot-starter-test 依赖-->
-
<dependency>
-
<groupId>org.springframework.boot</groupId>
-
<artifactId>spring-boot-starter-test</artifactId>
-
<scope>test</scope>
-
</dependency>
-
-
<!--mysql 链接驱动-->
-
<dependency>
-
<groupId>mysql</groupId>
-
<artifactId>mysql-connector-java</artifactId>
-
</dependency>
-
-
<!--druid数据库连接池-->
-
<dependency>
-
<groupId>com.alibaba</groupId>
-
<artifactId>druid-spring-boot-starter</artifactId>
-
<version>${druid.version}</version>
-
</dependency>
-
-
<!--mybatis-plus-->
-
<dependency>
-
<groupId>com.baomidou</groupId>
-
<artifactId>mybatis-plus-boot-starter</artifactId>
-
<version>${mybatis-plus.version}</version>
-
</dependency>
-
-
<!--swagger-->
-
<dependency>
-
<groupId>io.springfox</groupId>
-
<artifactId>springfox-swagger2</artifactId>
-
<version>${swagger.version}</version>
-
</dependency>
-
-
<!--swagger.ui-->
-
<dependency>
-
<groupId>com.github.xiaoymin</groupId>
-
<artifactId>swagger-bootstrap-ui</artifactId>
-
<version>${swagger-ui.version}</version>
-
</dependency>
-
-
</dependencies>
-
-
<build>
-
<plugins>
-
<plugin>
-
<groupId>org.springframework.boot</groupId>
-
<artifactId>spring-boot-maven-plugin</artifactId>
-
<configuration>
-
<excludes>
-
<exclude>
-
<groupId>org.projectlombok</groupId>
-
<artifactId>lombok</artifactId>
-
</exclude>
-
</excludes>
-
</configuration>
-
</plugin>
-
</plugins>
-
</build>
-
-
</project>
如果文章对你有所帮助的话,可以点赞关注一下~
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhgfhieg
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01 -
怎样阻止微信小程序自动打开
PHP中文网 06-13