spring boot 开启 swagger2 (IntelliJ IDEA)

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

添加 dependencies

    compile group: 'io.springfox', name: 'springfox-swagger2', version: '2.6.1'
    compile group: 'io.springfox', name: 'springfox-swagger-ui', version: '2.6.1'

添加 SwaggerConfig.java

@Configuration
@EnableSwagger2
public class SwaggerConfig {                                    
    @Bean
    public Docket api() { 
        return new Docket(DocumentationType.SWAGGER_2)  
          .select()                                  
          .apis(RequestHandlerSelectors.any())              
          .paths(PathSelectors.any())                          
          .build();                                           
    }
}

打开项目地址+swagger-ui.html即可,例如

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

如果要设置指定 package 下的 controller 和 自定义 apiInfo.则需要修改 SwaggerConfig.java

@Configuration
@EnableSwagger2
public class SwaggerConfig {

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

    private ApiInfo apiInfo() {
        return new ApiInfo("中国古琴", "接口文档", "1.0", "termsOfServiceUrl", new Contact("oldfeel", "http://yuncommunity.com", "[email protected]"), "license", "licenseUrl");
    }
}

转载于:https://my.oschina.net/oldfeel/blog/844005

你可能感兴趣的:(spring boot 开启 swagger2 (IntelliJ IDEA))