【问题标题】:The exception java.lang.NoSuchMethodError thrown when invoking Azure storage related java API调用 Azure 存储相关 java API 时抛出异常 java.lang.NoSuchMethodError
【发布时间】:2020-06-20 16:50:22
【问题描述】:

在此处为可能遇到相同问题的其他人留下话题。


我正在尝试通过以下代码从 Azure container 读取 blob

public static void main(String[] args) {
    String connectStr = "it's a workable connection string...";
    // Create a BlobServiceClient object which will be used to create a container client
    BlobServiceClient blobServiceClient = new BlobServiceClientBuilder().connectionString(connectStr).buildClient();
    String containerName = "eugenecontainer";
    BlobContainerClient blobContainerClient = blobServiceClient.getBlobContainerClient(containerName);
    for (BlobItem blobItem: blobContainerClient.listBlobs()){
        System.out.println(blobItem.getName());
    }
}

但是,当它执行blobContainerClient.listBlobs()时,会抛出如下异常:

Exception in thread "main" java.lang.NoSuchMethodError: io.netty.bootstrap.Bootstrap.config()Lio/netty/bootstrap/BootstrapConfig;

我使用maven 作为构建工具。

这里发生了什么?

【问题讨论】:

    标签: java amazon-web-services azure maven netty


    【解决方案1】:

    我终于找到了解决方案,它与maven 依赖冲突有关。多个依赖依赖于不同版本的netty

    我在 maven 中添加了 awsazure 依赖项,如下所示:

    <dependency>
      <groupId>com.amazonaws</groupId>
      <artifactId>aws-java-sdk</artifactId>
      <version>1.11.327</version>
    </dependency>
    <dependency>
      <groupId>com.azure</groupId>
      <artifactId>azure-storage-blob</artifactId>
      <version>12.0.0</version>
    </dependency>
    

    通过使用maven工具mvn dependency:tree,我得到如下输出:

    [INFO] |  +- com.amazonaws:aws-java-sdk-kinesisvideo:jar:1.11.327:compile
    [INFO] |  |  +- io.netty:netty-codec-http:jar:4.1.17.Final:compile
    [INFO] |  |  |  \- io.netty:netty-codec:jar:4.1.17.Final:compile
    [INFO] |  |  \- io.netty:netty-handler:jar:4.1.17.Final:compile
    [INFO] |  |     +- io.netty:netty-buffer:jar:4.1.17.Final:compile
    [INFO] |  |     |  \- io.netty:netty-common:jar:4.1.17.Final:compile
    [INFO] |  |     \- io.netty:netty-transport:jar:4.1.17.Final:compile
    [INFO] |  |        \- io.netty:netty-resolver:jar:4.1.17.Final:compile
    [INFO] |  \- com.azure:azure-storage-common:jar:12.0.0:compile
    [INFO] |     \- com.azure:azure-core-http-netty:jar:1.0.0:compile
    [INFO] |        +- io.netty:netty-handler-proxy:jar:4.1.42.Final:compile
    [INFO] |        |  \- io.netty:netty-codec-socks:jar:4.1.42.Final:compile
    [INFO] |        +- io.projectreactor.netty:reactor-netty:jar:0.9.0.RELEASE:compile
    [INFO] |        |  +- io.netty:netty-codec-http2:jar:4.1.39.Final:compile
    [INFO] |        |  +- io.netty:netty-transport-native-epoll:jar:linux-x86_64:4.1.39.Final:compile
    [INFO] |        |  |  \- io.netty:netty-transport-native-unix-common:jar:4.1.39.Final:compile
    [INFO] |        |  \- io.projectreactor.addons:reactor-pool:jar:0.1.0.RELEASE:compile
    [INFO] |        \- com.azure:azure-core-test:jar:1.0.0:compile
    [INFO] |           \- io.projectreactor:reactor-test:jar:3.3.0.RELEASE:compile
    

    正如我们所见,azureaws 确实依赖于netty,并且netty 的版本不同。所以问题是关于解决冲突。

    根据maven的介绍,

    由于 Maven 以传递方式解决依赖关系,因此可以 不需要的依赖项包含在项目的类路径中。为了 例如,某个较旧的 jar 可能存在安全问题或 与您使用的 Java 版本不兼容。为了解决这个问题, Maven 允许您排除特定的依赖项。已设置排除项 在您的 POM 中的特定依赖项上,并针对特定的 groupId 和 artifactId。当您构建项目时,该工件 不会通过依赖项添加到项目的类路径中 其中声明了排除。

    我们需要排除netty 4.1.17,这样它就不会被添加到项目的类路径中,并将netty明确设置为azure

        <dependency>
          <groupId>com.amazonaws</groupId>
          <artifactId>aws-java-sdk</artifactId>
          <version>1.11.327</version>
          <exclusions>
            <exclusion>
              <artifactId>*</artifactId>
              <groupId>io.netty</groupId>
            </exclusion>
          </exclusions>
        </dependency>
        <dependency>
          <groupId>com.azure</groupId>
          <artifactId>azure-storage-blob</artifactId>
          <version>12.0.0</version>
        </dependency>
        <dependency>
          <groupId>io.netty</groupId>
          <artifactId>netty-all</artifactId>
          <version>4.1.42.Final</version>
        </dependency>
    

    通过将上述依赖项添加到pom.xmlazure 就可以正常工作了。

    【讨论】:

    • 如果一个 jar 已经存在于类路径中,因为它是由服务器在运行时提供的,只需告诉 maven 提供了工件。
    【解决方案2】:

    如果您正在使用下面提到的 Spring Boot 依赖项,请排除 azure-core-http-netty 并为 azure-core-http-okhttp 添加新的单独依赖项,如下所示。

    <dependency>
                    <groupId>com.azure.spring</groupId>
                    <artifactId>azure-spring-boot-starter-storage</artifactId>
                    <exclusions>
                        <exclusion>
                          <groupId>com.azure</groupId>
                          <artifactId>azure-core-http-netty</artifactId>
                        </exclusion>
                    </exclusions>
                </dependency>
               <dependency>
                   <groupId>com.azure</groupId>
                   <artifactId>azure-core-http-okhttp</artifactId>
                   <version>1.2.1</version>
               </dependency>

    【讨论】:

      猜你喜欢
      • 2013-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-24
      相关资源
      最近更新 更多