3

Hi i'm using springboot 1.5.3.RELEASE with swagger2. When i run app and i can access the json swagger response by accessing "localhost:3030/v2/api-docs" . But i cannot access "localhost:3030/swagger-ui.html" also showing no mapping found for "/swagger-ui.html" . How do i solve this issue.

dependencies

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- mock testing -->
        <dependency>
            <groupId>com.jayway.restassured</groupId>
            <artifactId>rest-assured</artifactId>
            <version>2.3.4</version>
            <scope>compile</scope>
        </dependency>
        <!-- /mock testing -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.6.1</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.6.1</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>com.fasterxml</groupId>
            <artifactId>classmate</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>commons-codec</groupId>
            <artifactId>commons-codec</artifactId>
            <version>1.9</version>
        </dependency>

swagger config

@Configuration
@Component
@EnableSwagger2
public class RestConfig {

    @Bean
    public MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
        ObjectMapper mapper = new ObjectMapper();
        mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
        MappingJackson2HttpMessageConverter converter =
                new MappingJackson2HttpMessageConverter(mapper);
        return converter;
    }

    @Bean
    public ErpAppConfig configureErpApp() {
        // just to process the @PostConstruct
        return new ErpAppConfig();
    }

      @Bean
    public Docket productApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
    }

}
boycod3
  • 5,033
  • 11
  • 58
  • 87
  • Add ResourceHandler such as `registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/"); registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");` – Kalaiselvan Oct 09 '17 at 06:02
  • where should i add this registry??? – boycod3 Oct 09 '17 at 06:29
  • a file which extends `WebMvcConfigurerAdapter` In that override existing method `addResourceHandlers` for your ref: http://www.baeldung.com/swagger-2-documentation-for-spring-rest-api – Kalaiselvan Oct 09 '17 at 06:41
  • Duplicate : Check Answer here https://stackoverflow.com/questions/46151540/added-springfox-swagger-ui-and-its-not-working-what-am-i-missing/64333853#64333853 – Ravi Parekh Oct 13 '20 at 12:15
  • Does this answer your question? [Added Springfox Swagger-UI and it's not working, what am I missing?](https://stackoverflow.com/questions/46151540/added-springfox-swagger-ui-and-its-not-working-what-am-i-missing) – Ravi Parekh Aug 18 '21 at 09:49

1 Answers1

4
Create a mapping for Swagger by override existing method addResourceHandler


public class AppConfig extends WebMvcConfigurerAdapter {
    @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/");
    }
}
Kalaiselvan
  • 2,095
  • 1
  • 18
  • 31
  • 1
    Also i added permit url in spring security for swagger URL – boycod3 Oct 13 '17 at 05:23
  • 1
    Slightly different solution worked for me, also calling parent method: `super.addResourceHandlers(registry);`, check: https://github.com/springfox/springfox/issues/2037 – y.luis.rojo Mar 15 '18 at 18:55