0

I have non boot spring mvc application. When I hit the swagger-ui.html or swagger-ui/. I am getting the 404 error.

My config classes:

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");
  }
}

Dependencies:

<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>
Helen
  • 87,344
  • 17
  • 243
  • 314
Alex
  • 33
  • 1
  • 6
  • 1
    can you try as dependency just `io.springfox springfox-boot-starter3.0.0` instead of the two you provided? also, you either need to access it via /swagger-ui/ or /swagger-ui/index.html. the swagger-ui.html is no longer available, afaik – Andrei Sfat Jan 20 '22 at 10:43
  • also, I don't think you need `@EnableSwagger2` nor to define the `addResourceHandlers`. just create a bean of type `Docket`, like ```@Bean public Docket api() { return new Docket(DocumentationType.OAS_30) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.any()) .paths(PathSelectors.any()) .build(); }``` – Andrei Sfat Jan 20 '22 at 10:46
  • @AndreiSfat Doesn't work... – Alex Jan 20 '22 at 11:57
  • are you using by any chance server.servlet.context-path in your configuration? what are using? spring boot? – Andrei Sfat Jan 20 '22 at 12:30
  • @AndreiSfat I don't use spring boot and also i don't use any configurations, except for the ones I posted – Alex Jan 20 '22 at 12:34
  • Consider moving to `springdoc`. In https://stackoverflow.com/questions/70178343/springfox-3-0-0-is-not-working-with-spring-boot-2-6-0/70178391#70178391 you can read more about it. – João Dias Jan 20 '22 at 20:14

1 Answers1

0

step1: try my config file for you, just copy and past step2: enjoy

@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 Doesn't work, and I noticed that .html doesn't compile in target/META-INF – Alex Jan 20 '22 at 11:59