user7367999
user7367999

Reputation: 41

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

Upvotes: 4

Views: 21028

Answers (8)

Bezkup
Bezkup

Reputation: 53

For who use Springdoc web-ui and upgraded to SpringBoot 3.2.0 ,

you need to add this dependency:

<dependency>
    <groupId>org.webjars</groupId>
    <artifactId>webjars-locator-core</artifactId>
</dependency>

Refer to this github issue: https://github.com/springdoc/springdoc-openapi/issues/2204

Upvotes: 1

I had an almost similar issue in spring-boot using the springfox-boot-starter:3.0.0 where swagger UI would come as empty and looking at the console it was throwing 404

http://{ip}:{port}/custom/url/swagger-ui/index.html/swagger-ui-bundle.js?v=3.0.0 net::ERR_ABORTED 404

The error was the "/" that I was adding at the end of the documentation URL

I was writing "http://{ip}:{port}/custom/url/swagger-ui/index.html/"

Instead of

"http://{ip}:{port}/custom/url/swagger-ui/index.html"

Note the "/" at the end

Upvotes: 0

Jinz
Jinz

Reputation: 11

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.

Upvotes: 1

Xin He
Xin He

Reputation: 1

Please invite following dependence into pom.xml

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

Upvotes: 0

lewis machilika
lewis machilika

Reputation: 897

Just use /swagger-ui/ instead of swagger-ui.html if you are using version > 3

Upvotes: 1

Vladimir Stanciu
Vladimir Stanciu

Reputation: 1536

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.

Upvotes: 9

fpezzini
fpezzini

Reputation: 794

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

Upvotes: 0

mh377
mh377

Reputation: 1856

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

Upvotes: 1

Related Questions