Search code examples
javaspring-bootrestswagger-uispringfox

Swagger UI does not get generated when WebMvcConfigurationSupport is present


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?


Solution

  • 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());
      }
    }