【问题标题】:Getting 404 eror with Swager UISwagger UI 出现 404 错误
【发布时间】:2022-01-20 10:35:27
【问题描述】:

我有非启动 spring mvc 应用程序。当我点击 swagger-ui.html 或 swagger-ui/.我收到 404 错误。

我的配置类:

SpringFoxConfig:

@Configuration
@EnableSwagger2
public class SpringFoxConfig {}

SpringConfig

@ComponentScan("com.leverx.internship_project")
@Configuration
@EnableWebMvc
public class SpringConfig implements WebMvcConfigurer {
  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/swagger-ui.html")
        .addResourceLocations("classpath:src/main/resources");
    registry.addResourceHandler("/webjars/**")
        .addResourceLocations("classpath:src/main/resources");
  }
}

依赖关系:

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


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

【问题讨论】:

  • 您可以尝试仅将 &lt;dependency&gt;&lt;groupId&gt;io.springfox&lt;/groupId&gt; &lt;artifactId&gt;springfox-boot-starter&lt;/artifactId&gt;&lt;version&gt;3.0.0&lt;/version&gt;&lt;/dependency&gt; 作为依赖项而不是您提供的两个吗?此外,您需要通过 /swagger-ui/ 或 /swagger-ui/index.html 访问它。 swagger-ui.html 不再可用,afaik
  • 另外,我认为您不需要@EnableSwagger2,也不需要定义addResourceHandlers。只需创建一个Docket 类型的bean,例如@Bean public Docket api() { return new Docket(DocumentationType.OAS_30) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.any()) .paths(PathSelectors.any()) .build(); }
  • @AndreiSfat 不起作用...
  • 您是否在配置中使用了 server.servlet.context-path?正在使用什么?弹簧靴?
  • @AndreiSfat 我不使用spring boot,也没有使用任何配置,除了我发布的配置

标签: java spring spring-mvc swagger springfox


【解决方案1】:

step1: 试试我的配置文件,复制过去 第二步:享受

@Configuration
@EnableSwagger2
public class SwaggerConfig implements WebMvcConfigurer {
@Bean
public Docket api() {
    return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.any())
            .paths(PathSelectors.any()).build().apiInfo(apiEndPointsInfo());
}

private ApiInfo apiEndPointsInfo() {
    return new ApiInfoBuilder().title("phat").description("Test Management REST API")
            .contact(new Contact("tester", "https://test.vn/", "tester_pro@gmail.com")).license("Apache 2.0")
            .licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html").version("1.0.0").build();
}

@Override
public void addResourceHandlers(final ResourceHandlerRegistry registry) {

    registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");

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

【讨论】:

  • @caothanphatit 不起作用,我注意到 .html 无法在 target/META-INF 中编译
猜你喜欢
  • 2020-04-13
  • 2018-06-11
  • 2019-11-24
  • 1970-01-01
  • 2016-02-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-19
  • 2016-06-22
相关资源
最近更新 更多