SpringBoot 集成 Swagger

Updated on with 0 views and 0 comments

添加依赖

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

配置不同环境是否启用

在相应配置文件中添加(以yml为例)

swagger:
  enable: true

创建配置类

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Value("${swagger.enable}")
    private boolean enableSwagger;

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(new ApiInfoBuilder()
                        .title("面试题库_接口文档")
                        .version("1.0")
                        .build())
                .enable(enableSwagger)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }
}

文档相关描述配置

Controller 描述信息

@Api(tags = "类别相关接口")
@RestController
@RequestMapping("/cat")
public class CategoryController{
}

接口 描述信息

    @ApiOperation("添加类别")
    @PostMapping("/add")
    public ResultBean addCategory(@Valid Category category) {
        return categoryService.addCategory(category);
    }

接口过滤

@ApiIgnore

实体类 描述信息

@ApiModel("类别实体")
@Data
public class Category implements Serializable {

    private static final long serialVersionUID = 1L;

    @ApiModelProperty("类别ID")
    private Integer id;

    @ApiModelProperty("类别名")
    private String name;
}

访问

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