## First, Let Me Talk About How I Botched the First Version
First, Let Me Talk About How I Botched the First Version
Last month I took over a small Spring Boot project and needed to add an AI summarization feature. My thinking at the time was very "engineer-like": I hadn't carefully read the Spring AI docs, but isn't OpenAI's HTTP interface just a few JSON calls? Why not just hand-write the requests with RestTemplate?
So I pulled up the official SDK dependencies, stuffed the OpenAI Java SDK directly into pom.xml, wrote a key I'd obtained from somewhere into the config file, and excitedly ran it.
The result was a chain of disasters:
Pitfall #1: The official Java SDK only recognizes the official endpoint by default. I wanted to point it to a compatible gateway, and it took me forever digging through constructors to find that I had to manually change baseUrl. After changing it, the versions didn't match—the SDK had been upgraded and the API changed, so compilation failed outright.
Pitfall #2: The key was hard-coded in the config and I committed it to Git. By the time a colleague reminded me, the repo had already been pushed twice. I had to delete the key, rewrite history, and reissue keys—a whole morning gone.
Pitfall #3: No timeouts or retries. Model responses were occasionally slow, and RestTemplate waits indefinitely by default. One hanging request tied up the entire thread pool, and the API cascaded into failure.
Pitfall #4 (the most painful): Single vendor—when they say a model is deprecated, it's deprecated. A model version I depended on got adjusted, my code had the model name hard-coded, and I got woken up by alerts at midnight to change the config and redeploy.
If you're also planning to integrate AI capabilities into a Spring Boot project, you'll probably hit at least one of these four pitfalls. Here's the path that finally worked for me.
The Right Path: OpenAI-Compatible Gateway + Spring AI
The core idea is: don't tie your project to any single vendor's SDK and endpoint—instead, route everything through an aggregation gateway that speaks the OpenAI-compatible protocol. That way, when you switch models or vendors, you only change one model name string in your code.
The gateway I'm using here is ThisToken.AI. It exposes a standard OpenAI-compatible interface, which means Spring AI's OpenAI starter can connect seamlessly—you just need to change base-url to the gateway address.
Step 1: Register and Get an API Key
- Open the ThisToken.AI registration page and sign up with your email;
- Go to the console, find API Key management, and create a new key;
- Save it immediately to a local password manager or environment variable—the page usually only displays it in full once;
- For available models and billing details, refer to the official pricing page—don't trust numbers floating around online.
Step 2: Inject the Key via Environment Variable
Don't write the key into application.yml and commit it. The correct approach:
export THISTOKEN_API_KEY=sk-your-keyStep 3: Spring Boot Configuration
Add Spring AI's OpenAI starter to pom.xml (check the Spring AI official docs for the version), then in application.yml:
spring:
ai:
openai:
base-url: https://api.thistoken.ai/v1
api-key: ${THISTOKEN_API_KEY}
chat:
options:
model: gpt-4o-miniNote that base-url points to the gateway—all subsequent requests go through it. To switch models, just change the model line.
Step 4: Run Your First Piece of Code
Here's a minimal working example. Although the main project is Java, I suggest you first verify that the key and gateway work with a quick script—ruling out Spring Boot itself as a variable makes troubleshooting much faster:
from openai import OpenAI
client = OpenAI(
api_key="sk-你的key", # 生产环境请用环境变量
base_url="https://api.thistoken.ai/v1"
)
resp = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": "你是一个简洁的中文摘要助手。"},
{"role": "user", "content": "用一句话总结:Spring Boot 通过 OpenAI 兼容网关接入多个模型,可以避免绑定单一供应商。"}
]
)
print(resp.choices[0].message.content)If it prints out a summary, that means the key, gateway, and model are all working. Then you can go back to Spring Boot and write a ChatClient call, without the confusion of "not knowing which layer is broken."
The Three Engineering Practices I Skipped Initially
Getting it running is just the passing grade. For production, you also need:
Timeouts and retries. Set reasonable connect and read timeouts on the HTTP client, and do limited retries with backoff for retryable errors—don't let one slow request drag down your thread pool.
Key isolation. If you have frontend or mini-program clients that need direct access, never hand out the server-side key. Add a server-side proxy layer and keep the key only in server environment variables.
Centralized model name management. Consolidate model names into config or constants, and combine with the gateway to switch between multiple models—so a vendor's adjustments won't force you into a midnight deployment.
Final Thoughts
Looking back, my initial failure wasn't really a technical problem—it was failing to think through the architectural boundaries of "integrating AI." Hand-writing HTTP and locking into a single SDK saves effort short-term but is all debt long-term. With the OpenAI-compatible gateway approach, a Spring Boot project depends only on one standard protocol, and no matter how you switch models later, the cost is minimal.
If you want to try it, register an account first, use the Python code above to get your first call working within ten minutes, and then decide whether to go deeper in your project: https://api.thistoken.ai/register
---
Tired of juggling provider integrations? Register at https://api.thistoken.ai/register and call every model through one base_url.
Bạn muốn thử Token.AI?
Tạo API Key cấp dự án, bật kênh trong bảng điều khiển và định cấu hình định tuyến, ngân sách và nhật ký kiểm tra.
注册 ThisToken.AI 并获取 API Key