azure java.lang.NoClassDefFoundError:reactor/util/context/ContextView

waxmsbnn  于 2023-03-31  发布在  Java
关注(0)|答案(1)|浏览(852)

我正在将Azure SDK for Java升级到版本12.21.1。Sping Boot 版本是2.1.6。我在Gradle中使用以下依赖项:实现'com.azure:azure-storage-blob:12.21.1'。
我使用以下代码创建BlobServiceClient:

String accountUrl = "https://" + accountName + ".blob.core.windows.net";
 StorageSharedKeyCredential sharedKeyCredential = new StorageSharedKeyCredential(accountName, accountKey);

try{
BlobServiceClient blobServiceClient = new BlobServiceClientBuilder().credential(sharedKeyCredential).endpoint(accountUrl).buildClient();
}

但它在运行时抛出一个错误:

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Handler dispatch failed; nested exception is java.lang.NoClassDefFoundError: reactor/util/context/ContextView] with root cause

java.lang.ClassNotFoundException: reactor.util.context.ContextView
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:581) ~[na:na]
BuiltinClassLoader.java:581
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178) ~[na:na]
ClassLoaders.java:178
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522) ~[na:na]
ClassLoader.java:522
    at com.azure.core.http.policy.HttpPolicyProviders.addAfterRetryPolicies(HttpPolicyProviders.java:52) ~[azure-core-1.37.0.jar:1.37.0]
HttpPolicyProviders.java:52
    at com.azure.storage.blob.implementation.util.BuilderHelper.buildPipeline(BuilderHelper.java:128) ~[azure-storage-blob-12.21.1.jar:12.21.1]
BuilderHelper.java:128
    at com.azure.storage.blob.BlobServiceClientBuilder.buildAsyncClient(BlobServiceClientBuilder.java:135) ~[azure-storage-blob-12.21.1.jar:12.21.1]
BlobServiceClientBuilder.java:135
    at com.azure.storage.blob.BlobServiceClientBuilder.buildClient(BlobServiceClientBuilder.java:114) ~[azure-storage-blob-12.21.1.jar:12.21.1]

如何解决这一问题?

klsxnrf1

klsxnrf11#

java.lang.NoClassDefFoundError:reactor/util/context/ContextView
这可能是因为应用程序中缺少依赖项。请检查是否已将所需的依赖项添加到项目的类路径中。
我复制了您使用Azure Java SDK上传/下载文件的要求。
下面是我在应用程序中使用的依赖项。

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation group: 'com.azure', name: 'azure-storage-blob', version: '12.16.0'
implementation group: 'com.microsoft.azure', name: 'azure-storage-spring-boot-starter', version: '2.2.5'
implementation group: 'commons-fileupload', name: 'commons-fileupload', version: '1.4'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

注意Azure门户中存储帐户的访问令牌:

我已经创建了简单的API来调用这个方法,这个方法正在创建文件并从Azure Blob存储下载文件。

@PostMapping("/upload")
public  void  uploadFile(@RequestParam(value  =  "file")  MultipartFile  file)  throws  IOException  {

// Code To Create and File In Blob Storage
String  str = "DefaultEndpointsProtocol=https;AccountName=<storage_account_name>;AccountKey=storage_account_access_key;EndpointSuffix=core.windows.net";
OffsetDateTime  expiryTime  =  OffsetDateTime.now().plusDays(1);
BlobSasPermission  permission  =  new  BlobSasPermission().setReadPermission(true);
BlobServiceSasSignatureValues  values  =  new  BlobServiceSasSignatureValues(expiryTime,  permission).setStartTime(OffsetDateTime.now());
BlobContainerClient  container  =  new  BlobContainerClientBuilder().connectionString(str).containerName("<conatiner_name>").buildClient();
BlobClient  blob  =  container.getBlobClient(file.getOriginalFilename());
blob.upload(file.getInputStream(),  file.getSize(),  true);
String  sasToken  =  blob.generateSas(values);
// Code To Create and File In Blob Storage

// Code To download the File From Blob Storage
URL  url  =  new  URL(blob.getBlobUrl()  +  "?"  +  sasToken);
HttpURLConnection  httpConn  =  (HttpURLConnection)  url.openConnection();
int  responseCode  =  httpConn.getResponseCode();
// Check if the response code is HTTP_OK (200)
if  (responseCode  ==  HttpURLConnection.HTTP_OK)  {
// Open input stream from the HTTP connection
InputStream  inputStream  =  httpConn.getInputStream();
// Open output stream to save the file
FileOutputStream  outputStream  =  new FileOutputStream("C:\\Users\\win10\\Downloads\\data.txt");
// Read bytes from input stream and write to output stream
int  bytesRead;
byte[]  buffer  =  new  byte[4096];
while  ((bytesRead  =  inputStream.read(buffer))  !=  -1)  {
outputStream.write(buffer,  0,  bytesRead);
}
// Close streams
outputStream.close();
inputStream.close();
System.out.println("File downloaded");
}  else  {
System.out.println("Failed to download file: "  +  httpConn.getResponseMessage());
}
httpConn.disconnect();
// Code To download the File From Blob Storage
}

调用我创建的API,它正在生成URL以下载已上传的文件,如下图所示:

已使用上述代码将文件上载到Azure Blob存储:

通过使用上面的代码,下载的文件为 * data.txt *。

相关问题