【问题标题】:Swagger UI dosn't show upSwagger UI 不显示
【发布时间】:2020-06-10 11:20:07
【问题描述】:

我正在从事春季项目。首先,我添加到我的项目 swagger 中,并且我的 swagger ui 也显示了所有控制器,但是当我将 JWT 不记名令牌添加到我的项目中时。不知何故,我无法进入招摇用户界面。它一直在我的浏览器中显示以下窗口。我有什么想法可以解决这个问题吗?

在 pom.xml 中

 <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.9.2</version>
    </dependency>



  <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>

我目前还没有的 swagger 其他配置类的配置类

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SpringFoxConfig {

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage("uzb.ofb.reportgen.controller"))
                .paths(PathSelectors.any())
                .build();
    }
}

更新

更新2

在下面的代码之后

@Configuration
@EnableSwagger2
public class SpringFoxConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/swagger-ui.html/")
                .addResourceLocations("classpath:/META-INF/resources/");

        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage("uzb.ofb.reportgen.controller"))
                .paths(PathSelectors.any())
                .build();
    }
}

【问题讨论】:

  • 考虑使用springdoc-ui。 SpringFox 已经严重过时了。

标签: java spring jwt swagger swagger-ui


【解决方案1】:

您应该扩展WebMvcConfigurerAdapter 并覆盖方法:

        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/swagger-ui.html")
                    .addResourceLocations("classpath:/META-INF/resources/");

            registry.addResourceHandler("/webjars/**")
                    .addResourceLocations("classpath:/META-INF/resources/webjars/");
        }

【讨论】:

  • WebMvcConfigurerAdapter 自 Spring Framework 5.0 起已弃用。最好实现WebMvcConfigurer
  • 谢谢先生的回答,但没有帮助
【解决方案2】:

您在项目中使用 spring-security 吗?尝试在您的安全配置中覆盖 configure() 方法:

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

【讨论】:

    【解决方案3】:

    Swagger UI 由 webjar 提供,您需要配置 Spring MVC。

        @Configuration
        @EnableSwagger2
        public class WebConfig implements WebMvcConfigurer {
    
            @Override
            public void addResourceHandlers(ResourceHandlerRegistry registry) {
                registry.addResourceHandler("/swagger-ui.html")
                        .addResourceLocations("classpath:/META-INF/resources/");
    
                registry.addResourceHandler("/webjars/**")
                        .addResourceLocations("classpath:/META-INF/resources/webjars/");
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2013-11-17
      • 1970-01-01
      • 2021-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-07-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多