Reputation: 1367
I am trying to generate Swagger UI in my spring boot project. The JSON API docs are generated, however Swagger UI doesn't, at least I am getting 404 when I enter the swagger ui address. My dependencies:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>3.0.0</version>
</dependency>
However, when I remove this config (and do nothing else):
@Configuration
public class WebConfig extends WebMvcConfigurationSupport {
@Override
protected void addFormatters(FormatterRegistry registry) {
registry.addConverter(new SomeEnumConverter());
registry.addConverter(new AnotherEnumConverter());
}
}
The Swagger UI works. Does Swagger UI have a problem with converters? Is there a workaround for the problem?
Upvotes: 2
Views: 1691
Reputation: 75
For anyone still looking for this, I got it running using WebMvcConfigurationSupport
without the need of @EnableWebMvc
.
I'm using OpenAPI 3.0, not Springfox, with following Dependency:
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.6.8</version>
</dependency>
I initially just got the JSON and a 404 on the UI.
What ultimately worked was adding the right resource handlers to my WebConfig
and configuring my SecurityConfig
to allow unrestricted access for everyone.
The resource handlers in my WebConfig
:
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/swagger-ui/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/swagger-ui/4.10.3/swagger-initializer.js")
.addResourceLocations("classpath:/META-INF/resources/webjars/swagger-ui/4.10.3/index.html")
.addResourceLocations("classpath:/META-INF/resources/webjars/swagger-ui/4.10.3/swagger-ui.css")
.addResourceLocations("classpath:/META-INF/resources/webjars/swagger-ui/4.10.3/index.css")
.addResourceLocations("classpath:/META-INF/resources/webjars/swagger-ui/4.10.3/swagger-ui-bundle.js")
.addResourceLocations("classpath:/META-INF/resources/webjars/swagger-ui/4.10.3/swagger-ui-standalone-preset.js");
}
Important was to add each one specifically (with version and everything, ** didn't work). Otherwise it wouldn't run in the cloud.
I also needed to add the paths "/v3/api-docs/**", "/swagger-ui/**"
my SecurityConfig
to allow access to these resources. So in my case I added those here:
@Override
protected void configure(HttpSecurity httpSecurity) throws Exception {
httpSecurity.addFilterBefore(...
.authorizeRequests().antMatchers("/v3/api-docs/**", "/swagger-ui/**", "...")...
}
and:
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers(HttpMethod.GET, "/v3/api-docs/**", "/swagger-ui/**", "...");
}
Upvotes: 3
Reputation: 1072
You need to add the @EnableWebMvc annotation
@EnableWebMvc
@Configuration
public class WebConfig extends WebMvcConfigurationSupport {
@Override
protected void addFormatters(FormatterRegistry registry) {
registry.addConverter(new SomeEnumConverter());
registry.addConverter(new AnotherEnumConverter());
}
}
But I think the official way to configure WebMvc now is implementing WebMvcConfigurer. You can see several examples in the official docs. I think it does not require @EnableWebMvc if you use Spring Boot.
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
protected void addFormatters(FormatterRegistry registry) {
registry.addConverter(new SomeEnumConverter());
registry.addConverter(new AnotherEnumConverter());
}
}
Upvotes: 5