swagger的使用(代码片段)

kingning kingning     2022-12-20     145

关键词:

传统文档的痛点

  • 对API文档进行更新的时候,需要通知前端开发人员,导致文档更新交流不及时;

  • API接口返回信息不明确

  • 大公司中肯定会有专门文档服务器对接口文档进行更新。

  • 缺乏在线接口测试,通常需要使用相应的API测试工具,比如postman、SoapUI等

  • 接口文档太多,不便于管理

Swagger具有以下优点

  • 功能丰富:支持多种注解,自动生成接口文档界面,支持在界面测试API接口功能;

  • 及时更新:开发过程中花一点写注释的时间,就可以及时的更新API文档,省心省力;

  • 整合简单:通过添加 pom 依赖和简单配置,内嵌于应用中就可同时发布API接口文档界面,不需要部署独立服务。

使用springboot的集成

后面会又springCloud的和zuul整合进行文档的管理

依赖文件

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
    </parent>

    <dependencies>
        <!-- SpringBoot整合Web组件 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--SpringBoot swagger2 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.8.0</version>
        </dependency>
        <!--SpringBoot swagger2 -UI -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.8.0</version>
        </dependency>
    </dependencies>

文件的配置类(java形式的)

@Configuration
@EnableSwagger2
public class SwaggerConfig 
    @Bean
    public Docket api() 
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
                // 自行修改为自己的包路径
                .apis(RequestHandlerSelectors.basePackage("com.king.swagerr.controller")).paths(PathSelectors.any())
                .build();
    

    private ApiInfo apiInfo() 
        return new ApiInfoBuilder().title("api文档").description("restfun 风格接口")
                // 服务条款网址
                // .termsOfServiceUrl("http://blog.csdn.net/forezp")
                .version("1.0")
                // .contact(new Contact("帅呆了", "url", "email"))
                .build();
    

接口类

@RestController
@RequestMapping("api")
@Api("swaggerDemoController相关的api")
public class SwaggerDemoController 

    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 String getStudent(@PathVariable int id) 
        logger.info("开始查询某个学生信息");
        return "success";
    

访问地址 http://localhost:8080//swagger-ui.html#/

zuul网关整合Swagger

zuulServer 网关的配置,其他的配置忽略了

