zoukankan      html  css  js  c++  java
  • Swagger使用

    一.pom文件中添加Swagger依赖

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

    二.创建Swaggerconfig类:Swagger2Config

    package com.github.xxx.security.config;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.bind.annotation.RestController;
    import springfox.documentation.builders.ApiInfoBuilder;
    import springfox.documentation.builders.ParameterBuilder;
    import springfox.documentation.builders.PathSelectors;
    import springfox.documentation.builders.RequestHandlerSelectors;
    import springfox.documentation.schema.ModelRef;
    import springfox.documentation.service.ApiInfo;
    import springfox.documentation.service.Parameter;
    import springfox.documentation.spi.DocumentationType;
    import springfox.documentation.spring.web.plugins.Docket;
    import springfox.documentation.swagger2.annotations.EnableSwagger2;
    
    import java.util.ArrayList;
    import java.util.List;
    
    
    @Configuration
    @EnableSwagger2
    public class Swagger2Config {
    
        @Bean
        public Docket createRestApi() {
            List<Parameter> pars = new ArrayList<Parameter>();
            return new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(apiInfo())
                    .select()
                    .apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
                    .paths(PathSelectors.any())
                    .build()
                    .globalOperationParameters(pars)
                    .apiInfo(apiInfo());
        }
    
        private ApiInfo apiInfo() {
            return new ApiInfoBuilder()
                    .title("Lark API")
                    .description("测试接口")
                    .termsOfServiceUrl("https://xxxxxx")
                    .version("1.0")
                    .build();
        }
    
    }

    三.运行项目

     四.访问前端界面查看接口

    http://项目地址:微服务端口/swagger-ui.html#/

    五.Swagger注解

    1、@Api():用在请求的类上,表示对类的说明,也代表了这个类是swagger2的资源

      1)tags:说明该类的作用,参数是个数组,可以填多个。例:

    @Api(tags = "控制类集合接口")  效果如下:

      

       2)description: "接口描述"。例:

    @Api(tags = "控制类集合接口",description = "这是一些描述控制类的接口") 效果如下

     

    2.@ApiOperation():用于方法,表示一个http请求访问该方法的操作

      1)value="接口名称"

  • 相关阅读:
    5- MySQL数据库SELECT查询操作
    4- MySQL创建表以及增删改查
    3- MySQL数据类型
    2- MySQL客户端工具Workbench的使用及数据库的操作
    4-1 Postman脚本的应用
    ImportError: cannot import name HTTPSHandler
    python zlib模块缺失报错:RuntimeError: Compression requires the (missing) zlib module
    make 命令出现:"make:*** No targets specified and no makefile found.Stop."
    用Wget下载的文件在哪里可以找到。。
    Linux wget命令详解
  • 原文地址:https://www.cnblogs.com/zhangzimo/p/13985240.html
Copyright © 2011-2022 走看看