[英]“java.lang.NoSuchMethodError” exception is thrown on using Spreadsheet Service
[英]The exception java.lang.NoSuchMethodError thrown when invoking Azure storage related java API
為可能遇到相同問題的其他人留下線程。
我正在嘗試通過以下代碼從 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
作為構建工具。
這里會發生什么?
我終於找到了解決方案,它是關於maven
依賴沖突的。 在不同版本中,不止一種依賴依賴於netty
。
我在 maven 中添加了aws
和azure
依賴項,如下所示:
<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
正如我們所看到的, azure
和aws
也取決於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.xml
, azure
可以正常工作了。
如果您使用如下所述的 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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.