Swagger2 + Spring REST API not working

I have a Spring Rest controller and its NOT Spring application. Its just a REST API project. I want to integrate swagger2 in my project. I tried all the examples on the web and in the demo, but no luck. When I try to execute http: // localhost: 8085 / context / swagger-ui.html , I get 404 error. Please find my explanation below and let me know if there are any discrepancies. Any help is appreciated.

jars - under / WEB-INF / lib

Google collection-1.0.jar springfox-core-2.2.2.jar springfox-scheme-2.2.2.jar springfox-SPI-2.2.2.jar springfox-spring -web-2.2.2.jar springfox-staticdocs-2.2 .2.jar springfox-swag-in-phase 2.2.2.jar springfox-swag-sch-2.2.2.jar springfox-swagger2-2.2.2.jar

My swagger configuration class is

@EnableSwagger2
public class SwaggerConfiguration {

}

My springconfig class

@EnableWebMvc
@ComponentScan(basePackageClasses = controller.class)
@Import(SwaggerConfiguration.class)
public class SpringConfiguration 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/");
}
}

My application initializer class is below according to springfox-java demo. I tried with and without a class below and it does not work anyway.

Application Initializer class

public class ApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return null;
}

@Override
protected Class<?>[] getServletConfigClasses() {
    return new Class[]{controller.class};
}

@Override
protected String[] getServletMappings() {
    return new String[]{"/*"};
}
}

I can access my urls but not swagger-ui.html in the same context.

Please let me know that I am not here?

+4
source share
1 answer

I add a manual selection of controllers:

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket productApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage("my.package.to.api"))
                .paths(regex("/product.*")) //optionnal
                .build();

    }
}
0
source

Source: https://habr.com/ru/post/1621028/


All Articles