springboot 集成swagger-ui

springboot 集成swagger-ui

  1. 引入maven依赖
        <!-- swagger -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.5.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.5.0</version>
        </dependency>
  1. 配置swagger
public class SwaggerApi {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                //为当前包路径
//                .apis(RequestHandlerSelectors.basePackage("com.yunxi.zhang.admin..project.system.controller"))
// 扫描注解
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build();
//        return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)).build();
    }
    //构建 api文档的详细信息函数,注意这里的注解引用的是哪个
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                //页面标题
                .title("xxx")
                //创建人
                .contact(new Contact("xx", null, null))
                //版本号
                .version("1.0")
                //描述
                .description("API 描述")
                .build();
    }
}

3.访问 /swagger-ui.index.html

posted @ 2020-02-16 13:20  it_dog_zhang  阅读(107)  评论(0)    收藏  举报