创建Google Storage的存储对象时的NoSuchMethodError(Java)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了创建Google Storage的存储对象时的NoSuchMethodError(Java)相关的知识,希望对你有一定的参考价值。

尝试创建存储元素时出现以下错误 -

java.lang.NoSuchMethodError:com.google.cloud.storage.StorageOptions.newBuilder()Lcom / google / cloud / storage / StorageOptions $ Builder;

使用 -

 AuthCredentials.ServiceAccountAuthCredentials serviceAccountAuthCredentials = AuthCredentials.createForJson(this.getClass().getClassLoader().getResourceAsStream("abc.json"));
 ServiceAccountCredentials serviceAccountCredentials = serviceAccountAuthCredentials.credentials();
 Storage storage = StorageOptions.newBuilder().setCredentials(serviceAccountCredentials).build().getService();

相同的代码适用于其他应用程序。有人可以帮助我解决这个应用程序可能出现的问题。

我的pom.xml看起来像 -

HTTP://maven.Apache.org/下水道/maven-4.0.0.下水道">

<modelVersion>4.0.0</modelVersion>
<packaging>war</packaging>
<version>1.0</version>

<groupId>com.example.abc</groupId>
<artifactId>abc</artifactId>

<properties>
    <appengine.app.version>1</appengine.app.version>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.8</maven.compiler.source>
    <maven.compiler.target>1.8</maven.compiler.target>
</properties>

<prerequisites>
    <maven>3.5.1</maven>
</prerequisites>

<dependencies>

    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>24.0-jre</version>
    </dependency>

    <!-- Compile/runtime dependencies -->
    <dependency>
        <groupId>com.google.appengine</groupId>
        <artifactId>appengine-api-1.0-sdk</artifactId>
        <version>1.9.62</version>
    </dependency>



    <dependency>
        <groupId>com.google.cloud</groupId>
        <artifactId>google-cloud-storage</artifactId>
        <version>1.16.0</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.google.cloud/gcloud-java-storage -->
    <dependency>
        <groupId>com.google.cloud</groupId>
        <artifactId>gcloud-java-storage</artifactId>
        <version>0.2.3</version>
    </dependency>

    <dependency>
        <groupId>com.google.cloud</groupId>
        <artifactId>google-cloud-bom</artifactId>
        <version>0.34.0-alpha</version>
        <type>pom</type>
        <scope>import</scope>
    </dependency>


    <dependency>
        <groupId>jstl</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>



    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-bundle</artifactId>
        <version>1.19.4</version>
    </dependency>
    <!--causes exception on removal-->
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-atom</artifactId>
        <version>1.19.4</version>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-core</artifactId>
        <version>1.19.4</version>
    </dependency>
    <dependency>
        <groupId>com.sun.jersey</groupId>
        <artifactId>jersey-json</artifactId>
        <version>1.19.4</version>
    </dependency>


    <dependency>
        <groupId>com.owlike</groupId>
        <artifactId>genson</artifactId>
        <version>1.4</version>
    </dependency>

    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20180130</version>
    </dependency>
    <dependency>
        <groupId>asm</groupId>
        <artifactId>asm</artifactId>
        <version>20041228.180559</version>
    </dependency>
    <dependency>
        <groupId>javax.inject</groupId>
        <artifactId>javax.inject</artifactId>
        <version>1</version>
    </dependency>

    <dependency>
        <groupId>commons-codec</groupId>
        <artifactId>commons-codec</artifactId>
        <version>1.11</version>
    </dependency>
    <!-- causes exception on removal-->
    <dependency>
        <groupId>org.codehaus.jackson</groupId>
        <artifactId>jackson-mapper-asl</artifactId>
        <version>1.9.11</version>
    </dependency>

    <dependency>
        <groupId>org.codehaus.jettison</groupId>
        <artifactId>jettison</artifactId>
        <version>1.3.8</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.9.4</version>
    </dependency>
    <dependency>
        <groupId>joda-time</groupId>
        <artifactId>joda-time</artifactId>
        <version>2.9.9</version>
    </dependency>

    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5.5</version>
    </dependency>


    <!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore -->
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpcore</artifactId>
        <version>4.4.9</version>
    </dependency>

    <dependency>
        <groupId>com.google.apis</groupId>
        <artifactId>google-api-services-drive</artifactId>
        <version>v3-rev99-1.23.0</version>
    </dependency>


</dependencies>

<build>
    <!-- for hot reload of the web application-->
    <outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>versions-maven-plugin</artifactId>
            <version>2.2</version>
            <executions>
                <execution>
                    <phase>compile</phase>
                    <goals>
                        <goal>display-dependency-updates</goal>
                        <goal>display-plugin-updates</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <version>3.5.1</version>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.8</source>
                <target>1.8</target>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-war-plugin</artifactId>
            <version>2.6</version>
            <configuration>
                <archiveClasses>true</archiveClasses>
                <webResources>
                    <!-- in order to interpolate version from pom into appengine-web.xml -->
                    <resource>
                        <directory>${basedir}/src/main/webapp/WEB-INF</directory>
                        <filtering>true</filtering>
                        <targetPath>WEB-INF</targetPath>
                    </resource>
                </webResources>
            </configuration>
        </plugin>

        <plugin>
            <groupId>com.google.appengine</groupId>
            <artifactId>appengine-maven-plugin</artifactId>
            <version>1.9.60</version>
            <configuration>
                <enableJarClasses>false</enableJarClasses>



            </configuration>
        </plugin>

        <!--<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-shade-plugin</artifactId>
            <version>2.3</version>
            <executions>
                <execution>
                    <phase>package</phase>
                    <goals>
                        <goal>shade</goal>
                    </goals>
                    <configuration>
                        <artifactSet>
                            <includes>
                                <include>com.google.guava:*</include>
                            </includes>
                        </artifactSet>
                        <minimizeJar>false</minimizeJar>
                        <relocations>
                            <relocation>
                                <pattern>com.google.common</pattern>
                                <shadedPattern>repackaged.com.google.common</shadedPattern>
                            </relocation>
                        </relocations>
                        <shadedArtifactAttached>true</shadedArtifactAttached>
                    </configuration>
                </execution>
            </executions>
        </plugin>-->
    </plugins>
</build>

答案

没有深入挖掘根本原因(这似乎是泽西的com.sun.jersey.spi.container.JavaMethodInvokerFactory使用反射结果与错误版本的com.google.cloud.ServiceOptions类)

到目前为止,我发现的唯一解决方法是将google-cloud-storage版本从1.16.0升级到1.19.0

<dependency>
   <groupId>com.google.cloud</groupId>
   <artifactId>google-cloud-storage</artifactId>
   <version>1.19.0</version>
</dependency>

如果有帮助,请告诉我,并感谢您分享完整的样本。

以上是关于创建Google Storage的存储对象时的NoSuchMethodError(Java)的主要内容,如果未能解决你的问题,请参考以下文章

为 Google Storage 存储分区中的所有对象设置元数据

在 Google Cloud Storage 中存储像数据一样的键值

Google App Engine 应用无法访问 Google Cloud Storage 存储分区

如何使用 PHP 在 Firebase 中自动导入存储桶 - Google Cloud Storage

如何从 Google Cloud Storage 中获取特定对象元数据信息?

将自定义元数据设置为 Google Cloud Storage 中的对象