转载

Spring Boot 集成 Swagger2 构建 RESTful API 文档

引入依赖

pom.xml 中添加 io.springfox:springfox-swagger2io.springfox:springfox-swagger-ui 依赖

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

相关配置

创建 Swagger2Config 配置类

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;

@Configuration
public class Swagger2Config {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("{Controler 扫描路径}"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("{标题}")
                .description("{描述}")
                .termsOfServiceUrl("{网址}")
                .version("{版本号}")
                .build();
    }
}

注意: 需要在 RequestHandlerSelectors.basePackage 参数中设置 Controller 包路径,否则生成的文档扫描不到接口

Application 中添加 @EnableSwagger2 注解

@EnableSwagger2
@SpringBootApplication
public class ServiceAdminApplication {
    public static void main(String[] args) {
        SpringApplication.run(ServiceAdminApplication.class, args);
    }
}

使用 Swagger2

Controller 中的请求接口里加入以下常用 Swagger 注解

@RestController
@RequestMapping(value = "/api/v2/user")
@Api(tags = "用户管理")
public class UserController() {

    @ApiOperation(value = "分页查询用户列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", required = true, dataType = "int", paramType = "path"),
            @ApiImplicitParam(name = "pageSize", value = "页数", required = true, dataType = "int", paramType = "path"),
            @ApiImplicitParam(name = "UserJson", value = "对象 JSON 字符串", required = false, dataTypeClass = String.class, paramType = "json")
    })
    @RequestMapping(value = "page/{pageNum}/{pageSize}", method = RequestMethod.GET)
    public BaseResult page(
        @PathVariable(required = true) int pageNum, 
        @PathVariable(required = true) int pageSize,
        @RequestParam(required = false) String UserJson
    ){
        return null;
    }
}

Swagger 注解使用说明

Controller
HTTP
HTTP
API

启动项目,访问 Swagger 地址: http:// {ip}:{port}/swagger-ui.html

  • 文章作者:彭超
  • 本文首发于个人博客: https://antoniopeng.com/2019/07/28/springboot/SpringBoot%E9%9B%86%E6%88%90Swagger2%E6%9E%84%E5%BB%BARESTfulAPI%E6%96%87%E6%A1%A3/
  • 版权声明:本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 彭超 | Blog !
原文  https://segmentfault.com/a/1190000022348303
正文到此结束
Loading...