Spring Boot:整合Swagger文档

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/lhh143400/article/details/102735380

综合概述

使用 Swagger 集成文档具有以下几个优势:

  • 功能丰富 :支持多种注解,自动生成接口文档界面,支持在界面测试API接口功能;
  • 及时更新 :开发过程中花一点写注释的时间,就可以及时的更新API文档,省心省力;
  • 整合简单 :通过添加pom依赖和简单配置,内嵌于应用中就可同时发布API接口文档界面,不需要部署独立服务。
  • 生成项目模板

    为方便我们初始化项目,Spring Boot给我们提供一个项目模板生成网站。

    1.  打开浏览器,访问:https://start.spring.io/

    2.  根据页面提示,选择构建工具,开发语言,项目信息等。

3.  点击 Generate the project,生成项目模板,生成之后会将压缩包下载到本地。

4.  使用IDEA导入项目,通过导入Maven项目的方式导入。

添加相关依赖

WEB依赖

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
</dependency>

swagger依赖,这里选择 2.9.2 版本。

<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
   <groupId>io.springfox</groupId>
   <artifactId>springfox-swagger2</artifactId>
   <version>2.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
   <groupId>io.springfox</groupId>
   <artifactId>springfox-swagger-ui</artifactId>
   <version>2.9.2</version>
</dependency>

 添加配置类

     添加一个swagger 配置类,在工程下新建 config 包并添加一个 SwaggerConfig 配置类。

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;

/**
 * @author lhh
 * @Date 2019/10/25 23:03
 */

@Configuration
@EnableSwagger2
public class Swagger2Config {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any()).build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("RESTful API Doc")
                .description("This is a restful api document")
                .version("1.0")
                .build();
    }
}

 添加测试类控制器

     

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author lhh
 * @Date 2019/10/25 23:07
 */
@Api(value = "desc of class")
@RestController
public class Swagger2TestController {



    /**
     * 方法注解
     *
     * @param name   ApiParam 入参说明
     */
    @ApiOperation(value = "方法注解", notes = "")
    @GetMapping(value="/hello")
    public Object hello(@ApiParam(value = "入参说明" , required=true ) @RequestParam String name) {
        return "Hello " + name + "!";
    }
}

打开浏览器,访问:http://localhost:8080/swagger-ui.html,进入swagger接口文档界面。

常用注解说明

swagger 通过注解接口生成文档,包括接口名,请求方法,参数,返回信息等。

@Api: 修饰整个类,用于controller类上

@ApiOperation: 描述一个接口,用户controller方法上

@ApiParam: 单个参数描述

@ApiModel: 用来对象接收参数,即返回对象

@ApiModelProperty: 对象接收参数时,描述对象的字段

@ApiResponse: Http响应其中的描述,在ApiResonse中

@ApiResponses: Http响应所有的描述,用在

@ApiIgnore: 忽略这个API

@ApiError: 发生错误的返回信息

@ApiImplicitParam: 一个请求参数

@ApiImplicitParam: 多个请求参数

添加请求参数

在很多时候,我们需要在调用我们每一个接口的时候都携带上一些通用参数,比如采取token验证逻辑的往往在接口请求时需要把token也一起传入后台,接下来,我们就来讲解一下如何给Swagger添加固定的请求参数。

修改SwaggerConfig配置类,替换成如下内容,利用ParameterBuilder构成请求参数。

SwaggerConfig.java

 /**
  *  添加请求参数,我们这里把token作为请求头部参数传入后端
  */
/* @Bean
 public Docket createRestApi(){
     ParameterBuilder parameterBuilder = new ParameterBuilder();
     List<Parameter> parameters = new ArrayList<Parameter>();
     parameterBuilder.name("token").description("令牌")
             .modelRef(new ModelRef("string")).parameterType("header").required(false).build();
     parameters.add(parameterBuilder.build());
     return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
             .apis(RequestHandlerSelectors.any()).paths(PathSelectors.any())
             .build().globalOperationParameters(parameters);
 }*/

猜你喜欢

转载自blog.csdn.net/lhh143400/article/details/102735380
今日推荐