简体   繁体   English

Swagger2 + Spring REST API无法正常工作

[英]Swagger2 + Spring REST API not working

I have a Spring Rest controller and its NOT spring boot application. 我有一个Spring Rest控制器及其NOT spring启动应用程序。 Its just a REST API Project. 它只是一个REST API项目。 I want to integrate swagger2 in my project. 我想在我的项目中集成swagger2。 I tried all the examples in Net and in demo but with no luck. 我尝试了Net和演示中的所有示例,但没有运气。 When i try to execute http://localhost:8085/context/swagger-ui.html i get 404 error. 当我尝试执行http:// localhost:8085 / context / swagger-ui.html时,我收到404错误。 Please find my confugration below and let me know if there is any discrepencies. 请在下面找到我的问候,如果有任何差异请告诉我。 Any help is highly appreciated. 任何帮助都非常感谢。

jars - under /WEB-INF/lib jars - 在/ WEB-INF / lib下

google-collections-1.0.jar springfox-core-2.2.2.jar springfox-schema-2.2.2.jar springfox-spi-2.2.2.jar springfox-spring-web-2.2.2.jar springfox-staticdocs-2.2.2.jar springfox-swagger-common-2.2.2.jar springfox-swagger-ui-2.2.2.jar springfox-swagger2-2.2.2.jar google-collections-1.0.jar springfox-core-2.2.2.jar springfox-schema-2.2.2.jar springfox-spi-2.2.2.jar springfox-spring-web-2.2.2.jar springfox-staticdocs-2.2 .2.jar springfox-swagger-common-2.2.2.jar springfox-swagger-ui-2.2.2.jar springfox-swagger2-2.2.2.jar

My swagger config class - 我的招摇配置类 -

@EnableSwagger2
public class SwaggerConfiguration {

}

My springconfig class 我的springconfig类

@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 below as per springfox-java demo . 我的应用程序初始化程序类如下所示,根据springfox-java演示。 I tried with and without the below class and its not working either way. 我尝试使用和不使用以下类,并且无论如何都无法正常工作。

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 rest urls but not swagger-ui.html in the same context. 我可以在相同的上下文中访问我的其他网址但不能访问swagger-ui.html。

Please let me know what i am missing here? 请让我知道我在这里缺少什么?

I add the 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.*")) //optional 
                .build();

    }
}

given details are not sufficient to reproduce/analyse the issue. 给定的细节不足以重现/分析问题。

But, today I faced similar problem, ofcourse used SpringBoot, and solved myself as below: as my sample have one controller class and an application class having main method, I created packages as below, and it got solved: 但是,今天我遇到了类似的问题,当然使用SpringBoot,并解决了我自己如下:由于我的示例有一个控制器类和一个具有main方法的应用程序类,我创建了如下的包,它得到了解决:

  • hello 你好
    • controllers 控制器
      • HelloController 为HelloController
    • swagger 昂首阔步
      • SwaggerConfig2 SwaggerConfig2
    • HelloApplication HelloApplication

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM