调用与Azure存储相关的Java API时抛出的java.lang.NoSuchMethodError异常

问题描述 投票:0回答:1

在这里留下线索给其他可能遇到相同问题的人。


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

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个回答
0
投票

我终于找到了解决方案,它涉及到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可以正常工作。

© www.soinside.com 2019 - 2024. All rights reserved.