更新到最新版本后替换@EnableSwagger2

Pet*_*zov 8 spring swagger swagger-2.0 springfox

我迁移到最新springfox-swagger2版本,2.10.0但看起来@EnableSwagger2已被弃用。

为了在 Spring Boot 项目中启用 Swagger,我应该使用什么注释?@EnableSwagger2WebMvc?

vaq*_*han 14

@EnableSwagger2 在 swagger 2.10+ 中被弃用

@EnableSwagger2WebMvc 在 3.0.0+ 中被弃用

有趣但真实:)

现在您可以在 Spring 5 MVC 中使用以下依赖项

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-boot-starter</artifactId>
    <version>3.0.0</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)

  • 删除对 springfox-swagger2 的显式依赖
  • 删除@EnableSwagger2 注释
  • 添加 springfox-boot-starter 依赖

见:https : //github.com/springfox/springfox

  • 看起来“@EnableSwagger2”在“springfox-swagger2 v.3.0.0”中再次工作了! (3认同)

Tus*_*jan 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();
    }
Run Code Online (Sandbox Code Playgroud)

对于与 Spring 安全检查有关的其他情况,您可以使您的安全配置类扩展 WebsecurityConfigurerAdapter,然后您可以实现以下方法 -

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

我想这应该有帮助

  • 这对我来说很奇怪!升级到最新版本即“2.10.5”后,它给了我一个相同的安全对话框 (2认同)