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

2017-07-04 09:32:24 +08:00
 ekoeko

这是报错信息:

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;

/**

@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 了一天没能找到答案。。。求大神指导。

15336 次点击
所在节点    Java
13 条回复
mineqiqi
2017-07-04 10:29:58 +08:00
目测 jar 包冲突?
mineqiqi
2017-07-04 10:30:37 +08:00
也有可能是 JDK 不匹配?
delavior
2017-07-04 10:37:16 +08:00
guava 版本不对吧,NoSuchMethodError 啊
pengfei
2017-07-04 10:43:50 +08:00
`NoSuchMethodError: com.google.common.collect.FluentIterable.toList()`
感觉是你的 guava 版本不对,
https://github.com/springfox/springfox/issues/441
pengfei
2017-07-04 10:46:09 +08:00
手贱快捷键用错了
https://github.com/springfox/springfox/issues/1055
用`dependency:tree`检查项目中的 guava 版本, 有可能 guava 被第三方依赖引入, 建议先检查是否 guava 引入重复再升级 guava 版本
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
2017-07-04 10:55:32 +08:00
你这个应该和 Swagger 没太大的关系,没跑起来。

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

guava 本身也是个坑爹的东西,版本升级又快,差几个版本之间的兼容性就不怎么样了。
ekoeko
2017-07-04 10:56:45 +08:00
@pengfei 谢谢你的帮助,com.google.guava:guava:jar:12.0.1:compile 版本是很低,好像是的,只有一个 guava,我看一下怎么升级。。。
ekoeko
2017-07-04 11:12:49 +08:00
@pengfei 我的 guava 是在 org.apache.hbase:hbase-server 中引入的,但是我更新了 hbase-server,但是 guava 并没有更新。。。
ekoeko
2017-07-04 11:19:28 +08:00
@hantsy 谢谢帮助,我试过 2.5 和 2.6 和 2.2.2 还是报同样的错误。。
ekoeko
2017-07-04 11:24:53 +08:00
@pengfei 我单独又把 guava15.0 添加到 pom.xml 中,就可以了,谢谢你!!!
acrisliu
2017-07-04 11:34:03 +08:00
都是好老的版本啊
jack80342
2017-10-26 23:47:08 +08:00
最近翻译了 Spring Boot 最新的官方文档,欢迎 Fork,https://www.gitbook.com/book/jack80342/spring-boot/details

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/372838

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX