随笔(十八)『SpringBoot 整合 Swagger2』

发布时间 2023-06-12 18:24:15作者: 小昕昕

1、添加Swagger2依赖

        <!--  swagger2  -->
        <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>

2、开启Swagger2(这里选择在配置类开启,可直接在启动类开启)

/**
 * Swagger2配置类
 */
@Configuration
@EnableSwagger2 // 开启Swagger2
public class SwaggerConfig {
    @Bean
    public Docket createDocket() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("标题...")
                .description("描述...")
                .version("1.0.0")
                .build();
    }
}

3、访问swagger-ui.html

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