Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"}
like image 804
user7367999 Avatar asked Jan 27 '23 22:01

user7367999


2 Answers

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.

like image 137
Vladimir Stanciu Avatar answered Jan 31 '23 08:01

Vladimir Stanciu


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();
    }
}
like image 23
mh377 Avatar answered Jan 31 '23 08:01

mh377