zuul:
  routes:
    api-a:
      path: /api-member/**
      service-id: service-member
    api-b:
      path: /api-order/**
      service-id: service-order

依赖文件,统一都用这一个

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.5.0</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.5.0</version>
</dependency>

zuulServer的Swagger的配置:

@Configuration
@EnableSwagger2
public class SwaggerConfig 

    @Bean
    public Docket createRestApi() 
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo());
    

    private ApiInfo apiInfo() 
        return new ApiInfoBuilder()
                .title("分布式购物系统")
                .description("购物系统接口文档说明")
                .termsOfServiceUrl("http://localhost:8081")
                .contact(new Contact("guoning", "", "[email protected]"))
                .version("1.0")
                .build();
    

orderServer与userServer配置相同:

@Configuration
@EnableSwagger2
public class SwaggerConfig 
    @Bean
    public Docket createRestApi() 
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.order.controller")) // 扫描包
                .paths(PathSelectors.any())
                .build();
    

    private ApiInfo apiInfo()  // 下面的信息,自行更改
        return new ApiInfoBuilder()
                .title("购物系统-订单模块")
                .description("购物系统订单模块接口文档说明")
                .termsOfServiceUrl("http://localhost:8083")
                .contact(new Contact("guoning", "", "[email protected]"))
                .version("1.0")
                .build();
    

controller中的配置,

@RestController
@RequestMapping("api")
@Api("swaggerDemoController相关的api")
public class SwaggerDemoController 
    
    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 String getStudent(@PathVariable int id) 
        logger.info("开始查询某个学生信息");
        return "success";
    

就到这了,其中有参考了 https://www.jianshu.com/p/af4ff19afa04 ,因为课程视频中不太详细。

swagger的使用(代码片段)

...绍:前后端分离开发模式中,api文档是最好的沟通方式。Swagger是一个规范和完整的框架,用于生成、描述、调用和可视化RESTful风格的Web服务。总结:目前使用到swagger的测试功能,可以替代postman进行各种请求方式的api接口测试... 查看详情

swagger认识与使用swagger实现接口文档(代码片段)

文章目录1.介绍2.使用方式3.常用注解1.介绍使用Swagger你只需要按照它的规范去定义接口及接口相关的信息,再通过Swagger衍生出来的一系列项目和工具,就可以做到生成各种各式的接口文档,以及在线接口调试页面等... 查看详情

swagger认识与使用swagger实现接口文档(代码片段)

文章目录1.介绍2.使用方式3.常用注解1.介绍使用Swagger你只需要按照它的规范去定义接口及接口相关的信息,再通过Swagger衍生出来的一系列项目和工具,就可以做到生成各种各式的接口文档,以及在线接口调试页面等... 查看详情

使用swagger生成简单接口文档(代码片段)

使用swagger通过简单的配置可以生成简单的接口文档;依赖包://Swagger2 compile‘io.springfox:springfox-swagger2:2.8.0‘ compile‘io.springfox:springfox-swagger-ui:2.8.0‘启动类添加配置:importorg.springframework.boot.SpringApplication;impor 查看详情

swagger使用(代码片段)

 一swagger简介Swagger是一个用于生成、描述、调用和可视化RESTful风格的Web服务。可以跟据业务代码自动生成相关的api接口文档具有以下好处?及时性(接口变更后,能够及时准确地通知相关开发人员)?规范性(能够规范表达接口的... 查看详情

swagger2的简单使用(代码片段)

swagger2的简单使用优点:可以生成文档形式的API并提供给不同的团队使用便于自己单测无需过多冗余的word文档,这一点很重要,因为我在工作中就遇到这么一个情况,由于开发使用的文档和最新文档版本导致不一致,导致后期很... 查看详情

swagger使用二:swagger配置多个项目注释(代码片段)

...:https://www.cnblogs.com/eastday/p/6382613.html 在项目中采用swagger测试接口,提供接口给其他人员都非常的方便.在swagger默认配置中,默认只显示接口访问层中的注释,可是很多的参数说明都已经在实体层中了啊?(如下图)不可能再把实体... 查看详情

dotnetcore之旅---swagger的简单使用(代码片段)

swaggerswagger用于可视化api,并可实现对api格式化说明以及测试。使用swagger首先通过nuget引入Swashbuckle.AspNetCore(切记不是swagger) 其次修改程序生成属性,分别配置程序生成路径和xml文档生成路径第三部修改startup类1usingSystem;2usin... 查看详情

swagger的使用(代码片段)

...试工具,比如postman、SoapUI等接口文档太多,不便于管理Swagger具有以下优点功能丰富:支持多种注解,自动生成接口文档界面,支持在界面测试API接口功能;及时更新:开发过程中花一点 查看详情

swagger2学习与使用(代码片段)

文章目录什么是swagger2swagger2注解的简单使用@Api:请求类的说明@ApiOperation@ApiImplicitParams、@ApiImplicitParam@ApiResponses、@ApiResponse@ApiModel:用于javaBean上,表示一个javaBean的 查看详情

使用swagger2(代码片段)

一、Swagger2是什么?Swagger是一个规范和完整的框架,用于生成、描述、调用和可视化RESTful风格的Web服务。优点:及时性(接口变更后,能够及时准确地通知相关前后端开发人员)规范性(并且保证接口的规范性,如接口的地址,请... 查看详情

swagger3的基本使用(代码片段)

Swagger3简介swagger官网:传送门swagger是一个Api框架,就是一个工具,就比如我们可以使用postman测试接口一样,swagger主要作用是生成RESTFUL接口的文档并且可以提供功能测试;可以看一下官方文档简介:WhatIsSw... 查看详情

swagger使用(代码片段)

为什么写这篇文章如果你打开百度,搜索Swagger使用,网上说引入各种包的都有,访问的路径也是千奇百怪,你始终搞不清楚到底需要引入哪些包,访问路径到底是哪个。究其原因,是因为springboot的版本不... 查看详情

springboot使用springdoc库的swagger3.0(代码片段)

Swagger定义Swagger是一个规范和完整的框架,用于生成、描述、调用和可视化RESTful风格的Web服务。总体目标是使客户端和文件系统作为服务器以同样的速度来更新。文件的方法,参数和模型紧密集成到服务器端的代码,... 查看详情

接口文档生成工具swagger2的使用(代码片段)

一、什么是Swagger  Swagger是一个规范和完整的框架,用于生成、描述、调用和可视化RESTful风格的Web服务。总体目标是使客户端和文件系统作为服务器以同样的速度来更新。文件的方法,参数和模型紧密集成到服务器端的代码,... 查看详情

swagger2在项目中的使用(代码片段)

小伙伴大家好,今天小编为大家介绍一个简单的测试工具swagger2。大家以前进行测试都是在浏览器里面或者一些工具(postman等)进行测试,这样稍微有点麻烦。大家了解下swagger.1.在pom里面添加jar依赖,这里面选的swagger2是版本2.8.0... 查看详情

swagger配置和简单使用(代码片段)

说明:本地环境idea+ maven3.5+springboot2.0.0+ springfox-swagger22.8.0 + springfox-swagger-ui2.8.0+ swagger-bootstrap-ui1.7.2(为了展示的更好看) 1搭建完Springboot项目后在pom文件中添加依赖<springfox- 查看详情

springfox-swagger-ui3的基本使用(代码片段)

spring+springfox-swagger-ui3在线API文档的基本使用1、简介源码地址:https://github.com/springfox/springfox帮助文档:http://springfox.github.io/springfox/http://springfox.github.io/springfox/docs/current/swagger是一个 查看详情