Java Optional chain used properly in Spring Boot LLM app
Borys Nikolenko
0:00 / 0:00
Java Optional chain used properly in Spring Boot LLM app
85 просмотров · 10 дней назад
Borys Nikolenko
63 подписчика
85 просмотров · 10 дней назад
The category of an expense was decided entirely by the model. When it got
one wrong, you could fix it in the app — and the next purchase from the
same shop would arrive wrong again, because nothing remembered the fix.
So the order gets inverted. What the same merchant was categorised as
before comes first, and the model's answer becomes the fallback behind it.
That is one expression: a lookup that may find nothing, or the parsed
category, or NOT_RECOGNIZED when neither answers.
Along the way: why a query that returns Optional needs flatMap and not map,
why or() takes a supplier, and why "Other" is the wrong name for a value
that means nobody knows. Also two full reversals — I move the category into
a record and then move it back to a plain string, and both are left in.
This is real code from an expense tracker I build in public, on Java 25 and
Spring Boot. Nothing is a textbook example.
00:00 The category the model got wrong
00:34 Making the model a fallback instead of the source
01:25 A class to resolve the category
02:15 String, or a record?
03:23 Other becomes NOT_RECOGNIZED
04:22 Where a category can come from
05:20 Reading the account's own history
05:57 A derived query by account and merchant
06:41 The latest one, ordered by created date
07:18 The merchant can be null
08:49 Optional inside Optional, and flatMap
10:32 orElse and the not-recognized fallback
11:13 Falling back to the model
12:35 orElseGet and the supplier
13:03 Fix it once instead of every time
13:37 Back to a plain string
14:58 Wrap-up
Source code: https://github.com/Bogr11/cash-lens-y...
Full series: • Building in Public: AI Expense Tracker wit...
#Java #Optional #SpringBoot