You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Java调用Azure OpenAI服务报ResourceNotFoundException问题求助

关于Azure OpenAI Java SDK调用出现DeploymentNotFound错误的排查

问题描述

在Java 11环境中使用Azure OpenAI Java SDK调用服务时,抛出com.azure.core.exception.ResourceNotFoundException,状态码404,错误码DeploymentNotFound,提示资源的API部署不存在。已确认端点配置正确,部署创建超过10小时,且将azure-ai-openai依赖从1.0.0-beta.1升级至1.0.0-beta.2后问题仍未解决。

代码示例

package com.dolphin.soa.post.config.ai.azure;

import com.azure.ai.openai.OpenAIClient;
import com.azure.ai.openai.OpenAIClientBuilder;
import com.azure.ai.openai.models.*;
import com.azure.core.credential.AzureKeyCredential;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Slf4j
public class AzureOpenAIClient {

    public static void getChatCompletion(){
        String azureOpenaiKey = "the-key";
        String endpoint = "https://reddwarfcv.openai.azure.com/";
        String deploymentOrModelId = "gpt-35-turbo";

        OpenAIClient client = new OpenAIClientBuilder()
                .endpoint(endpoint)
                .credential(new AzureKeyCredential(azureOpenaiKey))
                .buildClient();

        List<ChatMessage> chatMessages = new ArrayList<>();
        chatMessages.add(new ChatMessage(ChatRole.SYSTEM).setContent("You are a helpful assistant."));
        chatMessages.add(new ChatMessage(ChatRole.USER).setContent("Does Azure OpenAI support customer managed keys?"));
        chatMessages.add(new ChatMessage(ChatRole.ASSISTANT).setContent("Yes, customer managed keys are supported by Azure OpenAI?"));
        chatMessages.add(new ChatMessage(ChatRole.USER).setContent("Do other Azure Cognitive Services support this too?"));

        ChatCompletions chatCompletions = client.getChatCompletions(deploymentOrModelId, new ChatCompletionsOptions(chatMessages));

        System.out.printf("Model ID=%s is created at %d.%n", chatCompletions.getId(), chatCompletions.getCreated());
        for (ChatChoice choice : chatCompletions.getChoices()) {
            ChatMessage message = choice.getMessage();
            System.out.printf("Index: %d, Chat Role: %s.%n", choice.getIndex(), message.getRole());
            System.out.println("Message:");
            System.out.println(message.getContent());
        }

        System.out.println();
        CompletionsUsage usage = chatCompletions.getUsage();
        System.out.printf("Usage: number of prompt token is %d, "
                        + "number of completion token is %d, and number of total tokens in request and response is %d.%n",
                usage.getPromptTokens(), usage.getCompletionTokens(), usage.getTotalTokens());
    }

    public static void main(String[] args) {
        getChatCompletion();
    }

}

错误堆栈

com.azure.core.exception.ResourceNotFoundException: Status code 404, "{\"error\":{\"code\":\"DeploymentNotFound\", \"message\":\"The API deployment for this resource does not exist. If you created the deployment within the last 5 minutes, please wait a moment and try again.\"}}"
Exception in thread "main"  at com.azure.core.implementation.http.rest.RestProxyBase.instantiateUnexpectedException(RestProxyBase.java:347)
    at com.azure.core.implementation.http.rest.SyncRestProxy.ensureExpectedStatus(SyncRestProxy.java:130)
    at com.azure.core.implementation.http.rest.SyncRestProxy.handleRestReturnType(SyncRestProxy.java:213)
com.azure.core.exception.ResourceNotFoundException: Status code 404, "{\"error\":{\"code\":\"DeploymentNotFound\", \"message\":\"The API deployment for this resource does not exist. If you created the deployment within the last 5 minutes, please wait a moment and try again.\"}}"
    at com.azure.core.implementation.http.rest.RestProxyBase.instantiateUnexpectedException(RestProxyBase.java:347)
    at com.azure.core.implementation.http.rest.SyncRestProxy.ensureExpectedStatus(SyncRestProxy.java:130)
    at com.azure.core.implementation.http.rest.SyncRestProxy.handleRestReturnType(SyncRestProxy.java:213)
    at com.azure.core.implementation.http.rest.SyncRestProxy.invoke(SyncRestProxy.java:81)
    at com.azure.core.implementation.http.rest.SyncRestProxy.invoke(SyncRestProxy.java:81)
    at com.azure.core.implementation.http.rest.RestProxyBase.invoke(RestProxyBase.java:109)
    at com.azure.core.http.rest.RestProxy.invoke(RestProxy.java:91)
    at jdk.proxy2/jdk.proxy2.$Proxy3.getChatCompletionsSync(Unknown Source)
    at com.azure.core.implementation.http.rest.RestProxyBase.invoke(RestProxyBase.java:109)
    at com.azure.ai.openai.implementation.OpenAIClientImpl.getChatCompletionsWithResponse(OpenAIClientImpl.java:757)
    at com.azure.core.http.rest.RestProxy.invoke(RestProxy.java:91)
    at jdk.proxy2/jdk.proxy2.$Proxy3.getChatCompletionsSync(Unknown Source)
    at com.azure.ai.openai.implementation.OpenAIClientImpl.getChatCompletionsWithResponse(OpenAIClientImpl.java:757)
    at com.azure.ai.openai.OpenAIClient.getChatCompletionsWithResponse(OpenAIClient.java:255)
    at com.azure.ai.openai.OpenAIClient.getChatCompletionsWithResponse(OpenAIClient.java:255)
    at com.azure.ai.openai.OpenAIClient.getChatCompletions(OpenAIClient.java:381)
    at com.dolphin.soa.post.config.ai.azure.AzureOpenAIClient.getChatCompletion(AzureOpenAIClient.java:37)
    at com.azure.ai.openai.OpenAIClient.getChatCompletions(OpenAIClient.java:381)
    at com.dolphin.soa.post.config.ai.azure.AzureOpenAIClient.main(AzureOpenAIClient.java:55)
    at com.dolphin.soa.post.config.ai.azure.AzureOpenAIClient.getChatCompletion(AzureOpenAIClient.java:37)
    at com.dolphin.soa.post.config.ai.azure.AzureOpenAIClient.main(AzureOpenAIClient.java:55)
