BackToReal
BackToReal

Reputation: 141

Cannot access to swagger UI using tomcat, spring boot and Rest

I am trying to add swagger ui documentation to my spring boot application. Here the steps I did into my code:

Add dependencies to my POM.XML

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

then add swagger annotation to the swagger config as follow:

    @Configuration
@EnableSwagger2
public class SwaggerConfig {
       @Bean
        public Docket api() {
            return new Docket(DocumentationType.SWAGGER_2)
                    .select()
                    .apis(RequestHandlerSelectors.basePackage("com.foo.samples.swaggersample"))
                    .paths(PathSelectors.any())
                    .build();
        }
}

when executing this url: http://localhost:8080/my-app-name/swagger-ui.html I got this result:

enter image description here

Your help will be more than welcome!

Upvotes: 0

Views: 1212

Answers (1)

QuocNg
QuocNg

Reputation: 152

I had this similar issue (my error code is 404 not 500 like yours), until I added resource handler:

@Override
protected void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("swagger-ui.html")
        .addResourceLocations("classpath:/META-INF/resources/");

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

Hope that helps

Upvotes: 1

Related Questions