spring cloud / spring boot 专题

Spring Boot & Swagger

2019-01-31  本文已影响61人  十毛tenmao

本文接着前面的继续,介绍如何快速接入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>
@Configuration
@EnableSwagger2
public class SwaggerConfig {                                    
    @Bean
    public Docket api() { 
        return new Docket(DocumentationType.SWAGGER_2)  
          .select()                                  
          .apis(RequestHandlerSelectors.any())              
          .paths(PathSelectors.any())                          
          .build();                                           
    }
}

展示

Swagger文档展示

已经实现了最简单的接入了


其他配置

@Configuration
@EnableSwagger2
@Profile("dev")
public class SwaggerConfig {                                    

测试环境启动命令如下

java -jar boot-web-2.1.2.RELEASE.jar -Dspring.profiles.active=dev
@Bean
public Docket api() {
    return new Docket(DocumentationType.SWAGGER_2)
            .select()
            .apis(RequestHandlerSelectors.basePackage("com.tenmao.mvc.controller"))
            .paths(PathSelectors.ant("/api/*"))
            .build();
}
@Bean
public Docket api() {
    return new Docket(DocumentationType.SWAGGER_2)
            .select()
            .apis(RequestHandlerSelectors.basePackage("com.tenmao.mvc.controller"))
            .paths(PathSelectors.ant("/api/*"))
            .build()
            // 这里有一个调用
            .apiInfo(apiInfo());
}

private ApiInfo apiInfo() {
    return new ApiInfo(
            "十毛API",
            "十毛API文档",
            "1.0",
            "Terms of service",
            new Contact("tenmao", "https://www.jianshu.com/u/518bde83a9bc", "tenmao@example.com"),
            "License of API", "API license URL", Collections.emptyList());
}

SpringBoot入门系列

参考

上一篇下一篇

猜你喜欢

热点阅读