V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
ekoeko
V2EX  ›  Java

求助大神, spring boot 整合 swagger 报错。。。。

  •  
  •   ekoeko · 2017-07-04 09:32:24 +08:00 · 15321 次点击
    这是一个创建于 2482 天前的主题,其中的信息可能已经有所发展或是发生改变。

    这是报错信息:

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'requestMappingHandlerAdapter' defined in class path resource [org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration$EnableWebMvcConfiguration.class]: Initialization of bean failed; nested exception is java.lang.NoSuchMethodError: com.google.common.collect.FluentIterable.toList()Lcom/google/common/collect/ImmutableList; at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:564) ~[spring-beans-4.3.7.RELEASE.jar!/:4.3.7.RELEASE] at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483) ~[spring-beans-4.3.7.RELEASE.jar!/:4.3.7.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306) ~[spring-beans-4.3.7.RELEASE.jar!/:4.3.7.RELEASE] at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) ~[spring-beans-4.3.7.RELEASE.jar!/:4.3.7.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302) ~[spring-beans-4.3.7.RELEASE.jar!/:4.3.7.RELEASE] at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197) ~[spring-beans-4.3.7.RELEASE.jar!/:4.3.7.RELEASE] at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:761) ~[spring-beans-4.3.7.RELEASE.jar!/:4.3.7.RELEASE] at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:866) ~[spring-context-4.3.7.RELEASE.jar!/:4.3.7.RELEASE] at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542) ~[spring-context-4.3.7.RELEASE.jar!/:4.3.7.RELEASE] at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:122) ~[spring-boot-1.5.2.RELEASE.jar!/:1.5.2.RELEASE] 。。。。。

    这是我的 swagger 类:

    package com.codeages.data.service.restful;

    import org.springframework.context.annotation.Bean;

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

    /**

    • Created by eko on 03/07/2017. */

    @Configuration @EnableSwagger2 public class Swagger2 {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.didispace.web"))
                .paths(PathSelectors.any())
                .build();
    }
    
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("test title")
                .description("test description")
                .termsOfServiceUrl("test termsOfServiceUrl")
                .contact("test contact")
                .version("test version")
                .build();
    }
    

    } 我试着去掉 @Configuration 就不报错了,但是 swagger 也不起作用了。。 我的 swagger 版本是 2.6.1,org.springframework.boot 版本 1.5.2.RELEASE。 昨天 Google 了一天没能找到答案。。。求大神指导。

    13 条回复    2017-10-26 23:47:08 +08:00
    mineqiqi
        1
    mineqiqi  
       2017-07-04 10:29:58 +08:00
    目测 jar 包冲突?
    mineqiqi
        2
    mineqiqi  
       2017-07-04 10:30:37 +08:00
    也有可能是 JDK 不匹配?
    delavior
        3
    delavior  
       2017-07-04 10:37:16 +08:00
    guava 版本不对吧,NoSuchMethodError 啊
    pengfei
        4
    pengfei  
       2017-07-04 10:43:50 +08:00   ❤️ 1
    `NoSuchMethodError: com.google.common.collect.FluentIterable.toList()`
    感觉是你的 guava 版本不对,
    https://github.com/springfox/springfox/issues/441
    pengfei
        5
    pengfei  
       2017-07-04 10:46:09 +08:00
    手贱快捷键用错了
    https://github.com/springfox/springfox/issues/1055
    用`dependency:tree`检查项目中的 guava 版本, 有可能 guava 被第三方依赖引入, 建议先检查是否 guava 引入重复再升级 guava 版本
    hantsy
        6
    hantsy  
       2017-07-04 10:53:03 +08:00
    参考: https://github.com/hantsy/angularjs-springmvc-sample-boot

    SpringFox 这种第三方的每个大版本变化很大,目前我的例子好像使用 Springfox 2.5,2.6 都没问题。我的例子没有升到 2.7,2.7 删除了 Springfox-staticdocs。
    hantsy
        7
    hantsy  
       2017-07-04 10:55:32 +08:00
    你这个应该和 Swagger 没太大的关系,没跑起来。

    ```
    java.lang.NoSuchMethodError: com.google.common.collect.FluentIterable.toList()
    ```

    guava 本身也是个坑爹的东西,版本升级又快,差几个版本之间的兼容性就不怎么样了。
    ekoeko
        8
    ekoeko  
    OP
       2017-07-04 10:56:45 +08:00
    @pengfei 谢谢你的帮助,com.google.guava:guava:jar:12.0.1:compile 版本是很低,好像是的,只有一个 guava,我看一下怎么升级。。。
    ekoeko
        9
    ekoeko  
    OP
       2017-07-04 11:12:49 +08:00
    @pengfei 我的 guava 是在 org.apache.hbase:hbase-server 中引入的,但是我更新了 hbase-server,但是 guava 并没有更新。。。
    ekoeko
        10
    ekoeko  
    OP
       2017-07-04 11:19:28 +08:00
    @hantsy 谢谢帮助,我试过 2.5 和 2.6 和 2.2.2 还是报同样的错误。。
    ekoeko
        11
    ekoeko  
    OP
       2017-07-04 11:24:53 +08:00
    @pengfei 我单独又把 guava15.0 添加到 pom.xml 中,就可以了,谢谢你!!!
    acrisliu
        12
    acrisliu  
       2017-07-04 11:34:03 +08:00
    都是好老的版本啊
    jack80342
        13
    jack80342  
       2017-10-26 23:47:08 +08:00
    最近翻译了 Spring Boot 最新的官方文档,欢迎 Fork,https://www.gitbook.com/book/jack80342/spring-boot/details
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2818 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 02:24 · PVG 10:24 · LAX 19:24 · JFK 22:24
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.