Setting Up Trae IDE for Spring AI: A Quick‑Start Guide

This article walks Java developers through installing Trae IDE, configuring the required JDK, Maven and Spring AI dependencies, creating a Spring Boot project, adding AI model settings, implementing a chat controller with synchronous and streaming endpoints, and troubleshooting common issues.

Architecture Digest
Architecture Digest
Architecture Digest
Setting Up Trae IDE for Spring AI: A Quick‑Start Guide

Why Trae + Spring AI?

In 2026 the Java AI toolchain converges on a high‑value combo: Spring AI provides a unified model abstraction layer that makes swapping models as easy as changing a database driver, while Trae IDE, an AI‑native editor built on the VS Code core, boosts development efficiency.

Spring AI offers out‑of‑the‑box RAG, tool calling and model‑centered programming (MCP). Trae IDE, released by ByteDance, gives three compelling advantages for Java developers: built‑in AI models (e.g., Doubao, DeepSeek) optimized for Chinese, a Builder mode that can generate multiple related files from a single instruction, and free usage with an IntelliJ IDEA key‑map for zero‑cost migration.

Environment Setup

Software requirements

JDK 21+ (Spring AI 2.0 requires at least JDK 17; 21 LTS is recommended)

Maven 3.8+

Trae IDE latest version (Windows/macOS/Linux)

Verify JDK installation with java -version and javac -version. If missing, download JDK 21 LTS from Adoptium and set JAVA_HOME.

In Trae, press Ctrl+Shift+X to open the Extensions panel and install Extension Pack for Java (language support, debugger, Maven integration).

Press Ctrl+Shift+P, run Java: Configure Java Runtime, and point the runtime to your JDK directory (e.g., C:\Program Files\Java\jdk-21), then reload the window.

Create a Spring AI Project

Generate a skeleton with Spring Initializr ( start.spring.io) or let Trae’s Builder mode do it. Core parameters:

Spring Boot 4.0.x

Java 21

Dependencies: Spring Web + OpenAI (Spring AI)

Open the project root in Trae and wait for Maven indexing to finish.

Core Dependency and API‑Key Configuration

Add the following to pom.xml:

<!-- Spring AI: OpenAI‑compatible starter (supports Doubao, DeepSeek, etc.) -->
<dependency>
  <groupId>org.springframework.ai</groupId>
  <artifactId>spring-ai-openai-spring-boot-starter</artifactId>
</dependency>

Spring AI 2.0 is GA and can be pulled directly from Maven Central.

Obtain a DeepSeek API key (the most cost‑effective model) and set it as an environment variable DEEPSEEK_API_KEY=sk‑xxxx (do not hard‑code it).

Configure application.yml:

spring:
  ai:
    openai:
      base-url: https://api.deepseek.com/v1   # DeepSeek follows the OpenAI protocol
      api-key: ${DEEPSEEK_API_KEY}
      chat:
        options:
          model: deepseek-chat   # V4‑Flash official model
          temperature: 0.7
          connect-timeout: 30s
          read-timeout: 120s   # increase for slow generation

Implement the Chat Controller

Create ChatController.java:

@RestController
@RequestMapping("/api/chat")
public class ChatController {

    private final ChatClient chatClient;

    public ChatController(ChatClient.Builder builder) {
        this.chatClient = builder
            .defaultSystem("你是架构文摘的Java技术助手,回答简洁专业,优先给代码示例。")
            .build();
    }

    // Synchronous Q&A: GET /api/chat/ask?q=xxx
    @GetMapping("/ask")
    public String ask(@RequestParam String q) {
        return chatClient.prompt()
            .user(q)
            .call()
            .content();
    }

    // Streaming Q&A: GET /api/chat/stream?q=xxx
    @GetMapping(value = "/stream", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> stream(@RequestParam String q) {
        return chatClient.prompt()
            .user(q)
            .stream()
            .content()
            .timeout(Duration.ofSeconds(60))
            .onErrorResume(e -> Flux.just("[生成失败,请稍后重试]"));
    }
}

The Builder mode can generate this file automatically, but writing it once helps you understand each annotation.

Debugging and Running

Press Ctrl+Shift+PJava: Configure Java Runtime to confirm the JDK.

Open DemoApplication.java and click the Run | Debug icon (or press F5) to start the application.

Set a breakpoint on chatClient.prompt() to inspect prompt assembly.

When the console shows Started DemoApplication in X seconds, the service is ready.

Test the endpoints:

# Synchronous query
curl "http://localhost:8080/api/chat/ask?q=SpringAI的Advisor是什么"

# Streaming output (use -N to disable buffering)
curl -N "http://localhost:8080/api/chat/stream?q=用三句话解释RAG"

You can also invoke /stream from Trae’s AI panel to see incremental output.

Common Issues and Solutions

Java files show errors / no code completion – Install the Extension Pack for Java and configure the JDK runtime.

"ChatClient bean not found" at startup – Ensure api-key is set in application.yml or as an environment variable, then restart Trae (IDE does not hot‑reload system env).

HTTP 401 response – Verify the API key is correct and the account has sufficient quota.

HTTP 404 response – Use the correct base-url: DeepSeek requires /v1, while other models may need /compatible-mode/v1.

Frequent read timeout – Increase read-timeout to > 120 s.

Streaming endpoint returns no data – Use curl -N, an EventSource client, or WebFlux to consume Server‑Sent Events.

Maven cannot resolve spring‑ai artifacts – Spring AI 2.0 is GA; use version 2.0.x without milestone repositories.

Conclusion and Outlook

Three key takeaways:

Environment layer : Trae is VS Code‑based; manual Java extension and JDK configuration are the most common stumbling blocks.

Integration layer : Spring AI’s unified abstraction hides model differences; once base-url, api-key and model are set, ChatClient can be injected and used directly.

Practice layer : Both call() (synchronous) and stream() (streaming) work out of the box—remember to add timeout and error fallback for streaming.

Future extensions include adding ChatMemory for multi‑turn context, integrating a Milvus vector store for private‑document retrieval, and using the @Tool annotation to let the LLM invoke your business APIs.

Trae speeds up coding; Spring AI keeps the code alive—an essential combo for every Java developer’s toolbox.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaAIspring-bootSpring AIAPI KeyBuilder modeTrae IDE
Architecture Digest
Written by

Architecture Digest

Focusing on Java backend development, covering application architecture from top-tier internet companies (high availability, high performance, high stability), big data, machine learning, Java architecture, and other popular fields.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.