skip to Main Content

I am upgrading the azure sdk for java to version 12.21.1. The spring boot version is 2.1.6.
I am using the following dependency in gradle : implementation ‘com.azure:azure-storage-blob:12.21.1’ .

I am using the following code to create 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();
}

But it is throwing an error at runtime :

 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]

How to resolve this?

2

Answers


  1. Chosen as BEST ANSWER

    Looks like there was some dependency conflict between spring boot version and swagger . I upgraded spring boot to 2.7.10 and swagger to openAPI. This issue was resolved after that. Thanks.


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

    This could be because of missing dependency in your application. Check if you have added the required dependencies to your project’s classpath.

    I have reproduced your requirement to upload/download file using Azure Java SDK.

    Below are the dependencies which I have used in my application.

    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'
    }
    

    Note the Access token of your storage account in the Azure portal:
    enter image description here

    I have created the simple API to call this method which is creating the file and downloading the file from Azure Blob Storage.

    @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
    }
    
    

    Calling the API which I have created and it is generating the url to download the file which has been uploaded as shown below:

    enter image description here

    Uploaded the file to Azure Blob Storage using the above code:
    enter image description here

    By using the above code, downloaded the file as data.txt.

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search