AI for Text Summarization with Spring AI in Java

Let’s explore how to build a text summarization tool using Spring AI. The goal is simple. We’ll create a service that accepts long text and returns a concise summary. You’ll see how Spring AI makes this possible with clean Java code and minimal effort.

Why Build a Summarization Tool?

We all deal with long texts daily. Articles, reports, and even documentation can overwhelm readers. A summarization tool solves this by condensing information.

Users save time. Businesses get quick insights. Developers add value to their apps. That’s why summarization matters.

AI enhances this task more effectively than traditional keyword extraction. It understands context, tone, and key points. Instead of chopping sentences, AI creates meaningful summaries.

Spring AI seamlessly integrates these models into Java applications. Instead of worrying about low-level APIs, you work with clean abstractions. This speeds up development and keeps your code maintainable.

So, why not empower your app with this feature?

Introducing Spring AI

Spring AI extends the well-known Spring ecosystem. If you’ve used Spring Boot, this feels familiar. It provides connectors to AI models, including OpenAI and Hugging Face.

You interact with models through simple clients. Behind the scenes, Spring AI manages HTTP calls, authentication, and responses. The developer experience stays clean.

With this abstraction, you can focus on crafting summarization logic. No need to reinvent the wheel.

Setting up the project Spring AI and Gemini

Writing the Summarization Service

Let’s build the heart of the tool. This service accepts text, creates a summarization request, and returns the result.

import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.chat.prompt.PromptTemplate;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class SummarizationService {

    private final ChatClient chatClient;

    public SummarizationService(ChatClient.Builder chatClientBuilder) {
        this.chatClient = chatClientBuilder.build();
    }

    public String summarize(String input) {
        String prompt = """
            Summarize the following text concisely: {input}
            """;

        PromptTemplate promptTemplate = new PromptTemplate(prompt);
        promptTemplate.add("input", input);

        return chatClient.prompt(promptTemplate.create()).call().content();
    }
}

Here’s how it works:

  1. You inject ChatClient, provided by Spring AI.
  2. You create a natural prompt that asks for a summary.
  3. You call the model and return the output.

This is the core engine of the summarizer.

Creating the API Layer

Next, let’s make the service accessible. We’ll add a REST controller to handle requests.

@RestController
@RequestMapping("/summarize")
public class SummarizationController {

    private final SummarizationService service;

    public SummarizationController(SummarizationService service) {
        this.service = service;
    }

    @PostMapping("/normal")
    public String summarize(@RequestBody String text) {
        return service.summarize(text);
    }
}

Now, the API listens at /summarize. You send a text through POST, and it responds with a summary.

This keeps the logic reusable and straightforward. The controller only delegates, while the service handles AI interaction.

Handling Long Inputs

Models have token limits. If someone sends a huge article, you must handle it smartly. One solution is chunking.

Here’s the process:

  1. Break text into chunks of a safe size.
  2. Summarize each chunk.
  3. Combine summaries into a final summary.

This recursive strategy ensures no input overwhelms the model.

//SummarizationService

public String summarizeLargeText(String input) {
    List<String> chunks = splitText(input, 1000);
    List<String> partialSummaries = chunks.stream()
            .map(this::summarize)
            .toList();
    return summarize(String.join(" ", partialSummaries));
}

private List<String> splitText(String input, int maxChunkSize) {
    List<String> chunks = new ArrayList<>();
    if (input == null || input.isEmpty()) {
        return chunks;
    }

    int length = input.length();
    for (int i = 0; i < length; i += maxChunkSize) {
        int endIndex = Math.min(i + maxChunkSize, length);
        chunks.add(input.substring(i, endIndex));
    }
    return chunks;
}
//SummarizationController

@PostMapping("/large")
public String summarizeLargeText(@RequestBody String text) {
    return service.summarizeLargeText(text);
}

This way, you scale your tool for real-world scenarios.

Example practical tips

Adding Control Over Summary Length

Different use cases demand different summary lengths. Sometimes three sentences are enough. Other times, you need a short paragraph.

You can control this through prompts. For example:

String prompt = "Summarize the following text in three sentences:\n" + input;

Or, for a slightly longer version:

String prompt = "Summarize the following text in under 100 words:\n" + input;

This flexibility lets you customize the tool for blogs, reports, or customer support.

