Skip to content
Advertisement

Swagger OpenAPI 3.0 – Springdoc – GroupedOpenApi not working in Spring MVC

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

enter image description here

What I’m doing wrong?

Do I need to import any other class to get group functionality work?

Thanks

Advertisement

Answer

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 {

}
Advertisement