【问题标题】:Replace @EnableSwagger2 after update to latest version更新到最新版本后替换 @EnableSwagger2
【发布时间】:2020-10-13 19:34:54
【问题描述】:

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

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

【问题讨论】:

  • 不要使用未完全发布的2.10.0版本 github.com/springfox/springfox/issues/3336 ,github.com/springfox/springfox/issues/3335
  • @EnableSwagger2WebMvc 现在也已弃用!

标签: spring swagger swagger-2.0 springfox


【解决方案1】:
@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();
    }

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

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

这应该对我有帮助

【讨论】:

  • 我已经使用 @EnableSwagger2WebMvc 服务器启动,但我无法在 http://localhost:8090/swagger-ui.html 上加载 Swagger UI。我得到SpringSecurity 登录页面,在输入有效凭据后仍然没有加载,并一次又一次地要求凭据
  • 错误是WARN [2020-06-24T10:20:00.527+0530] servlet.PageNotFound ||${fallback:user}|No mapping for GET /swagger-ui.html
  • 我有 WebsecurityConfigurerAdapter 你提到的
  • 当我遇到同样的错误时,这对我有用
  • 这对我来说很奇怪!!升级到最新版本后,即2.10.5,它给了我一个相同的安全对话框
【解决方案2】:

@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>

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

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

【讨论】:

  • 看来@EnableSwagger2 又在springfox-swagger2 v.3.0.0 工作了!
猜你喜欢
  • 2023-02-02
  • 2022-06-17
  • 1970-01-01
  • 2019-08-12
  • 2017-01-21
  • 2020-06-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多