Swagger Api Docs和Swagger UI无法使用Spring 5

时间:2018-05-12 03:37:46

标签: java spring spring-boot swagger

我确实有一些在Spring @RestController上使用Spring5.0.5开发的Restful Web服务。 (这不是Spring Boot项目)

我正在尝试使用Swagger生成API文档,为此我尝试使用Springfox。

以下是我在项目中所做的更改: - 在Pom.xml中添加

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

创建了一个SwaggerConfig.java类 -

@Configuration
@EnableSwagger2
@EnableAspectJAutoProxy(proxyTargetClass=true)
public class SwaggerConfig {                                    
    @Bean
    public Docket api() { 
        return new Docket(DocumentationType.SWAGGER_2)  
          .select()                                  
          .apis(RequestHandlerSelectors.basePackage("package-name"))              
          .paths(PathSelectors.any())                          
          .build();                                           
    }
}

注意: - 我也试过添加/删除@EnableAspectJAutoProxy(proxyTargetClass = true)。

AppConfig.java

@EnableWebMvc
public class AppConfig extends WebMvcConfigureAdaptor{

@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/");
}
}

但是在使用http://localhost:port/context-root/v2/api-docshttp://localhost:port/context-root/v2/swagger-ui.html时,我不断得到一些Bean初始化异常 -

使用名称&#39; documentationPluginsBootStrapper&#39;创建bean时出错。 请在实施中建议我的错误。 我一直在尝试删除并添加一些注释但到目前为止没有成功。

错误: -

DispatcherSer       E   org.springframework.web.servlet.FrameworkServlet initServletBean  Context initialization failed
        org.springframework.beans.factory.UnsatisfiedDependencyException: Error createion bean with name 'documentationPluginsBootstrapper' defined in URL [wsjar:file:/C:/WebspherePathforWarfile/WEB-INF/lib/springfox-spring-web-2.7.0.jar!/springfox/documentation/spring/web/plugins/DocumentationPluginsBootstrapper.class]: Unsatisfied dependency expressed through constructor parameter 1; nested exception is org.springframework.beans.UnsatisfiedDependencyException: Error creating bean with name 'webMvcRequestHandlerProvider' defined in URL [wsjar:file:C:/webspeherisntalledapp/WEB-INF/lib/springfox-spring-web2.7.0.jar!/springfox/documentation/spring/web/plugins/webMVCRequestHandlerProvider.class ] Unsatisfied dependency expressed through constructor parameter 0; nested exception  is org.springframework.beans.factory.NosuchBeandefinitionException : No qualifying bean of type 'java.util.List<org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping>' available :expected at least 1 bean which qualifies as autowire candidate.Dependency annotations {}
        at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:729)
        at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:192)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1270)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1127)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:541)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:501)
        at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$dogetBean$0(AbstractBeanFactory.java:317)
                at org.springframework.beans.factory.support.AbstractBeanFactory$$Lambda.000000002E7527.getObject(unkown source)
        ......
        at javax.servlet.GenericServlet.init(GenericServlet.java:244)
.................
caused by :org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'webMvcRequestHandlerProvider' defined in URL [wsjar:file:C:/Webspherinstalledapp/WEB-INF/lib/springfox-spring-web-2.7.0.jar!/springfox/documentation/spring/web/plugins/WebMvcRequestHandlerProvider.class] : Unsatisfied dependency expressed through constructor parameter 0; nested exception  is org.springframework.beans.factory.NosuchBeandefinitionException : No qualifying bean of type 'java.util.List<org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping>' available :expected at least 1 bean which qualifies as autowire candidate.Dependency annotations: {}
        at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:729)
        at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:192)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1270)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1127)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:541)
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:501)
        at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$dogetBean$0(AbstractBeanFactory.java:317)
                at org.springframework.beans.factory.support.AbstractBeanFactory$$Lambda.000000002E7527.getObject(unkown source)

更新我能够通过从SwaggerConfig.java中删除@Configuration注释来解决此错误,但现在我遇到了另一个问题。

获取 - noHandlerFound当我访问http://localhost:port/context-root/v2/api-docs

时找不到映射

0 个答案:

没有答案