🎯 What you will build
This capstone assembles every major concept from Arcs D through G into one deployable Spring Boot service. By the end of this lesson you will have shipped:
- A
QuestionLogJPA entity that persists every Ollama interaction to H2 (dev) / PostgreSQL (prod) - An
OllamaServicethat callsllama3.2:3bviaRestClientand saves the result - A
/api/ai/explainendpoint that templates prompts by topic and level - A
/api/ai/historyendpoint returning all past questions from the database - A
/api/ai/statsendpoint aggregating question counts by topic via@Query
🏆 Real-world scenario
Everything from Arcs D–G comes together: Spring Boot setup, REST controllers, constructor injection, validated DTOs, error handling, config profiles, JPA persistence, and Ollama LLM integration. Riya ships the VidyaPath AI Tutor — a service that accepts Java concept questions, calls the LLM, persists every interaction to H2, and exposes usage statistics.
📦 What's in this capstone
🗂️ Arc D–G concepts wired together
- Lesson 19–20:
@SpringBootApplication,@RestController, HTTP verbs - Lesson 21: Constructor injection (
@Service,@Repository) - Lesson 22:
@ValidDTOs (ChatRequestrecord) - Lesson 23:
@RestControllerAdviceerror handling - Lesson 24:
application.properties+ H2 dev / PostgreSQL prod - Lesson 25: HTTP Basic security (optional extension)
- Lesson 27–28: JdbcTemplate + JPA (
@Entity,JpaRepository) - Lesson 29–30:
@Query,@Transactional - Lesson 34–35:
HttpClient→ Ollama, SpringRestClientAI service - Lesson 36–37: RAG context, prompt templates
🗃️ Block 1 — QuestionLog entity + JPA persistence
Every LLM interaction is persisted to a question_logs table. The @PrePersist lifecycle hook sets askedAt automatically — no manual timestamp management. JpaRepository provides save, findAll, and custom query methods for free.
@PrePersist tip: The askedAt field is set automatically by the JPA lifecycle callback before the first INSERT. Never forget to add audit timestamps — just annotate a method with @PrePersist and it runs before every save.
🤖 Block 2 — OllamaService + AI explain endpoint
OllamaService wraps the Ollama REST API using Spring's RestClient. It measures call duration, extracts the response text from the JSON payload, persists a QuestionLog record, and returns a structured ChatResponse record to the controller.
Constructor injection: OllamaService receives its QuestionLogRepository through the constructor — no field injection, no @Autowired on the field. Spring resolves the dependency automatically when it constructs the bean. This makes the class testable with plain new OllamaService(mockRepo).
📊 Block 3 — Persistent history + stats
The /history endpoint reads all persisted interactions from H2. The /stats endpoint runs the JPQL aggregate query from the repository and returns topic counts — no SQL written by hand.
Stream + toList(): The Stream pipeline on logRepo.findAll() uses Java 16's .toList() shorthand instead of Collectors.toList(). The result is an unmodifiable list — safe to return directly from the controller.
🎤 Interview Q&A
Add PostgreSQL datasource, Flyway migrations, Spring Security with JWT, and structured logging (JSON format for log aggregation). Replace H2 with the real database. Update application-prod.properties with the PostgreSQL JDBC URL and set ddl-auto=validate so Flyway owns schema changes — never create-drop in production.
Add a rate_limits table or use Redis. Before calling generate(), check how many questions the user has asked in the last hour: SELECT COUNT(*) WHERE user_id=? AND asked_at > NOW()-1hour. If the count is ≥ 10, return 429 Too Many Requests. Redis is preferable because the check and increment are atomic with INCR + EXPIRE.
Change generate() return type to Flux<String> (reactive). In OllamaService use WebClient (not RestClient). Set stream=true in the Ollama body. Use Server-Sent Events: @GetMapping(produces=MediaType.TEXT_EVENT_STREAM_VALUE) and return Flux<ServerSentEvent>. The browser receives each token chunk as a separate SSE message.
Add an embedding column to QuestionLog. When saving a new question, also generate and store its embedding via Ollama's /api/embed endpoint. At query time, embed the user's question and run a cosine similarity search using pgvector: ORDER BY embedding <=> query_vec LIMIT 3. Return the top-3 similar past questions as context for the new prompt.
Split into: (1) api-gateway (Spring Cloud Gateway for routing and auth), (2) tutor-service (this capstone — LLM calls + DB), (3) embedding-service (dedicated Ollama embed calls), (4) knowledge-service (vector search). Use OpenAPI contracts between services, Kafka for async question logging, and service discovery (Consul or Kubernetes DNS). Each service has its own database — no shared schema.
📝 Quiz — 5 questions
@PrePersist do?RestClient instead of raw HttpClient for Ollama calls?/api/ai/explain in this capstone?Next: Capstone — Microservice LLM Pipeline →
⚠️ Common mistakes
spring.threads.virtual.enabled=true in Spring Boot 3.2+).columnDefinition = "TEXT" or VARCHAR(4096) in @Column. H2 will silently truncate without TEXT, causing corrupted data in production.llama3.2:3b is not installed and Ollama returns 404, every /explain call fails. Add an @EventListener(ApplicationReadyEvent.class) that calls GET /api/tags and logs a warning if the configured model is absent.✅ Key takeaways
- This service combines
@Entity+ JPA (persistence),RestClient(Ollama),@RestController(API), and constructor injection (DI) @PrePersistauto-sets audit timestamps — never forgetcreatedAtagain- In-memory H2 for dev; switch datasource URL to PostgreSQL for prod — zero code changes
- Add
SecurityFilterChainfrom Lesson 25 to protect/api/ai/**with one bean - Production checklist: PostgreSQL + Flyway, Spring Security + JWT, WebClient for async LLM, structured logging
From AI Tutor to LLM Pipeline
Join the Best AI Programs — three 5-month courses, fully online, with placement assistance. Week 1 is free.
Explore our AI-era courses →