













1. pom
<properties>
<java.version>17</java.version>
<spring-ai.version>2.0.0-M7</spring-ai.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webmvc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-starter-model-openai</artifactId>
</dependency><dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webmvc-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-bom</artifactId>
<version>${spring-ai.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
2. application.yml
${OPENAI_API_KEY}

spring:
application:
name: dashscope18088
ai:
openai:
api-key: ${OPENAI_API_KEY}
base-url: https://dashscope.aliyuncs.com/compatible-mode/v1
chat:
model: qwen3.6-plus
# chat:
# model: qwen3.6-plus
# 可选:超时/重试配置
# options:
# temperature: 0.7
# max-tokens: 2000
server:
port: 18088
3. config
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class AiConfiguration {@Bean
public ChatClient chatClient(OpenAiChatModel model){
return ChatClient
.builder(model) // 绑定OpenAiChatModel模型
.build(); // 构建ChatClient实例
}
}
4. controller
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class MyAiChatController {@Autowired
private ChatClient chatClient;@RequestMapping("/ai")
public String ai(String question){
return chatClient.prompt() // 创建 Prompt 对象,用于构建聊天请求
.user(question) // 设置用户输入
.call() // 调用模型,返回结果
.content(); // 获取结果内容
}
}
5. Visit
http://localhost:18088/ai?question=你是谁?

posted on 2026-05-27 21:34 cn2025 Views (0) Comments () Bookmark Report
This content is automatically aggregated by InertiaRSS (RSS Reader) for reading reference only. Original from — Copyright belongs to the original author.