提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
—
localhost:8080显示
Unable to infer base url. This is common when using dynamic servlet registration or when the API is behind an API Gateway. The base url is the root of where all the swagger resources are served. For e.g. if the api is available at http://example.org/api/v2/api-docs then the base url is http://example.org/api/. Please enter the location manually:
http://localhost:8080/swagger-ui.html
一、注意@EnableSwagger2这个注解
二、就是检查端口8080是否改了
package com.swa.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.PathSelectors;
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;
import static springfox.documentation.service.ApiInfo.DEFAULT_CONTACT;
@Configuration
@EnableSwagger2 //开启Swagger2
public class SwaggerConfig {
//配置Swagger的Docket的bean实例
@Bean
public Docket docket(Environment environment){
//设置要显示的Swagger环境
Profiles profiles = Profiles.of("dev","test");
//通过environment.acceptsProfiles判断是否处在自己设定的环境当中
boolean flag = environment.acceptsProfiles(profiles);
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(this.apiInfo())
.enable(flag)//是否启动Swagger false 则Swagger不能在浏览器访问
.select()
//RequestHandlerSelectors.配置要扫描结构的方式
// basePackage:指定要扫描的包
//any():扫描全部
//none();不扫描
//withClassAnnotation:扫描类上的注解,参数是注解反射对象
//withMethodAnnotation:扫描方法上的注解
.apis(RequestHandlerSelectors.basePackage("com.swa.controller"))
//过滤路径
.paths(PathSelectors.ant("/"))
.build();
}
//配置Swagger信息=apiInfo
private ApiInfo apiInfo(){
//作者信息
Contact contact = new Contact("我的", "", "422249663@qq.com");
return new ApiInfo("我的SwaggerAPI文档",
"这个作者有点酷",
"v1.0",
"https://127.0.0.1/",
contact,
"Apache 2.0",
"http://www.apache.org/licenses/LICENSE-2.0",
new ArrayList());
}
}