• SpringBoot使用Swagger2构建API文档


    一、添加依赖

    <!--SpringBoot使用Swagger2构建API文档的依赖-->
            <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配置类

    package com.offcn.config;
    
    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;
    
    @Configuration//表示该类为一个配置类,相当于spring中的xml配置文件
    @EnableSwagger2 //开启在线文档
    public class SwaggerConfig {
    
        //1.声明 api 文档的属性
        private ApiInfo apiInfo() {
            return new ApiInfoBuilder()
                    .title("Spring Boot中使用Swagger2构建RESTful APIs")
                    .description("优就业")
                    .termsOfServiceUrl("http://www.ujiuye.com/")
                    .contact("小刘同学")
                    .version("1.0")
                    .build();
        }
    
        //配置核心配置信息
        public Docket createRestApi() {
            return new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(apiInfo())
                    .select()
                    .apis(RequestHandlerSelectors.basePackage("com.offcn.controller"))
                    .paths(PathSelectors.any())
                    .build();
        }
    
    
    }

    三、修改Controller  增加文档注释

    通过@ApiOperation注解来给API增加说明 

    通过@ApiImplicitParams@ApiImplicitParam注解来给参数增加说明

     1 package com.offcn.controller;
     2 
     3 import com.offcn.dao.UserDao;
     4 import com.offcn.entity.User;
     5 import io.swagger.annotations.ApiImplicitParam;
     6 import io.swagger.annotations.ApiImplicitParams;
     7 import io.swagger.annotations.ApiOperation;
     8 import org.springframework.beans.factory.annotation.Autowired;
     9 import org.springframework.web.bind.annotation.DeleteMapping;
    10 import org.springframework.web.bind.annotation.GetMapping;
    11 import org.springframework.web.bind.annotation.RequestMapping;
    12 import org.springframework.web.bind.annotation.RestController;
    13 
    14 @RequestMapping("/rest")
    15 @RestController
    16 public class RestFulController {
    17 
    18     @Autowired
    19     private UserDao userDao;
    20 
    21     @GetMapping("/getUserById")
    22     @ApiOperation(value="查找指定id用户信息", notes="根据id查找用户信息")
    23     @ApiImplicitParams({
    24             @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Integer"),
    25     })
    26     public User getUserById(Integer id){
    27         User user = userDao.getOne(id);
    28         return user;
    29     }
    30 
    31     @DeleteMapping("/del")
    32     @ApiOperation(value="删除指定id用户信息", notes="根据id删除用户信息")
    33     @ApiImplicitParams({
    34             @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Integer"),
    35     })
    36     public String delUserById(Integer id){
    37         userDao.deleteById(id);
    38         return "success";
    39     }
    40 }

     

    四、查看Swagger2文档

    重启项目

    访问:

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

  • 相关阅读:
    web.config常用配置
    Asp.net高级程序设计之WEB窗体(3)
    Asp.net高级程序设计之.NET开发模型(2)
    Asp.net高级程序设计之ASP.NET简介(1)
    vs2005新建类,自定义模板信息(转载)
    Petshop详解之数据库设计
    Probabilistic Graphical Models: Principles and Techniques下载 Lei
    一些推荐系统包 Lei
    [转]Learning to Rank for IR的评价指标—MAP,NDCG,MRR Lei
    office2010每次启动都要配置的解决办法 Lei
  • 原文地址:https://www.cnblogs.com/lqcswy/p/11802673.html
Copyright © 2020-2023  润新知