Summarize PDFs with AI in Java using Spring AI and webforJ

You can summarize PDFs with AI in Java by letting the user upload the file in a webforJ view, extracting the text with Spring AI's Tika reader, and sending that text to a chat model. The summary runs on a background thread, so the page stays responsive while the model works. You will build the whole process in three Java files, with no REST controller and no JavaScript. The complete app, Summarize, is on GitHub and runs with your own Gemini API key.

What you will build
The finished page has one Upload component and one MarkdownViewer. The user picks a PDF, DOCX, or text file, and the viewer shows a short summary of the document. The model answers in markdown, so the viewer renders its bullet points as a list. Each part has one job:
- The
Uploadcomponent receives the file from the browser and hands it to the server. - A Spring service extracts the text and calls the model.
- The view moves the file out of the upload cache and writes the result to the page when the service finishes.
The model call takes seconds. If a click handler waited for it, the UI thread would stall and the page would freeze. The service returns a CompletableFuture instead, and the view uses Environment.runLater() to update components when the result arrives. The background jobs guide covers this pattern in more detail.
Add the dependencies
Add these artifacts to your webforJ Spring Boot project:
spring-ai-client-chat, which provides theChatClienttype.spring-ai-tika-document-reader, which providesTikaDocumentReader. The reader extracts text from formats such as PDF, DOC and DOCX, PPT and PPTX, and HTML.- A chat model starter for your provider. The Summarize app uses
spring-ai-starter-model-google-genaifor Gemini. The starter enables Spring AI's auto-configuration, which creates theChatClient.Builderbean that the service receives in its constructor. Set the API key your provider expects in your configuration before you run the app.
The snippets target webforJ 26.03, Spring Boot 4.1.0, and Spring AI 2.0.0. The Spring AI reference lists the starters for other providers.
