Background
我遇到了一些Swagger配置问题,所以我试图通过复制一些简单的示例配置来修复它们 .
他们有这个:
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
}
Setup
我以前在我的_2534402中有这个bean Docket但是它们似乎在自己的类中有配置 . 我想匹配他们的设置,所以我在与Application.java相同的位置创建了一个SwaggerConfiguration.java文件 .
然后我让SwaggerConfiguration.java包含以下代码:
@Configuration
@EnableSwagger2
public class SwaggerConfiguration {
@Bean
public Docket Api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build()
.pathMapping("/")
.apiInfo(apiInfo());
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Service API")
.build();
}
}
我的Application.Java包含以下代码:
@SpringBootApplication
@EnableTransactionManagement
@EnableSwagger2
@ComponentScan({"myproject.request"})
public class Application {
public static void main(String[] args) {
new SpringApplication(Application.class).run(args);
}
}
Question: 如何将此SwaggerConfiguration.java绑定到我的项目中? (导入它)
他们在这里做到了我相信:"Import the bean by adding the package name (if it’s missing) in the component-scan tag of the existing rest-dispatcher-servlet.xml" - http://www.baeldung.com/swagger-2-documentation-for-spring-rest-api
但是,我没有那个serverlet,也没有除了maven的pom.xml之外的任何xml文件 .
Question Rephrased
我的SwaggerConfig.java只是坐在我的项目中,但没有被任何东西使用或导入 . apiInfo 例如没有在Swagger UI上设置任何内容 . 我该如何使用它 .
UPDATE 1
有人建议删除 @ComponentScan({"myproject.request"}) ,但是当我的构建失败并打印出来时:
Description:
Field actionRepository in myproject.service.ActionServiceImpl required a bean of type 'myproject.repository.ActionRepository' that could not be found.
Action:
Consider defining a bean of type 'myproject.repository.ActionRepository' in your configuration.
UPDATE 2
我已将SwaggerConfiguration更改为SwaggerConfig
@Configuration
@EnableSwagger2
public class SwaggerConfig {
/*
@Bean
public Docket Api() {
return new Docket(DocumentationType.SWAGGER_2).select()
.apis(RequestHandlerSelectors.basePackage("myproject.controller")).paths(regex("/api/*"))
.build().apiInfo(apiInfo());
}
*/
@Bean
public Docket Api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("ibm.controller"))
.paths(regex("/api/*"))
.build()
.pathMapping("/")
.apiInfo(apiInfo());
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Service API")
.description("API for Service REST operations")
}
}
但我有同样的问题
UPDATE 3 more background