13:35:44.075 [main] ERROR com.azure.core.implementation.http.rest.RestProxyBase - Status code 404, "{\"error\":{\"code\":\"DeploymentNotFound\", \"message\":\"The API deployment for this resource does not exist. If you created the deployment within the last 5 minutes, please wait a moment and try again.\"}}"
com.azure.core.exception.ResourceNotFoundException: Status code 404, "{\"error\":{\"code\":\"DeploymentNotFound\", \"message\":\"The API deployment for this resource does not exist. If you created the deployment within the last 5 minutes, please wait a moment and try again.\"}}"
    at com.azure.core.implementation.http.rest.RestProxyBase.instantiateUnexpectedException(RestProxyBase.java:347)
    at com.azure.core.implementation.http.rest.SyncRestProxy.ensureExpectedStatus(SyncRestProxy.java:130)
    at com.azure.core.implementation.http.rest.SyncRestProxy.handleRestReturnType(SyncRestProxy.java:213)
    at com.azure.core.implementation.http.rest.SyncRestProxy.invoke(SyncRestProxy.java:81)
    at com.azure.core.implementation.http.rest.RestProxyBase.invoke(RestProxyBase.java:109)
    at com.azure.core.http.rest.RestProxy.invoke(RestProxy.java:91)
    at jdk.proxy2/jdk.proxy2.$Proxy3.getChatCompletionsSync(Unknown Source)
    at com.azure.ai.openai.implementation.OpenAIClientImpl.getChatCompletionsWithResponse(OpenAIClientImpl.java:757)
    at com.azure.ai.openai.OpenAIClient.getChatCompletionsWithResponse(OpenAIClient.java:255)
    at com.azure.ai.openai.OpenAIClient.getChatCompletions(OpenAIClient.java:381)
    at com.dolphin.soa.post.config.ai.azure.AzureOpenAIClient.getChatCompletion(AzureOpenAIClient.java:37)
    at com.dolphin.soa.post.config.ai.azure.AzureOpenAIClient.main(AzureOpenAIClient.java:55)

依赖信息

implementation 'com.azure:azure-ai-openai:1.0.0-beta.1'

排查建议

  • 确认部署名称准确性:Azure OpenAI的部署名称是自定义的,并非模型名称(如gpt-35-turbo)。需登录Azure Portal,进入对应OpenAI资源→部署页面,复制实际的部署名称替换代码中的deploymentOrModelId。
  • 验证API密钥归属:确保使用的API密钥属于创建部署的那个OpenAI资源,且密钥拥有Cognitive Services OpenAI User等相关权限。
  • 检查区域一致性:确认端点的区域与部署所在区域完全匹配,若端点为https://reddwarfcv.openai.azure.com/,需保证部署也在该端点对应的区域内。
  • 指定API版本:beta版本SDK可能需要显式指定API版本,在构建客户端时添加.apiVersion("2023-05-15")(可根据Azure OpenAI支持的最新兼容版本调整),示例:
    OpenAIClient client = new OpenAIClientBuilder()
            .endpoint(endpoint)
            .credential(new AzureKeyCredential(azureOpenaiKey))
            .apiVersion("2023-05-15")
            .buildClient();
    
  • 基础连通性测试:用curl直接调用API验证是否正常,替换<部署名称>和<你的密钥>:
    curl https://reddwarfcv.openai.azure.com/openai/deployments/<部署名称>/chat/completions?api-version=2023-05-15 \
      -H "Content-Type: application/json" \
      -H "api-key: <你的密钥>" \
      -d '{
        "messages": [{"role": "user", "content": "Hello"}]
      }'
    
    若curl报错,说明是Azure端配置问题;若curl成功,需排查SDK代码或环境配置。
  • 检查网络代理:如果运行环境使用代理,需为Azure SDK配置正确的代理参数,避免请求被拦截或路由错误。

内容的提问来源于stack exchange,提问作者Dolphin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 17:47:00