Reputation: 373
I'm trying to configure the org.sprindoc-openapi library in Spring Boot 3. In my application, I need to extend the WebMvcConfigurationSupport class for some custom configurations that I need to apply to my REST API, and here comes the problem. If I extend the WebMvcConfigurationSupport class, I can access /v3/api-docs, but I can no longer access /swagger-ui.html to browse the APIs. If I remove the extension of the WebMvcConfigurationSupport class, everything works correctly, but I need my configuration, so I can't remove it.
Checking in the Actuator, the /swagger-ui.html path is mapped, but if I try to access it, I receive a 404 page not found error. I'm sharing my configuration.
@Configuration
@EnableSpringDataWebSupport
public class WebConfig extends WebMvcConfigurationSupport {
@Override
protected RequestMappingHandlerMapping createRequestMappingHandlerMapping() {
return new VersionedHandlerMapping();
}
@Override
protected void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
argumentResolvers.add(new PageableHandlerMethodArgumentResolver());
}
}
and this is the org.springdoc config:
@Configuration
@ComponentScan(basePackages = { "org.springdoc" })
public class OpenApiConfig {
@Bean
OpenAPI coreMicroserviceOpenAPI() {
return new OpenAPI()
.info(new Info().title("xxxx")
.description("desc")
.version("1.0"));
}
}
below the dependencies used:
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-api</artifactId>
<version>2.1.0</version>
</dependency>
Thank you all in advance. I hope that someone among you has found a solution to the problem.
Upvotes: 1
Views: 1528
Reputation: 1
First, this problem comes from ignoring Spring Web Mvc auto-configuration. So we can solve it provided the swagger resource path
to the Web Mvc Configuration.
At your class of extends WebMvcConfiugrationSupport
, you just override the addResourceHandlers
method with SwaggerIndexTransformer
component.
@Configuration
@RequiredArgsConstructor
public class AuthConfiguration extends WebMvcConfigurationSupport {
private final SwaggerIndexTransformer swaggerIndexTransformer; // <- ADD THIS
// ADD THIS METHOD
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/swagger-ui*/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/")
.resourceChain(false)
.addTransformer(swaggerIndexTransformer);
}
}
You can use WebMvcConfigurer
and WebMvcRegistrations
. Unlike WebMvcConfigurationSupport
, these interfaces may operate Spring MVC auto-configuration. This means that MVC auto-configuration can access the default configuration of spring doc!
@Configuration
@RequiredArgsConstructor
//public class AuthConfiguration extends WebMvcConfigurationSupport { // REPLACE
public class AuthConfiguration implements WebMvcConfigurer, WebMvcRegistrations {
}
My Project Setting
you can see my article of same problem. https://blog.naver.com/chgy2131/223416148854
Upvotes: 0
Reputation: 44466
The following configuration works for me but you have to use the 2.X version of the springdoc-openapi supporting OpenAPI 3.0 with Spring Boot 3:
Spring Boot dependencies:
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>3.0.2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
Open API dependency:
Don't get fooled. The 2.X baseline and the whole springdoc-openapi library support the Open API 3.0 specification.
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.0.0</version>
</dependency>
Spring Boot application
Make sure the @RestController beans are properly component-scanned.
@SpringBootApplication(scanBasePackages = "com.company")
public class Application {
}
Web configuration class
Use WebMvcConfigurerAdapter
instead of WebMvcConfigurationSupport
.
@Configuration
public class WebMvcConfiguration extends WebMvcConfigurerAdapter {
// code
}
Once you include this configuration, reimport the Maven project, build and run the application, you can access the Swagger UI here: http://localhost:8080/swagger-ui/index.html
Upvotes: 0