swagger2

本文介绍如何在SpringBoot项目中集成Swagger2,实现RESTful API的自动化测试与文档生成。通过配置与注解,可轻松完成接口测试并实时更新。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

微服务学习二:springboot与swagger2的集成

现在测试都提倡自动化测试,那我们作为后台的开发人员,也得进步下啊,以前用postman来测试后台接口,那个麻烦啊,一个字母输错就导致测试失败,现在swagger的出现可谓是拯救了这些开发人员,便捷之处真的不是一点两点。下面我们看下如何在微服务中将springboot与swagger来结合吧。

1、swagger是什么,这个我觉得凡是一个开发人员就应该知道度娘啊,绝对强大。

简单说下,它的出现就是为了方便进行测试后台的restful形式的接口,实现动态的更新,当我们在后台的接口修改了后,swagger可以实现自动的更新,而不需要认为的维护这个接口进行测试。

2、springboot与swagger的集成:

第一步:jar包的引入:

关于jar包的引入出现了一个问题就是版本的问题,可能需要与你的编辑器或者jdk要匹配吧,试了几个才最终成功导入jar。

第二步:swagger的配置启动类编写:

要使用swagger要进行一些配置,这个在界面的图上是可以显示的:类似于说明书:在这个类中我们会使用注解来进行启动swagger:

具体配置如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package  com.springboot.example;
 
 
 
 
//swagger2的配置文件,在项目的启动类的同级文件建立
 
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.service.Contact;
import  springfox.documentation.spi.DocumentationType;
import  springfox.documentation.spring.web.plugins.Docket;
import  springfox.documentation.swagger2.annotations.EnableSwagger2;
 
@Configuration
@EnableSwagger2
public  class  Swagger2 {
//swagger2的配置文件,这里可以配置swagger2的一些基本的内容,比如扫描的包等等
     @Bean
     public  Docket createRestApi() {
         return  new  Docket(DocumentationType.SWAGGER_2)
                 .apiInfo(apiInfo())
                 .select()
                 //为当前包路径
                 .apis(RequestHandlerSelectors.basePackage( "com.springboot.example.Controller" ))
                 .paths(PathSelectors.any())
                 .build();
     }
     //构建 api文档的详细信息函数,注意这里的注解引用的是哪个
     private  ApiInfo apiInfo() {
         return  new  ApiInfoBuilder()
                 //页面标题
                 .title( "Spring Boot 测试使用 Swagger2 构建RESTful API" )
                 //创建人
                 .contact( new  Contact( "MarryFeng" "http://www.baidu.com" "" ))
                 //版本号
                 .version( "1.0" )
                 //描述
                 .description( "API 描述" )
                 .build();
     }
 
 
}

  这里的坑是:所使用类的引入文件要注意到底是哪个,之前因为这个出错了,

1
2
@Configuration
@EnableSwagger2 <br>这两个注解,一个是swagger2的配置,一个是项目启动的时候启动swagger2.<br>具体什么意思看下代码就知道了。
1
2
//为当前包路径
<span style= "color: #ff0000" >.apis(RequestHandlerSelectors.basePackage( "com.springboot.example.Controller" ))</span><br><span style= "color: #ff0000" >这个包指的是我们在哪些类中使用swagger2来测试。</span>

第三步:使用swagger来进行模拟测试:

使用swagger2来进行测试接口主要是在哪些类中使用:这里我们依然选择在controller层:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package  com.springboot.example.Controller;
 
import  com.springboot.example.Service.StudentService;
import  com.springboot.example.entity.Student;
import  io.swagger.annotations.Api;
import  io.swagger.annotations.ApiImplicitParam;
import  io.swagger.annotations.ApiOperation;
import  org.slf4j.Logger;
import  org.slf4j.LoggerFactory;
import  org.springframework.beans.factory.annotation.Autowired;
import  org.springframework.web.bind.annotation.PathVariable;
import  org.springframework.web.bind.annotation.RequestMapping;
import  org.springframework.web.bind.annotation.RequestMethod;
import  org.springframework.web.bind.annotation.RestController;
 
/**
  * Created by Administrator on 2017/9/13.
  */
@RestController
@RequestMapping ( "api" )
@Api ( "swaggerDemoController相关的api" )
public  class  SwaggerDemoController {
     @Autowired
     private  StudentService studentService;
 
     private  static  final  Logger logger= LoggerFactory.getLogger(SwaggerDemoController. class );
 
 
     @ApiOperation (value =  "根据id查询学生信息" , notes =  "查询数据库中某个的学生信息" )
     @ApiImplicitParam (name =  "id" , value =  "学生ID" , paramType =  "path" , required =  true , dataType =  "Integer" )
     @RequestMapping (value =  "/{id}" , method = RequestMethod.GET)
     public  Student getStudent( @PathVariable  int  id) {
         logger.info( "开始查询某个学生信息" );
         return  studentService.selectStudentById(id);
     }
 
 
}

 

上面这些可以看下具体的注解是什么意思:

这样swagger2与springboot就集成完毕了。

看下最终效果吧:

访问路径:

http://localhost:8080/swagger-ui.html

输入id后,我们可以看到查询结果:、

是不是很方便,我们不用像postman一样来编写入口,swagger2自动完成:

而且实时更新:

是不是很方便!

至此swagger2与springboot的集成完毕。

### Swagger2 使用指南 Swagger 是一种用于设计、构建和记录 RESTful API 的开源框架。以下是关于如何使用 Swagger2 和其文档生成工具的详细介绍。 #### 工具概述 Swagger 提供了一系列工具来支持开发者的工作流程,其中包括 `swagger-tools`[^1] 这样的库,可以用来验证模式以及实现其他功能扩展。对于离线环境下的开发需求,则有专门的项目如 `SwaggerOfflineDoc` 可供选择[^2]。 #### 配置与集成 为了在应用程序中启用 Swagger2 功能,通常需要完成以下几个方面的设置: - **引入依赖** 如果您正在使用 Maven 构建您的 Java 应用程序,那么可以在项目的 pom.xml 文件里加入如下配置片段以添加 swagger-core 相关依赖项: ```xml <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> ``` - **初始化 Docket Bean** 接下来,在 Spring Boot 中通过创建一个 Configuration 类并定义相应的 bean 来启动 Swagger 支持。 ```java @Configuration public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.any()) .paths(PathSelectors.any()) .build(); } } ``` #### 页面浏览体验 一旦上述步骤执行完毕之后,当运行起服务端口,默认情况下可以通过浏览器访问 `/swagger-ui.html` 路径查看自动生成的API文档界面[^3]。在这个界面上不仅能够直观看到各个接口及其描述信息外,还允许用户直接在线尝试调用这些接口而无需额外编写客户端代码。 ### Swagger2 文档生成工具推荐 除了官方自带的功能之外,还有许多第三方插件可以帮助更高效地管理和维护 API 文档。比如前面提到过的 `SwaggerOfflineDoc`, 它是一个针对无法联网场景特别优化后的解决方案, 方便内部网络或者无互联网连接环境下继续享受完整的 swagger 特性. ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值