🎯 What you will learn
You don't need a heavyweight AI SDK to call a language model from Java. Ollama exposes a simple REST API — the same one curl uses. Java 11's built-in java.net.http.HttpClient is all you need. By the end of this lesson you will be able to:
- POST to Ollama's
/api/generateendpoint from plain Java - Use
HttpClient,HttpRequest, andHttpResponsefromjava.net.http - Serialize a request body to JSON with Jackson's
ObjectMapper - Parse the JSON response into a
JsonNodetree - GET
/api/tagsto list all locally installed models - Understand the difference between
/api/generateand/api/chat
🎓 Real-world scenario
VidyaPath adds AI-generated concept explanations to its platform. Rather than importing a heavy AI SDK, the team uses Java 11+'s built-in HttpClient to POST directly to Ollama's REST API — the same HTTP call any curl command would make.
🌐 Basic Ollama HTTP call
The OllamaClient class holds the HTTP client (singleton, thread-safe) and an ObjectMapper for JSON. The generate method builds a request with Map.of(), serializes it, sends it, and extracts the "response" field from the returned JSON.
Why stream=false? By default Ollama streams tokens as they are generated (Server-Sent Events / NDJSON). Setting "stream": false tells Ollama to wait until the model finishes, then return one complete JSON object. Simpler to parse for one-shot use cases.
💬 Factual Q&A with llama3.2:3b
The main method calls generate twice — once for a factual question and once for code generation. Both are simple prompt strings; the model decides the format of the response.
📋 List available models via /api/tags
Before sending a prompt, it's good practice to verify the model exists locally. A simple GET /api/tags returns a JSON array of installed models with their sizes and parameter counts.
📋 Ollama API endpoints at a glance
| Method | Endpoint | Purpose | Key body fields |
|---|---|---|---|
POST | /api/generate | Single prompt completion | model, prompt, stream |
POST | /api/chat | Multi-turn chat (messages array) | model, messages, stream |
POST | /api/embed | Generate vector embeddings (RAG) | model, input |
GET | /api/tags | List installed models | — |
GET | /api/show | Model details (context length, params) | name (query param) |
🎤 Interview Q&A
Ollama is a local LLM server that runs open models (llama3, mistral, phi, gemma etc.) on your machine. No API key, no cost, no data sent to the cloud — ideal for development and privacy-sensitive applications. The REST API is almost identical to OpenAI's, so switching to a cloud provider later is straightforward.
/api/generate is for single-turn prompts: you send one string. /api/chat takes a messages array like OpenAI's Chat API format: [{"role":"user","content":"..."}, {"role":"assistant","content":"..."}] — for multi-turn conversation with history. Use /api/generate for simple one-shot tasks; /api/chat for chatbots that maintain context.
Set stream=true in the request. Ollama returns newline-delimited JSON objects (NDJSON). Use HttpResponse.BodyHandlers.ofLines() to get a Stream<String>; parse each line and extract "response" until "done":true. Each line is a partial token — concatenate them for the full response. This approach lets you forward tokens to a browser via SSE in real time.
For code: deepseek-r1, codellama, phi3.5. For general Q&A: llama3.2, mistral, gemma2. Smaller models (3B–7B) run well on a laptop GPU or even CPU. Larger models (13B+) need a GPU with 8GB+ VRAM. For a tutor application, llama3.2:3b gives a good balance of quality and speed on a 16GB MacBook.
Set .timeout(Duration.ofSeconds(60)) on HttpRequest for per-request timeout. For retry: use a loop with exponential backoff — Thread.sleep(Math.pow(2, attempt) * 1000) — catching IOException and HttpTimeoutException. Or use Resilience4j's @Retry annotation in Spring Boot for declarative retry with configurable max attempts and backoff strategy.
📝 Quiz — 5 questions
"stream": false mean in the Ollama request body?HttpRequest.BodyPublishers.ofString(body) do?Next: Spring Boot AI Gateway →
⚠️ Common mistakes
stream=false, Ollama returns streaming NDJSON lines. Parsing the body as a single JSON object will fail with a parse error because the response is multiple lines, each a partial JSON object.HttpResponse.BodyHandlers.ofString() buffers the whole response in memory — fine for small models but be mindful for large contexts (32k+ tokens).@Value("${ollama.default-model}") in Spring Boot so you can switch models in config without code changes.Content-Type: application/json Ollama returns a 400 error or parses the body incorrectly. Always set it — .header("Content-Type", "application/json") — before calling .POST().✅ Key takeaways
- java.net.http.HttpClient (Java 11+) makes REST calls with no extra dependencies
- Ollama's POST /api/generate accepts model + prompt; set stream=false for simple use
- Jackson's ObjectMapper.readTree() parses the JSON response into a traversable tree
- GET /api/tags lists all installed models — use to validate availability at startup
- Abstract the LLM call into a service class — swap Ollama for OpenAI/Anthropic in one place
From HttpClient to a production AI service
Join the Best AI Programs — three 5-month courses, fully online, with placement assistance. Week 1 is free.
Explore our AI-era courses →