swagger原来的配置类如下
/**
* @EnableSwaggerBootstrapUI swagger页面优化 使用时账密验证
* @EnableSwagger2 开启swagger
* swagger.enable 项目动态启用swagger
* @Author yanhongbo
*/
@Configuration
@EnableSwagger2
@ConditionalOnProperty(name = "swagger.enable", havingValue = "true")
public class SwaggerConfig {
@Bean
public Docket docket() {
//可以通过此方式添加全局header
/* ParameterBuilder parameterBuilder = new
ParameterBuilder();
List<Parameter> heads = new ArrayList<>();
parameterBuilder.name("token")
.description("访问令牌")
.modelRef(new ModelRef("string"))
.parameterType("header")
.required(false)
.build();
heads.add(parameterBuilder.build());*/
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.genericModelSubstitutes(Response.class)
.forCodeGeneration(true)
.select()// 是否开启
.apis(RequestHandlerSelectors.basePackage("com.yuanma.user_boss.
controller")) // 扫描的路径包
.paths(PathSelectors.any())// 指定路径处理
PathSelectors.any()代表所有的路径
.build();
// .globalOperationParameters(heads);
}
private ApiInfo apiInfo(){
return new ApiInfoBuilder()
.title("edu_boss采用swagger2集成和YApi使用示例")
.description("采用swagger2")
.termsOfServiceUrl("")//项目的ip
.version("1.0")
.build();
}
}
解决方案:
需要去掉"@ConditionalOnProperty(name = "swagger.enable", havingValue = "true")"
package com.yuanma.common.swagger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
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;
@Configuration
@EnableSwagger2
public class SwaggerConfiguration {
@Bean
public Docket buildDocket() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(buildApiInfo())
.select()
// 要扫描的API(Controller)基础包
.apis(RequestHandlerSelectors.basePackage("com.yuanma"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo buildApiInfo() {
Contact contact = new Contact("九智新闻","","");
return new ApiInfoBuilder()
.title("九智新闻-平台管理API文档")
.description("九智新闻后台api")
.contact(contact)
.version("1.0.0").build();
}
}