Offer Multiple Summary Styles

Sometimes, users prefer bullet points over paragraphs. You can modify prompts to produce structured output:

String prompt = "Summarize this text in five bullet points:\n" + input;

This works great for meeting notes or reports.

Provide a Word Count Comparison

Show users how much text was reduced:

"Original: 1200 words → Summary: 180 words."

It makes the tool feel impactful and professional.

Test Application with Normal and Long Input

Testing the summarization tool is as important as building it. You must check how it behaves with different input sizes. Let’s walk through two cases: standard text and long text.

Normal Input

Send a small paragraph, such as a blog snippet or product description. Use curlor Postman:

http://localhost:8080/summarize/normal
Artificial intelligence helps automate tasks and improve decision-making. It saves time and increases productivity.
Request

The response might look like this:

Response

Long Input

Now, test with a larger block of text. Copy a few pages from a report or research article. Send it to the same endpoint:

http://localhost:8080/summarize/large

Example longtext

Artificial intelligence has rapidly become one of the most influential technologies of the modern era. Every industry, from healthcare to finance, has started adopting AI solutions to improve efficiency and decision-making. Hospitals use AI models to assist doctors with early disease detection. Banks rely on AI to detect fraudulent activities by monitoring unusual transaction patterns in real time. Educators leverage AI-powered tools to personalize learning experiences for students, while businesses use it to forecast demand and optimize supply chains.

Despite its advantages, AI adoption also brings challenges. Many organizations struggle with data privacy and ethical concerns. Users often question how their data is collected, processed, and stored. Governments worldwide are working on regulations to ensure AI is used responsibly. Another concern involves job displacement. While AI creates new roles, it also automates repetitive tasks, raising debates about the future of work.

Beyond challenges, AI continues to inspire innovation. In climate science, researchers use AI to analyze satellite data and predict natural disasters more accurately. In transportation, autonomous vehicles aim to reduce accidents and provide safer roads. Creative industries also benefit. Writers, musicians, and designers experiment with AI tools to expand their creative possibilities. Some critics argue this reduces originality, but others see it as collaboration between human imagination and machine intelligence.

Overall, the influence of AI is undeniable. It transforms daily life, reshapes industries, and challenges us to rethink ethics and responsibility. Whether in short-term business operations or long-term global issues, AI remains at the center of technological progress. The way we guide its development will shape the future of society for decades to come.

This text is approximately 400 words, which is sufficiently long to simulate a “long input” but not too overwhelming for testing purposes.

Request and response

If the text exceeds the model’s limit, the service should chunk it, summarize each part, and then combine them. You’ll receive a concise output that retains the original meaning.

For example, a five-page article could shrink into a few clear paragraphs.

Why Both Tests Matter

Regular input checks basic functionality. Long input checks scalability and reliability. By running both tests, you ensure that the tool works effectively for both casual users and power users.

Best Practices for Summarization Apps

Before you deploy, consider these practices:

  • Handle errors gracefully: AI calls may fail or timeout. Provide clear error messages.
  • Cache results: Save summaries for repeated inputs to reduce costs and improve efficiency.
  • Secure your API: Never expose API keys in the frontend. Keep them server-side.
  • Log usage: Monitor performance and response times to ensure optimal system operation.

These steps ensure your tool remains reliable and efficient.

Expanding the Tool

Once the base works, extend it further:

  • Multi-language support: Summarize in different languages by adjusting prompts.
  • Topic-focused summaries: Ask AI to summarize with emphasis on “technical details” or “business insights.”
  • Integration with chatbots: Offer real-time summarization during conversations.

These additions turn a simple summarizer into a versatile AI assistant.

Finally

We just built a summarization tool using Spring AI and Java. We focused on logic and wrote clean services. You now know how to:

  • Use Spring AI’s ChatClient for summaries
  • Build a REST controller to expose the service
  • Handle significant inputs with chunking
  • Control summary length with prompts
  • Enhance usability with a small UI
  • Apply SEO benefits to your site

Summarization saves time, improves clarity, and boosts engagement. With Spring AI, integrating it is straightforward.

Now it’s your turn. Try the code, experiment with prompts, and customize the tool. Soon, you’ll have a production-ready feature that delights users and strengthens your site.

This article was originally published on Medium.

Leave a Comment

Your email address will not be published. Required fields are marked *