user10224616
user10224616

Reputation:

Swagger UI dosn't show up

I was working on spring project. Firstly I added into my project swagger and my swagger ui also shows all controller but when I added JWT bearer token into my project. Somehow I can't enter to swagger ui. And it keeps showing following window into my browser. How I could fix this problem any ideas ?

enter image description here

in pom.xml

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



  <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>

Config Class for swagger other config class I don't have so far

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SpringFoxConfig {

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage("uzb.ofb.reportgen.controller"))
                .paths(PathSelectors.any())
                .build();
    }
}

Update

enter image description here

UPDATE2 enter image description here

After following code

@Configuration
@EnableSwagger2
public class SpringFoxConfig implements WebMvcConfigurer {

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

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

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage("uzb.ofb.reportgen.controller"))
                .paths(PathSelectors.any())
                .build();
    }
}

Upvotes: 1

Views: 189

Answers (3)

Robin Rozo
Robin Rozo

Reputation: 154

Swagger UI is served from a webjar, you need to configure Spring MVC.

    @Configuration
    @EnableSwagger2
    public class WebConfig implements WebMvcConfigurer {

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

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

Upvotes: 0

Peter Lustig
Peter Lustig

Reputation: 1701

Do you use spring-security in your project? Try to override the configure() methdod in your security config:

@Override
public void configure(WebSecurity web) {
    web.ignoring()
       .antMatchers("/webjars/**", "/swagger-resources/**", "/swagger-ui.html",
               "/v2/api-docs/**");
}

Upvotes: 0

You should extend WebMvcConfigurerAdapter and override method:

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

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

Upvotes: 1

Related Questions