🎯 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 QuestionLog JPA entity that persists every Ollama interaction to H2 (dev) / PostgreSQL (prod)
  • An OllamaService that calls llama3.2:3b via RestClient and saves the result
  • A /api/ai/explain endpoint that templates prompts by topic and level
  • A /api/ai/history endpoint returning all past questions from the database
  • A /api/ai/stats endpoint aggregating question counts by topic via @Query
🏆 Capstone reality: This service is interview-ready. Showing a GitHub repo with Spring Boot + JPA + a real LLM integration is a strong differentiator for backend Java roles in Bangalore and beyond.

🏆 Real-world scenario

🏆
VidyaPath · Bangalore, India
Ship the full-stack AI Java Tutor

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: @Valid DTOs (ChatRequest record)
  • Lesson 23: @RestControllerAdvice error 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, Spring RestClient AI 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.

mvn spring-boot:run
QuestionLog.java + QuestionLogRepository.javaLesson 38 · @Entity, JpaRepository, @Query
verified output — startup with JPA + H2 + Ollama service
:: Spring Boot :: (v3.3.5) INFO --- VidyaPathApplication : Started VidyaPathApplication in 1.52 seconds (process running for 1.623) INFO --- HibernateJpaAutoConfiguration : Initialized JPA EntityManagerFactory for persistence unit 'default'

@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.

curl -s -X POST "http://localhost:8767/api/ai/explain?topic=generics&level=intermediate"
OllamaService.java + AiController.java (explain)Lesson 38 · RestClient, @Service, @PostMapping
verified output — llama3.2:3b response, 10,665 ms{ "model": "llama3.2:3b", "prompt": "Explain 'generics' to a intermediate Java developer...", "response": "Here are three key points about generics in Java: • Type Safety: Generics help prevent type-related errors at compile-time rather than runtime. This means that the compiler checks whether you're using the correct data types...", "durationMs": 10665, "timestamp": "2026-04-26T21:16:50.221800" }

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.

curl -s http://localhost:8767/api/ai/history
AiController.java (history + stats)Lesson 38 · @GetMapping, JPQL aggregate
verified output — 2 questions persisted to H2[ { "durationMs": 10665, "topic": "generics", "id": 1, "askedAt": "2026-04-26T21:16:50.221800", "model": "llama3.2:3b" }, { "durationMs": 7988, "topic": "streams", "id": 2, "askedAt": "2026-04-26T21:16:58.273811", "model": "llama3.2:3b" } ]

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

EasyWhat is the first thing to add when this service goes to production?
+

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.

EasyHow would you limit users to 10 AI questions per hour?
+

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.

MediumHow would you add streaming responses so the user sees tokens as they arrive?
+

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.

MediumHow would you add semantic search over QuestionLog (find similar past questions)?
+

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.

HardHow would you design this as multiple microservices?
+

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

Question 1 of 5
Which annotation marks the JPA entity's primary key?
Question 2 of 5
What does @PrePersist do?
Question 3 of 5
Why use RestClient instead of raw HttpClient for Ollama calls?
Question 4 of 5
What happens to H2 data when the Spring Boot app restarts?
Question 5 of 5
How would you add authentication to protect /api/ai/explain in this capstone?
0
/ 5
Capstone quiz complete!

Next: Capstone — Microservice LLM Pipeline →

⚠️ Common mistakes

Blocking the Spring thread pool with long LLM calls
A 10s LLM call holds a Tomcat worker thread. Under load, the thread pool exhausts. Fix: switch to WebClient + Mono return types, or use virtual threads (spring.threads.virtual.enabled=true in Spring Boot 3.2+).
Storing the full prompt in a VARCHAR(255)
LLM prompts can be hundreds of tokens. Use columnDefinition = "TEXT" or VARCHAR(4096) in @Column. H2 will silently truncate without TEXT, causing corrupted data in production.
No model availability check at startup
If 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.
Exposing raw LLM responses without sanitisation
LLM output can include markdown, code fences, or HTML. Decide on a contract: always plain text, or always markdown. Strip or escape as appropriate before sending to clients to avoid XSS and display issues.

✅ Key takeaways

  • This service combines @Entity + JPA (persistence), RestClient (Ollama), @RestController (API), and constructor injection (DI)
  • @PrePersist auto-sets audit timestamps — never forget createdAt again
  • In-memory H2 for dev; switch datasource URL to PostgreSQL for prod — zero code changes
  • Add SecurityFilterChain from Lesson 25 to protect /api/ai/** with one bean
  • Production checklist: PostgreSQL + Flyway, Spring Security + JWT, WebClient for async LLM, structured logging
Africa & India's Best AI Academy
🤖 AI-first 🎓 Placement Assistance

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 →