学习易懂版
package com.xiaosong.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.ArrayList;
@Configuration
@EnableSwagger2
public class SwaggerConfig{
/*配置了swagger的Docket的Bean实例*/
@Bean
public Docket docket(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
// .enable(false) //是否启用swagger true 表示启用 false表示停用(swagger不能在浏览器中访问) 默认是true
.select()
/*RequestHandlerSelectors 配置要扫描接口的方式
RequestHandlerSelectors.basePackage("com.xiaosong.controller") 指定要扫描的包
RequestHandlerSelectors.any() 扫描全部的包
RequestHandlerSelectors.none() 不扫描
RequestHandlerSelectors.withMethodAnnotation(GetMapping.class) 扫描方法上的注解
withClassAnnotation(RestController.class) 扫描类上的注解,参数是一个注解的反射对象*/
.apis(RequestHandlerSelectors.basePackage("com.xiaosong.controller"))
/*PathSelectors.ant("/xiaosong/**")过滤扫描路劲*/
// .paths(PathSelectors.ant("/xiaosong/**"))
.build();
}
/*配置swagger信息等于apiInfo*/
private ApiInfo apiInfo(){
// 作者信息
Contact contact = new Contact("@程序员贵哥","https://blog.csdn.net/m0_69114709?spm=1000.2115.3001.5343","203****78@qq.com");
return new ApiInfo(
"一叶十三刺", "个人信息","v1.0","http:localhost:8080//swagger-ui.html",contact,"Apache 2.0","http://"
,new ArrayList<>()
);
}
学习开发版
package com.xiaosong.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.ArrayList;
@Configuration
@EnableSwagger2
public class SwaggerConfig{
/*配置了swagger的Docket的Bean实例*/
@Bean
public Docket docket(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.xiaosong.controller")) // com.xiaosong.controller 要扫描包的路劲
.build();
}
/*配置swagger信息等于apiInfo*/
private ApiInfo apiInfo(){
// 作者信息
Contact contact = new Contact("@程序员贵哥","https://blog.csdn.net/m0_69114709?spm=1000.2115.3001.5343","203****78@qq.com");
return new ApiInfo(
"一叶十三刺", "个人信息","v1.0","http:localhost:8080//swagger-ui.html",contact,"Apache 2.0","http://"
,new ArrayList<>()
);
}
注意:我曾经也犯错:在浏览器访问swagger-ui的时候要去springfox-swagger-ui看swagger-ui.html所在的路径;提示:版本不同,所在的路径不同,我的是2.9.2版,swagger-ui.html在springfox-swagger-ui-2.9.2.jar/META_INT/resources路径下