Back to blog
January 22, 20265 min readMarina

Your First Hello World with Spring AI Will Be Easier Than You Think!

SpringSpring AIJava

Your First Hello World with Spring AI Will Be Easier Than You Think!

Spring AI with Ollama

Wanna bet? In just 10 minutes, you can already have your LLM responding!

Exploring Spring AI, nothing surprised me more than how easy it was to create my first Hello World. Of course, there are many cool things you can do with Spring AI, but nothing is more memorable than our first Hello World, right?

Step 1:

Go to Spring Initializr and choose the Spring AI (Ollama) and Web dependencies. I chose Ollama because it's free, just download the model and you're ready to go.

After generating the project, open it in your favorite IDE and let's create the Controller.

Step 2:

To follow a clean architecture that should already feel natural, don't put code directly in the Application class. Instead, create a Controller package and add a HelloWorldController class.

HelloWorldController class
@RestController
@RequestMapping("/assistant")
public class HelloWorldController {

    private final ChatClient chatClient;

    public HelloWorldController(ChatClient chatClient) {
        this.chatClient = chatClient;
    }

    @GetMapping
    public String ask(@RequestParam String question) {
        return chatClient.call(question);
    }
}

Step 3:

Run the application and access:

`http://localhost:8080/assistant?question=Hello`

Done! That's your first interaction with your LLM via Spring AI!

This is just the beginning of what it can do. Can you imagine all the cool projects we could build with this tool?

Summary:

  • In the ask method, `.content()` returns a String, perfect for simple chatbots.
  • It's the foundation for building more complex assistants, adding memory, data, or even integrating with external APIs.
  • You can change the model in `.model("ollama")` to any other model you have access to.
  • A small tutorial like this opens up many possibilities: testing different models and seeing how the responses differ, analyzing response performance, and much more.