{}const=>[]async()letfn</>var
Development

How to write your first REST API on Spring Boot

A simple guide for beginners: how to create the first REST API on Spring Boot, write controllers and get the first answers in JSON.

К

Kodik

Author

2 min read

Spring Boot has long been the standard for creating backend applications in Java. Its strength lies in its simplicity and speed: you can get a working REST API up and running in just a few minutes. In this article, we will figure out how to take the first step.

REST API is a way of communication between the client and the server through HTTP requests. A few examples:

  • GET /users — get a list of users

  • POST /users — create a new user

  • DELETE /users/1 — remove user with ID = 1

Step 1. Create a project

The fastest way to start is through Spring Initializr:

  1. Specify the language — Java.

  2. Choose the dependency: Spring Web.

  3. Download and open the project in IDE (IntelliJ IDEA, Eclipse or VS Code).

🔥 100,000+ students already with us

Tired of reading theory?
Time to code!

Kodik — an app where you learn to code through practice. AI mentor, interactive lessons, real projects.

🤖 AI 24/7
🎓 Certificates
💰 Free
🚀 Start learning
Joined today

Step 2. First controller

In the src/main/java folder, create a class:


import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

    @GetMapping("/hello")
    public String hello() {
        return "Hi, this is my first REST API!";
    }
}

What's going on?

  • @RestController — the class is responsible for REST requests.

  • @GetMapping("/hello") — processes a GET request at /hello.

  • The method returns a string, and it is displayed in the browser.

Step 3. Launch the application

The project already has a class with the annotation @SpringBootApplication. We run it. We go to http://localhost:8080/hello in the browser — and we see the response from the API. 🎉

Step 4. Working with data

Let's add a list of users:


import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

@RestController
public class UserController {

    @GetMapping("/users")
    public List<String> getUsers() {
        return List.of("Alice", "Bob", "Eva");
    }
}

Now when we request /users we will get JSON:


["Alice", "Bob", "Eva"]

What's next?

  • Add POST, PUT, DELETE methods.

  • Connect the database (Spring Data JPA).

  • Configure tests and documentation (e.g., Swagger).

Conclusion

Spring Boot allows you to quickly deploy a REST API: from the first request to working with data — just a few lines of code. Once you have mastered the basics, you can build full-fledged services, from training projects to microservices.

To feel confident in Java and Spring Boot, it is important to understand the basics of programming. In the application Code you can learn Python, JavaScript, Lua and other languages, take courses and get a certificate. This is a great starting point for moving to serious backend frameworks.

And we also have an active Telegram channel, where we discuss cool ideas, share experiences and analyze tasks together — learning becomes not only useful, but also fun.

🎯Stop procrastinating

Liked the article?
Time to practice!

In Kodik, you don't just read — you write code immediately. Theory + practice = real skills.

Instant practice
🧠AI explains code
🏆Certificate

No registration • No card