spring boot 中使用swagger 来自动生成接口文档

时间:2022-06-13 05:44:04

1.依赖包

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

 

2.配置类

package com.shinyway.workflow;

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;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class Swagger2Configuration {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.shinyway"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("审批平台API")
                .description("审批平台API")
                .version("1.0")
                .build();
    }

}

 

3.controller中的方法上加上注解

 

    @ApiOperation(value="获取审批意见列表(html格式)", notes="通过业务号获取意见列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "processDefinitionKey", value = "流程定义标识", required = true, dataType = "String"),
            @ApiImplicitParam(name = "businessKey", value = "业务号", required = true, dataType = "String")
    })

 

4.如果一一些方法或者整个类不需要提供文档,可以在方法或者类上加上@ApiIgnore注解