Azure OpenAI Java示例遇SLF4J加载失败及404资源未找到问题求助
问题排查方案
一、SLF4J日志加载问题
- 替换日志绑定依赖:
slf4j-log4j12是针对Log4j 1.x的绑定,该版本已停止维护,对OpenJDK 21兼容性较差。建议改用轻量级的slf4j-simple或兼容高版本Java的Log4j 2.x绑定,在pom.xml中添加以下依赖(二选一):
或者<!-- slf4j-simple 轻量实现 --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> <version>2.0.7</version> <scope>runtime</scope> </dependency><!-- Log4j 2.x 绑定 --> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-slf4j-impl</artifactId> <version>2.20.0</version> <scope>runtime</scope> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.20.0</version> <scope>runtime</scope> </dependency> - 清理依赖冲突:执行
mvn dependency:tree命令检查是否存在多个SLF4J绑定或版本不一致的情况,排除冲突的依赖(比如其他依赖自带的旧版SLF4J或Log4j)。 - 刷新IDE依赖:在IntelliJ IDEA中执行
Reload All Maven Projects,确保新依赖正确加载。
二、ResourceNotFoundException(404)问题
- 验证API密钥:确认代码中使用的API密钥是对应Azure OpenAI资源的有效密钥(可在Azure门户“密钥和终结点”页面获取),无拼写错误且未过期。
- 显式指定API版本:Azure OpenAI API需要指定正确的
api-version,旧beta版本依赖可能默认使用过时版本,需在代码中显式设置,例如:OpenAIClient client = new OpenAIClientBuilder() .endpoint(endpoint) .credential(new AzureKeyCredential(apiKey)) .apiVersion("2023-05-15") // 或当前可用的稳定版本 .buildClient(); - 匹配模型部署与调用接口:若示例代码调用聊天补全接口(
getChatCompletions),需确保部署的是聊天模型(如gpt-35-turbo、gpt-4);若调用文本补全接口,则需部署对应文本模型,类型不匹配会触发404。 - 升级Azure OpenAI依赖:
1.0.0-beta.5为早期测试版本,存在已知兼容性问题,建议升级至最新稳定版(如1.3.0),修改pom.xml中的依赖:<dependency> <groupId>com.azure</groupId> <artifactId>azure-ai-openai</artifactId> <version>1.3.0</version> </dependency> - 测试网络连通性:用curl命令直接验证endpoint是否可访问,例如:
若curl返回404,问题出在Azure资源配置或网络;若返回正常,则排查代码参数设置。curl -v https://<your-resource-name>.openai.azure.com/openai/deployments/<your-deployment-id>/chat/completions?api-version=2023-05-15 \ -H "Content-Type: application/json" \ -H "api-key: <your-api-key>" \ -d '{"messages":[{"role":"user","content":"Hello"}]}' - 确认区域模型支持:检查Azure OpenAI资源所在区域是否支持当前部署的模型,部分模型仅在特定区域可用。
内容的提问来源于stack exchange,提问作者Jeff
相关产品推荐
相关产品推荐

