英文:
Swagger OpenAPI 3.0 - Springdoc - GroupedOpenApi not working in Spring MVC
问题
以下是翻译好的内容:
我的应用程序是Spring MVC而不是Spring Boot。
我正在使用springdoc-openapi-ui 1.4.4。
此外,我在我的一个@Configuration类中添加了以下导入:
@Import({
org.springdoc.core.SpringDocConfigProperties.class, org.springdoc.core.SpringDocConfiguration.class,
org.springdoc.webmvc.core.SpringDocWebMvcConfiguration.class,
org.springdoc.webmvc.core.MultipleOpenApiSupportConfiguration.class,
org.springdoc.core.SwaggerUiConfigProperties.class, org.springdoc.core.SwaggerUiOAuthProperties.class,
org.springdoc.webmvc.ui.SwaggerConfig.class, org.springdoc.core.CacheOrGroupedOpenApiCondition.class,
org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration.class })
并且按照以下方式实现了这些beans:
@Bean
public GroupedOpenApi publicApi() {
return GroupedOpenApi.builder()
.group("user")
.pathsToExclude("/api/v2/**", "/v2/**")
.pathsToMatch("/api/v1/**", "/v1/**")
.build();
}
@Bean
public GroupedOpenApi adminApi() {
return GroupedOpenApi.builder()
.group("admin")
.pathsToExclude("/api/v1/**", "/v1/**")
.pathsToMatch("/api/v2/**", "/v2/**")
.build();
}
@Bean
public OpenAPI customOpenAPI() {
return new OpenAPI()
.components(new Components())
.info(new Info()
.title("titleI")
.version("1.0.0"));
}
但是,当我尝试访问 http://localhost:8080/MyApp/v3/api-docs/user
时,
会返回404错误。
但是,我可以在 http://localhost:8080/MyApp/v3/api-docs
中看到列出了所有的API。
同样,可以在 http://www.onlykalu.com:81/MyApp/swagger-ui/index.html?configUrl=/MyApp/v3/api-docs/swagger-config
中看到API,但是没有定义下拉框。
如果我在application.properties
中添加以下内容:
springdoc.group-configs[0].group=user
springdoc.group-configs[1].group=admin
那么我会得到如下所示的结果:
我做错了什么?
我是否需要导入其他类以使组功能正常工作?
谢谢。
英文:
My Application is Sping MVC not boot.
I'm using springdoc-openapi-ui 1.4.4
also, I added the following imports to one of my @configuration class;
org.springdoc.core.SpringDocConfigProperties.class, org.springdoc.core.SpringDocConfiguration.class,
org.springdoc.webmvc.core.SpringDocWebMvcConfiguration.class,
org.springdoc.webmvc.core.MultipleOpenApiSupportConfiguration.class,
org.springdoc.core.SwaggerUiConfigProperties.class, org.springdoc.core.SwaggerUiOAuthProperties.class,
org.springdoc.webmvc.ui.SwaggerConfig.class, org.springdoc.core.CacheOrGroupedOpenApiCondition.class,
org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration.class })
and implement the beans as below
@Bean
public GroupedOpenApi publicApi() {
return GroupedOpenApi.builder()
.group("user")
.pathsToExclude("/api/v2/**", "/v2/**")
.pathsToMatch("/api/v1/**", "/v1/**")
.build();
}
@Bean
public GroupedOpenApi adminApi() {
return GroupedOpenApi.builder()
.group("admin")
.pathsToExclude("/api/v1/**", "/v1/**")
.pathsToMatch("/api/v2/**", "/v2/**")
.build();
}
@Bean
public OpenAPI customOpenAPI() {
return new OpenAPI()
.components(new Components())
.info(new Info()
.title("titleI")
.version("1.0.0"));
}
But when I try http://localhost:8080/MyApp/v3/api-docs/user
gives me 404.
but I get all the APIs listed in http://localhost:8080/MyApp/v3/api-docs
Also it comes in http://www.onlykalu.com:81/MyApp/swagger-ui/index.html?configUrl=/MyApp/v3/api-docs/swagger-config
also without definition drop down.
if I add
springdoc.group-configs[0].group=user
springdoc.group-configs[1].group=admin
to application.properties then I get
What I'm doing wrong?
Do I need to import any other class to get group functionality work?
Thanks
答案1
得分: 5
你的问题在这里也有答案:
如前所述,您只需将Beans定义放入专用配置类中,并将其添加到springdoc-openapi的Beans导入中,以便相同的机制加载Beans。
这是正确的Beans配置:
@Configuration
public class GroupsConfiguration {
@Bean
public GroupedOpenApi publicApi() {
return GroupedOpenApi.builder().group("user").pathsToExclude("/api/v2/**").pathsToMatch("/api/v1/**").build();
}
@Bean
public GroupedOpenApi adminApi() {
return GroupedOpenApi.builder().group("admin").pathsToExclude("/api/v1/**").pathsToMatch("/api/v2/**").build();
}
@Bean
public OpenAPI customOpenAPI() {
return new OpenAPI().components(new Components()).info(new Info().title("Spring MVC REST API")
.contact(new Contact().name("Rasika Kaluwalgoda")).version("1.0.0"));
}
}
以及与GroupsConfiguration一起使用的springdoc-openapi导入:
@Configuration
@Import({ org.springdoc.core.SpringDocConfigProperties.class,
org.springdoc.webmvc.core.MultipleOpenApiSupportConfiguration.class,
org.springdoc.core.SpringDocConfiguration.class, org.springdoc.webmvc.core.SpringDocWebMvcConfiguration.class,
SwaggerUiConfigParameters.class, SwaggerUiOAuthProperties.class,
org.springdoc.core.SwaggerUiConfigProperties.class, org.springdoc.core.SwaggerUiOAuthProperties.class,
org.springdoc.webmvc.ui.SwaggerConfig.class, GroupsConfiguration.class,
org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration.class })
public class SwaggerConfig {
}
英文:
Your question has been also answered here:
As mentioned, you just had to put your Beans definition inside a dedicated configuration class, and add it to the springdoc-openapi Beans imports, so that the same mechanism loads the Beans.
This is the correct beans configuration:
@Configuration
public class GroupsConfiguration {
@Bean
public GroupedOpenApi publicApi() {
return GroupedOpenApi.builder().group("user").pathsToExclude("/api/v2/**").pathsToMatch("/api/v1/**").build();
}
@Bean
public GroupedOpenApi adminApi() {
return GroupedOpenApi.builder().group("admin").pathsToExclude("/api/v1/**").pathsToMatch("/api/v2/**").build();
}
@Bean
public OpenAPI customOpenAPI() {
return new OpenAPI().components(new Components()).info(new Info().title("Spring MVC REST API")
.contact(new Contact().name("Rasika Kaluwalgoda")).version("1.0.0"));
}
}
And the springdoc-openapi imports together with the GroupsConfiguration :
@Configuration
@Import({ org.springdoc.core.SpringDocConfigProperties.class,
org.springdoc.webmvc.core.MultipleOpenApiSupportConfiguration.class,
org.springdoc.core.SpringDocConfiguration.class, org.springdoc.webmvc.core.SpringDocWebMvcConfiguration.class,
SwaggerUiConfigParameters.class, SwaggerUiOAuthProperties.class,
org.springdoc.core.SwaggerUiConfigProperties.class, org.springdoc.core.SwaggerUiOAuthProperties.class,
org.springdoc.webmvc.ui.SwaggerConfig.class, GroupsConfiguration.class,
org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration.class })
public class SwaggerConfig {
}
答案2
得分: 0
我之前配置过一些类似的内容。在我那个情况下,每个 API 版本都在不同的包中,所以我也像下面这样定义了 packageToScan
:
@Bean
public GroupedOpenApi publicApi1() {
return GroupedOpenApi.builder()
.group("user")
.pathsToExclude("/api/v2/**")
.pathsToMatch("/api/v1/**")
.packagesToScan("com.my.app.v1")
.build();
}
此外,我需要创建一个 WebMvcConfigurer
的 Bean(我只是扩展了配置类),并配置 PathMatchConfigurer
:
public void configurePathMatch(PathMatchConfigurer configurer) {
configurer
.addPathPrefix("api/v1", HandlerTypePredicate.forAnnotation(RestController.class)
.and(HandlerTypePredicate.forBasePackage("com.my.app.v1")))
.addPathPrefix("api/v2", HandlerTypePredicate.forAnnotation(RestController.class)
.and(HandlerTypePredicate.forBasePackage("com.my.app.v2")));
}
英文:
I configured some similar one time ago. In my case each api version was in separate package, so I define also packageToScan
like below:
@Bean
public GroupedOpenApi publicApi1() {
return GroupedOpenApi.builder()
.group("user")
.pathsToExclude("/api/v2/**")
.pathsToMatch("/api/v1/**")
.packagesToScan("com.my.app.v1")
.build();
}
Moreover I needed to create bean of WebMvcConfigurer
(I just extended configuration class) and configure PathMatchConfigurer
:
public void configurePathMatch(PathMatchConfigurer configurer) {
configurer
.addPathPrefix("api/v1", HandlerTypePredicate.forAnnotation(RestController.class)
.and(HandlerTypePredicate.forBasePackage("com.my.app.v1")))
.addPathPrefix("api/v2", HandlerTypePredicate.forAnnotation(RestController.class)
.and(HandlerTypePredicate.forBasePackage("com.my.app.v2")));
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论