• Spring-Boot集成Swagger2


    SpringBoot与Swagger2集成

    By Zhai.Yt

    简介:

    swagger表示用于前后端分离,接口管理和测试工具集,Swagger规范定义了一系列的文件,用以描述API,这些文件被Swagger-ui显示用于展示API,也可以用于被Swagger-Codegen项目用于生产代码,我们使用Swagger进行APIDE设置 
    他可以帮助我们再看不到代码和源码的情况下,把我们我们去理解功能,调用我们的接口swagger表示用于前后端分离,接口管理和测试工具集,Swagger规范定义了一系列的文件,用以描述API,这些文件被Swagger-ui显示用于展示API,也可以用于被Swagger-Codegen项目用于生产代码,我们使用Swagger进行APIDE设置

    他可以帮助我们再看不到代码和源码的情况下,把我们我们去理解功能,调用我们的接口

    Swagger规范是OPENAPI的前身

    Swagger是描述REST API格式的一组规则(换言之,规范)。该格式是机器可读的和人类可读的。因此,它可以用来在产品经理,测试人员和开发人员之间共享文档,但也可以被各种工具用来自动执行与API相关的过程。通过在接口上的注解,生成可供第三方模拟测试和阅读的接口列表。

    使用:

    1.jar包引入

      <!-- 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>

      <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->

      <dependency>

           <groupId>io.springfox</groupId>

           <artifactId>springfox-swagger2</artifactId>

           <version>2.9.2</version>

            </dependency>

    2.swagger 配置启动类编写

        配置类与启动类同级

        @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("zhaixiaotao", "https://www.cnblogs.com/zhaiyt/", ""))

                          //版本号

                          .version("1.0")

                          //描述

                          .description("API 描述")

                          .build();

               }

          }

    3访问路径

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

    注意:

    @RequestMapping(value="/index",method = RequestMethod.GET)

    这里不写method的时候,页面会出现 delete put get post 多个方法

    @ApiOperation(value = "首页请求") 这些简单的描述随心加吧

     

     

  • 相关阅读:
    简单的登录验证小程序_python
    远程执行命令_python
    远程执行本地脚本_linux
    反射_python
    ssh oa项目介绍
    返回上一级过程
    ssh框架开发crm(客户关系系统总结)
    struct相对路径,绝对路径
    <s:textfield>标签回显
    ssh框架整合
  • 原文地址:https://www.cnblogs.com/zhaiyt/p/9518046.html
Copyright © 2020-2023  润新知