• 在Springboot中使用swagger2


    1.Swagger简介:在前后端分离的时代,无论是前端调用后端,还是后端调用后端,都期望有一个好的接口文档,Swagger的出现即提供了一个规范,解决了这个问题。

    2.在Springboot中使用Swagger:

      (1)pom.xml中添加依赖:

     1         <dependency>
     2             <groupId>io.springfox</groupId>
     3             <artifactId>springfox-swagger2</artifactId>
     4             <version>2.7.0</version>
     5         </dependency>
     6         <dependency>
     7             <groupId>io.springfox</groupId>
     8             <artifactId>springfox-swagger-ui</artifactId>
     9             <version>2.7.0</version>
    10         </dependency>

      (2)建立swagger配置文件比如命名为swaggerconfig.java

     1 package com.example.demo.config01;
     2 
     3 
     4 import io.swagger.annotations.ApiOperation;
     5 import org.springframework.context.annotation.Bean;
     6 import org.springframework.context.annotation.Configuration;
     7 import springfox.documentation.builders.PathSelectors;
     8 import springfox.documentation.builders.RequestHandlerSelectors;
     9 import springfox.documentation.service.ApiInfo;
    10 import springfox.documentation.service.Contact;
    11 import springfox.documentation.spi.DocumentationType;
    12 import springfox.documentation.spring.web.plugins.Docket;
    13 import springfox.documentation.swagger2.annotations.EnableSwagger2;
    14 
    15 @Configuration
    16 @EnableSwagger2
    17 public class SwaggerConfig {
    18 
    19     @Bean
    20     public Docket applicationApi() {
    21         return new Docket(DocumentationType.SWAGGER_2)
    22                 .groupName("fak")
    23                 .select().apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
    24                 .paths(PathSelectors.any()).build()
    25                 .apiInfo(application());
    26     }
    27 
    28     private ApiInfo application() {
    29         ApiInfo apiInfo = new ApiInfo("接口管理"
    30                 , "api接口说明"
    31                 , "0.1"
    32                 , ""
    33                 , new Contact("zwill", "", "")
    34                 , "连接显示文字"
    35                 , "");
    36         return apiInfo;
    37     }
    38 }

      (3)测试是否配置成功

     1 package com.example.demo.controller;
     2 
     3 import io.swagger.annotations.Api;
     4 import io.swagger.annotations.ApiOperation;
     5 import org.springframework.web.bind.annotation.GetMapping;
     6 import org.springframework.web.bind.annotation.RestController;
     7 
     8 @Api(tags = "Server")
     9 @RestController
    10 public class ServerController {
    11     @ApiOperation(value = "提供一个简单的对外接口")
    12     @GetMapping("/getServer")
    13     public String ServerApi() {
    14         return "hello";
    15     }
    16 }

      (4)结果展示(我默认8080端口访问这个网站即可 http://localhost:8080/swagger-ui.html

       单击Try it out! 即可进行测试,是不是特别的方便呢?

      (5)部分常用swagger2的注解

    @Api() 
    用于类;表示标识这个类是swagger的资源 
    tags–表示说明 
    value–也是说明,可以使用tags替代 
    但是tags如果有多个值,会生成多个list

    @ApiOperation() 用于方法;表示一个http请求的操作 
    value用于方法描述 
    notes用于提示内容 
    tags可以重新分组(视情况而用) 
    @ApiParam() 用于方法,参数,字段说明;表示对参数的添加元数据(说明或是否必填等) 
    name–参数名 
    value–参数说明 
    required–是否必填

    @ApiModel()用于类 ;表示对类进行说明,用于参数用实体类接收 
    value–表示对象名 
    description–描述 
    都可省略 
    @ApiModelProperty()用于方法,字段; 表示对model属性的说明或者数据操作更改 
    value–字段说明 
    name–重写属性名字 
    dataType–重写属性类型 
    required–是否必填 
    example–举例说明 
    hidden–隐藏

    @ApiIgnore()用于类或者方法上,可以不被swagger显示在页面上 
    比较简单, 这里不做举例

    @ApiImplicitParam() 用于方法 
    表示单独的请求参数 
    @ApiImplicitParams() 用于方法,包含多个 @ApiImplicitParam 
    name–参数ming 
    value–参数说明 
    dataType–数据类型 
    paramType–参数类型 
    example–举例说明

  • 相关阅读:
    [原创]iFPGACable FT2232H JTAG & UART调试器
    [原创]创芯电子实验室iFPGACable JTAG工具实物图
    [原创]FPGA JTAG工具设计(一)
    [方案]基于Zynq WiFi方案构建
    [原创]基于Zynq Linux环境搭建(四)
    [原创]基于Zynq Linux环境搭建(二)
    [原创]基于Zynq Linux环境搭建(三)
    [原创]Zynq AXICDMA测试结果
    [原创]基于Zynq Linux环境搭建(一)
    git使用教程及github远程仓库管理
  • 原文地址:https://www.cnblogs.com/apex-wzw/p/12093490.html
Copyright © 2020-2023  润新知