23

I migrated to latest springfox-swagger2 version 2.10.0 but looks like @EnableSwagger2 is deprecated.

What annotation should I use in order to enable Swagger into Spring Boot project? @EnableSwagger2WebMvc?

Helen
  • 87,344
  • 17
  • 243
  • 314
Peter Penzov
  • 1,126
  • 134
  • 430
  • 808

2 Answers2

31

@EnableSwagger2 was removed in swagger 2.10.x, but from 3.x.x it is there again.

@EnableSwagger2WebMvc is deprecated in 3.0.0+

Funny but true :)


Optionally you can use following dependency with Spring 5 MVC

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-boot-starter</artifactId>
    <version>3.0.0</version>
</dependency>

and

  • remove explicit dependencies on springfox-swagger2
  • remove the @EnableSwagger2 annotations
  • add the springfox-boot-starter dependency

see: https://github.com/springfox/springfox

Jacob van Lingen
  • 8,989
  • 7
  • 48
  • 78
vaquar khan
  • 10,864
  • 5
  • 72
  • 96
5
@Configuration
@EnableSwagger2WebMvc
@Import({SpringDataRestConfiguration.class, BeanValidatorPluginsConfiguration.class})
public class ApplicationSwaggerConfig {

    @Bean
    public Docket schoolApi() {
        return new Docket(DocumentationType.SWAGGER_2).
                select().
                apis(RequestHandlerSelectors.basePackage("com.example.SampleProject")).
                paths(PathSelectors.any()).
                build();
    }

For the other case pertaining to spring security checks, you can make your securityconfiguration class to extend WebsecurityConfigurerAdapter and then you can implement below method -

 @Override public void configure(WebSecurity web) throws Exception {
      web.ignoring().antMatchers( "/v2/api-docs", "/swagger-resources/**", "/configuration/ui","/configuration/security", "/swagger-ui.html");
      
      }

This should help I guess

Tushar Mahajan
  • 2,044
  • 1
  • 7
  • 18
  • 1
    I have used the `@EnableSwagger2WebMvc` server started but I am unable to load swagger UI on `http://localhost:8090/swagger-ui.html`. I am getting `SpringSecurity` login page, after entering the valid credential still it is not loading, and asking for credential again and again – SSK Jun 24 '20 at 04:48
  • Error is `WARN [2020-06-24T10:20:00.527+0530] servlet.PageNotFound ||${fallback:user}|No mapping for GET /swagger-ui.html` – SSK Jun 24 '20 at 04:51
  • I have `WebsecurityConfigurerAdapter ` in place as you mentioned – SSK Jun 24 '20 at 04:55
  • this had worked for me when I was getting kinda same errors – Tushar Mahajan Jun 24 '20 at 05:00
  • 2
    It's strange to me!!. After upgrade to latest version i.e `2.10.5`, it is giving me a same security dialog box – SSK Jun 24 '20 at 05:02
  • Anything else that I can try to get rid of this? – SSK Jun 24 '20 at 05:04
  • Found that this is an issue and latest release `2.10` is not ready to use https://github.com/springfox/springfox/issues/3336 https://github.com/springfox/springfox/issues/3335 – SSK Jun 24 '20 at 11:58