首页 > 解决方案 > 为 azure blob 添加 Pom 依赖项将我的 REST API 响应从 JSON 更改为 XML

问题描述

我有一个带有 REST API 的 Web 应用程序,它工作正常,返回 JSON 响应,完全没有问题。

现在我正在尝试添加 azure blob 服务以将 blob 文件上传到我的 azure 帐户。

一旦我为 azure blob 添加了 maven 依赖项,我现有的 REST API 就开始返回 XML 响应,从而破坏了我的整个应用程序。

<dependency>
            <groupId>com.azure</groupId>
            <artifactId>azure-storage-blob</artifactId>
            <version>12.6.0</version>
        </dependency>

有人遇到过这个问题吗?删除此依赖项可解决问题,但我将无法连接到 Azure 云。

标签: azurerestazure-blob-storage

解决方案


即使添加了依赖项,为什么不使用 Java SDK?

如果您仍想使用PUT BLOB等 REST API ,您可以设置Content-Typeapplication/json. 使用共享密钥访问时,还需要在“stringToSign”中添加Content-Type。您可以使用 C#参考此问题。

StringToSign = VERB + "\n" +
Content-Encoding + "\n" +
Content-Language + "\n" +
Content-Length + "\n" +
Content-MD5 + "\n" +
Content-Type + " \n" +
日期 + "\n" +
If-Modified-Since + "\n" +
If-Match + "\n" +
If-None-Match + "\n" +
If-Unmodified-Since + "\ n" +
范围 + "\n" +
CanonicalizedHeaders +
CanonicalizedResource;


使用SDK将本地文件上传到 blob 的演示:

String accountName = "";
String accountKey = "";
StorageSharedKeyCredential credential = new StorageSharedKeyCredential(accountName, accountKey);
String endpoint = String.format(Locale.ROOT, "https://%s.blob.core.windows.net", accountName);

BlobServiceClient storageClient = new BlobServiceClientBuilder().endpoint(endpoint).credential(credential).buildClient();
BlobContainerClient blobContainerClient = blobServiceClient.getBlobContainerClient("mycontainer");
BlobClient blobClient = blobContainerClient.getBlobClient("myblockblob");
blobClient.uploadFromFile("local-file.jpg");

推荐阅读