【问题标题】:Calling Azure Migrate Rest Api Using Azure SDK for Java使用 Azure SDK for Java 调用 Azure Migrate Rest Api
【发布时间】:2020-04-16 02:49:45
【问题描述】:

我正在尝试使用 spring boot 向 azure migrate 中的 craete 项目发出 PUT 请求。以前我正在使用 Rest Template。像 @Autowire RestTemplate 模板;

HttpHeaders 标头 = 新的 HttpHeaders(); headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

    MultiValueMap<String, String> map = new LinkedMultiValueMap<>();
    map.add("grant_type", assessment.getGrant_type());
    map.add("client_id", assessment.getClient_id());
    map.add("client_secret", assessment.getClient_secret());
    map.add("code", authToken);

    HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(map, headers);
    ResponseEntity<String> response = restTemplate.exchange(baseUrl, HttpMethod.POST, request, String.class);

authToken 我在 POST 调用之前对身份验证 URL 进行单独的 GET 调用并在 POST 标头中进行设置。

我发布部分代码,以便您了解我在做什么。

现在我被要求使用 Azure-sdk for java 来编写相同的代码。我不需要使用 Rest 模板。谁能告诉我如何使用 Azure-sdk 对 java 类进行 POST 调用。我在谷歌上搜索,你管,但没有找到具体的东西。

问候, 普拉巴什·米什拉

【问题讨论】:

  • 任何人都可以提供示例代码,因为它很紧急。
  • 您是否成功地将 Rest API 与 Azure Migrate 服务结合使用?如果可以,请在答案中上传示例代码。

标签: java spring-boot rest http-post azure-sdk


【解决方案1】:

如果要使用Azure java sdk调用Azure rest API,请参考以下代码

一个。创建服务主体(我使用 Azure CLI 执行此操作)

az login
az account set --subscription "<your subscription id>"
# the sp will have Azure Contributor role
az ad sp create-for-rbac -n "readMetric" 

  1. 安装 SDK
<!-- https://mvnrepository.com/artifact/com.microsoft.azure/azure -->
<dependency>
    <groupId>com.microsoft.azure</groupId>
    <artifactId>azure</artifactId>
    <version>1.33.0</version>
</dependency>

  1. 代码
 private static String tenantId="hanxia.onmicrosoft.com"; // sp tenant
    private static String clientId = "42e0d080-b1f3-40cf-8db6-c4c522d988c4"; // sp appid

    private static String clientKey = "Gbx*************JDfQpIjoae:";// sp password
    private static String subscriptionId="e5b0fcfa-e859-43f3-8d84-5e5fe29f4c68"; //sp subscription id

ApplicationTokenCredentials creds = new
                ApplicationTokenCredentials(clientId,tenantId,clientKey, AzureEnvironment.AZURE);

        RestClient restClient =new RestClient.Builder()
                .withBaseUrl(AzureEnvironment.AZURE, AzureEnvironment.Endpoint.RESOURCE_MANAGER)
                .withSerializerAdapter(new AzureJacksonAdapter())
                .withReadTimeout(150, TimeUnit.SECONDS)
                .withLogLevel(LogLevel.BODY)
                .withResponseBuilderFactory(new AzureResponseBuilder.Factory())
                .withCredentials(creds)
                .build();
        OkHttpClient httpClient = restClient.httpClient().newBuilder().build();
        String url="https://management.azure.com/subscriptions/"+subscriptionId+"/providers/Microsoft.Migrate/projects?api-version=2018-02-02";
        Request request = new Request.Builder()
                              .url(url)
                              .method("get",null)
                              .build();
        Response response1 = httpClient.newCall(request).execute();
        if(response1.isSuccessful()){

            System.out.println(response1.headers().toString());

        }

【讨论】:

  • 我正在使用费率卡 URL:final String getUrl = "management.azure.com/subscriptions/… eq '"+assessment.getOfferDurableId()+"' 和 Currency eq '"+assessment.getCurrency()+ "' 和Locale eq '"+assessment.getLocale()+"' 和 RegionInfo eq '"+assessment.getRegionInfo()+"'";使用 RestTemplate,我正在使用此 URL 获取输出。但使用 Azure SDK 代码时出现错误。
  • ERROR:404 Not Found management.azure.com/subscriptions/… (138 ms, 0-byte body) : 0-byte body: 404 error 表示错误的 URL。请让我知道我在哪里犯了错误。谢谢..
  • 你能帮我解决这个问题吗?
  • @PrabhashMishra 我犯了一个错误。请将请求更新为Request request = new Request.Builder().url(url).get().build(); : i.stack.imgur.com/U2MOo.png
猜你喜欢
  • 2021-03-10
  • 1970-01-01
  • 1970-01-01
  • 2017-02-16
  • 1970-01-01
  • 2015-03-17
  • 1970-01-01
  • 2020-11-26
  • 1970-01-01
相关资源
最近更新 更多