【问题标题】:NoSuchMethod while accessing Sheet API via Java通过 Java 访问 Sheet API 时的 NoSuchMethod
【发布时间】:2019-11-16 13:43:28
【问题描述】:
Google 表格快速入门
点击此链接时 - https://developers.google.com/sheets/api/quickstart/java
我得到了这个:
线程“主”java.lang.NoSuchMethodError 中的异常:
com.google.api.client.googleapis.services.json.AbstractGoogleJsonClient$Builder.setBatchPath(Ljava/lang/String;)Lcom/google/api/client/googleapis/services/AbstractGoogleClient$Builder;
在
com.google.api.services.sheets.v4.Sheets$Builder.setBatchPath(Sheets.java:3143)
在 com.google.api.services.sheets.v4.Sheets$Builder.(Sheets.java:3122)
在
com.pansari.promoter.service.SheetQuickStart.main(SheetQuickStart.java:70)
规格:
Java version (java -version) 1.8
OS Mac
POM 变化:
com.google.apis
google-api-services-sheets
v4-rev516-1.23.0
com.google.api-client
google-api-client
1.23.0
com.google.oauth-client
google-oauth-client-jetty
1.23.0
谁能帮忙?
【问题讨论】:
标签:
java
google-sheets
google-sheets-api
google-workspace
【解决方案1】:
我在这个问题上花了很尴尬的时间,所以想给小费。 setBatchPath 的问题在于它是在 google-api-client 1.23.0 及更高版本中引入的。我在我的 gradle.build 文件中包含了 google-api-client 的最新版本,但是项目中的其他地方有覆盖,这迫使使用 google-api-client 1.22.0 破坏了代码。
如果您遇到此问题,请尝试以下操作:
- 例如检查构建目录
build/dependency 中的jar 版本。
- 运行
gradle dependencyInsight --dependency com.google.api-client 看看 gradle 正在做什么
对我来说,解决方法是将以下内容添加到库项目中的 build.gradle 和使用该库的服务中。
dependencyManagement {
dependencies{
dependency 'com.google.apis:google-api-services-sheets:v4-rev607-1.25.0'
dependency 'com.google.api-client:google-api-client:1.25.0'
}
}
【解决方案2】:
您的 jar 的新版本不包含您尝试调用的方法。阅读这些包的 API,了解它们的使用方式与以前的版本有何不同。
【解决方案3】:
在您的项目中使用这些依赖项:
<dependencies>
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-sheets</artifactId>
<version>v4-rev1-1.21.0</version>
</dependency>
<dependency>
<groupId>com.google.oauth-client</groupId>
<artifactId>google-oauth-client</artifactId>
<version>1.30.4</version>
</dependency>
<dependency>
<groupId>com.google.oauth-client</groupId>
<artifactId>google-oauth-client-java6</artifactId>
<version>1.30.4</version>
</dependency>
<dependency>
<groupId>com.google.oauth-client</groupId>
<artifactId>google-oauth-client-jetty</artifactId>
<version>1.30.4</version>
</dependency>
</dependencies>
您可以通过谷歌HERE查看所有当前的官方依赖版本。
【解决方案4】:
使用以下命令检查项目的依赖树。
mvn org.apache.maven.plugins:maven-dependency-plugin:2.10:tree -Dverbose=truemvn org.apache.maven.plugins:maven-dependency-plugin:2.10:tree -Dverbose=true
检查不同版本的 google-api-client jar 是否仅包括 1.23.0 版本并排除其他版本,如下所示。它应该可以工作。
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-dataflow</artifactId>
<version>v1b3-rev207-1.20.0</version>
<exclusions>
<exclusion> <!-- declare the exclusion here -->
<groupId>com.google.api-client</groupId>
<artifactId>google-api-client</artifactId>
</exclusion>
</exclusions>
</dependency>
【解决方案5】:
使用以下命令检查项目的依赖关系树
mvn org.apache.maven.plugins:maven-dependency-plugin:2.10:tree -Dverbose=truemvn org.apache.maven.plugins:maven-dependency-plugin:2.10:tree -Dverbose=true
并包括 1.23.0 版本的 google-api-client 并排除其他版本,如下所示。
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-dataflow</artifactId>
<version>v1b3-rev207-1.20.0</version>
<exclusions>
<exclusion> <!-- declare the exclusion here -->
<groupId>com.google.api-client</groupId>
<artifactId>google-api-client</artifactId>
</exclusion>
</exclusions>
</dependency>