Java 如何导入Swagger配置
背景 我遇到了一些招摇过市的配置问题,所以我试图通过复制一些简单示例的配置来修复它们 我正在阅读本教程: 他们有:Java 如何导入Swagger配置,java,swagger,Java,Swagger,背景 我遇到了一些招摇过市的配置问题,所以我试图通过复制一些简单示例的配置来修复它们 我正在阅读本教程: 他们有: @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2)
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
}
设置
我以前在我的公共类应用程序中有这个bean摘要,但它们似乎在自己的类中有配置。我想匹配他们的设置,所以我在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();
}
}
My Application.Java包含以下代码:
@SpringBootApplication
@EnableTransactionManagement
@EnableSwagger2
@ComponentScan({"myproject.request"})
public class Application {
public static void main(String[] args) {
new SpringApplication(Application.class).run(args);
}
}
问题:如何将这个SwaggerConfiguration.java绑定到我的项目中?(导入)
我相信他们会在这里这样做:“通过在现有rest dispatcher servlet.xml的组件扫描标记中添加包名(如果缺少包名)来导入bean”——
但是,除了maven的pom.xml之外,我没有那个serverlet,也没有任何xml文件
问题重新表述
我的SwaggerConfig.java只是放在我的项目中,没有被任何东西使用或导入<例如,code>apiInfo
没有在Swigger UI上设置任何内容。我如何使用它
更新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.
更新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")
}
}
但我也有同样的问题
更新3个更多背景信息
这个问题多少与这个问题有关
马文
伊奥·斯普林福克斯
springfox-Swagger 2
2.6.1
编译
伊奥·斯普林福克斯
springfox招摇过市用户界面
2.6.1
编译
招摇过市
http://<servername>:<Port>/swagger-ui.html
http://:/swagger-ui.html
除了将SwaggerConfiguration
重命名为SwaggerConfig
并在PathSelectors
上使用regex之外,我看不出您与我有什么不同。我缺少什么?basePackage(“com.vk.test.controller”).path(regex(“/api/apiPath.*))应该是(“/api/api”)路径。*”)可以是(“myproject/controller”)路径,也可以是字面上的(“/api/api”)路径。*”)不,它应该是控制器上的api URI,如-@RestController@RequestMapping(value=“/api/apiPath“@Api(value=“onlinestore”,description=“dgh kjgj kbkjh”)好的,我已经添加了您的更改,但它们似乎并不是在“调用招摇过市”,请在删除应用程序中的@ComponentScan({“myproject.request”})
后再次查看更新2Try。注释@SpringBootApplication
应该考虑所有这些。@LucianovanderVeekens请参阅我的更新1
<!-- Swagger -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
<scope>compile</scope>
</dependency>
<!-- Swagger UI -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
<scope>compile</scope>
</dependency>
http://<servername>:<Port>/swagger-ui.html