'Receiving 404 error on Swagger UI with Spring
I am integrating with swagger UI with Spring boot application. When I hit the swagger-ui.html. I am getting the 404 error. My config class is below:
@Configuration
@EnableSwagger2
//@Import(SwaggerConfiguration.class)
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
...
Error message:
{"status":404,"message":"HTTP 404 Not Found","link":"https://jersey.java.net/apidocs/2.8/jersey/javax/ws/rs/NotFoundException.html"}
Solution 1:[1]
I had a similar problem where /swagger-ui.html (or the new endpoint version /swagger-ui/) returned 404, but the /v2/api-docs returned a valid json. The solution was to downgrade the swagger version from 3.0.0. to 2.9.2.
Solution 2:[2]
Hope this helps, please find my working swagger config below:
@Configuration
@EnableSwagger2
@Profile({"!production"})
public class SwaggerConfiguration extends WebMvcConfigurerAdapter {
@Autowired
private ServletContext servletContext;
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.host("localhost")
.directModelSubstitute(LocalDate.class, Date.class)
.pathProvider(new RelativePathProvider(servletContext) {
@Override
public String getApplicationBasePath() {
return "/";
}
})
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
}
Solution 3:[3]
Just use /swagger-ui/ instead of swagger-ui.html if you are using version > 3
Solution 4:[4]
I had the same problem and it was fixed by replacing the springfox-swagger2 and springfox-swagger-ui dependencies with springfox-boot-starter:3.0.0 and removing the @EnableSwagger2 annotation. Please note that the Swagger url also changes from http://localhost:8080/swagger-ui.html to http://localhost:8080/swagger-ui/index.html.
Solution 5:[5]
Do you have @EnableWebMvc in one of your configuration classes? If so, you will have to do the resources configuration manually, like below:
@Override
public void addResourceHandlers (ResourceHandlerRegistry registry) {
registry.addResourceHandler("/swagger-ui.html**")
.addResourceLocations("classpath:/META-INF/resources/swagger-ui.html");
registry.
addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
Solution 6:[6]
Please invite following dependence into pom.xml
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Vladimir Stanciu |
| Solution 2 | mh377 |
| Solution 3 | lewis machilika |
| Solution 4 | Dharman |
| Solution 5 | fpezzini |
| Solution 6 | Xin He |
