• Springboot集成swagger2


    1、添加pom依赖(mvnrepository.com搜索springfox即可):

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

    Swagger2用于扫描程序生成接口文档。Swagger ui生成可视化的接口页面的。

    2、启动类上添加@EnableSwagger2注解:

    @EnableSwagger2
    

    3、在Controller类上添加@Api注解,方法上添加@ApiOperation注解:

    package cn.mmweikt.es.controller;
    
    import io.swagger.annotations.Api;
    import io.swagger.annotations.ApiOperation;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    @Controller
    @RequestMapping("test")
    @Api("测试")
    public class TestController {
    
        @GetMapping("test1")
        @ResponseBody
        @ApiOperation("测试方法")
        public String test(@ApiParam("用户id") Integer id){
            return "test1";
        }
    }
    

    @ApiOperation注解用来描述接口功能,最常用的属性是value。
    参数,一部分是单个的,单个属性用@ApiParam;一部分是对象参数,对象参数的属性用@ApiModelProperty。

    4、访问http://127.0.0.1:8080/swagger-ui.html验证:

  • 相关阅读:
    String/StringBuffer
    二维数组的打印,查找等
    二叉树的各种遍历
    本地安装部署ActiveCollab
    为什么我们不使用JIRA
    本地安装部署禅道
    本地安装部署Jira
    拖拉插件 drag drop
    C++二维数组 取地址 复制给 二维指针
    解决:CentOS下的 error while loading shared libraries: libmysqlclient.so.16: cannot open shared object file: No such file or dir
  • 原文地址:https://www.cnblogs.com/kibana/p/10317555.html
Copyright © 2020-2023  润新知