Spring Boot makes building apps fast and pleasant. Instead of fiddling with tons of config, we can jump straight into writing code. Let’s see how it works.
1. The app creation process
Our journey starts with creating a project. Like a good craftsman picks the right tools for the job, we'll pick the right tools for our Spring Boot app. The main helper here is Spring Initializr, which will take care of most of the project setup boilerplate.
Step 1. Use Spring Initializr
Spring Initializr is a tool that makes project setup really easy: you just fill out a form. You can open https://start.spring.io/ and choose the following options for our project:
- Project type: Maven or Gradle (we pick Maven to keep things simple).
- Language: Java.
- Spring Boot version: the latest stable version (for example, 3.0.0).
- Artifact:
hello-world-app. - Dependencies:
- Spring Web (for building REST APIs),
- Spring Boot DevTools (for automatic reloads).
Click the "Generate" button and download the zip. Unzip it somewhere convenient and open it in your favorite IDE (for example, IntelliJ IDEA or Eclipse).
Spring Initializr is often called the "magic button" because it really saves us from a lot of boilerplate. One click and you have a ready project!
Step 2. Peek inside the project
Open the project and you'll see the standard Spring Boot application structure:
src/main/java
└── com.example.helloworldapp
└── HelloWorldAppApplication.java
src/main/resources
├── application.properties
└── static/ (for static files, e.g., CSS and JS)
The key file is HelloWorldAppApplication.java. It contains your application's entry point:
package com.example.helloworldapp;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class HelloWorldAppApplication {
public static void main(String[] args) {
SpringApplication.run(HelloWorldAppApplication.class, args);
}
}
The @SpringBootApplication annotation combines three annotations: @Configuration, @EnableAutoConfiguration, and @ComponentScan. It tells Spring Boot: "Hey, this is the app — do all the magic for me!"
2. Writing a simple REST controller
Now let's add some functionality. We'll create a REST controller that responds with a greeting message. In the src/main/java/com/example/helloworldapp directory create a new class HelloController.
It looks like this:
package com.example.helloworldapp;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello")
public String sayHello() {
return "Hello, Spring Boot!";
}
}
Here's where the magic starts. The @RestController annotation tells Spring that this class will handle HTTP requests. And the @GetMapping("/hello") annotation indicates that the sayHello() method should be called when someone sends a GET request to /hello.
How does this work?
Spring Boot auto-configures all the basic components. You just mark the class with @RestController, and Spring Boot does the heavy lifting: it starts a web server, sets up HTTP request handling, configures JSON conversion. When a request hits /hello, all that pre-wired infrastructure just does its job — you don't have to think about starting Tomcat or setting up serialization.
3. Running the app
Now that we have a controller, it's time to run the app. Open the HelloWorldAppApplication class and click "Run" in your IDE. You should see something like this in the console:
Tomcat started on port(s): 8080 (http)
Started HelloWorldAppApplication in 2.345 seconds (JVM running for 2.678)
That means the embedded Tomcat server is up and our app is ready to accept requests.
How to check?
Open your browser or a tool like Postman and go to: http://localhost:8080/hello. You should see:
Hello, Spring Boot!
Congrats! That's your first working REST API.
4. Configuring the port and other settings
If you want to use a different port (for example, 9090), it's easy. Open the application.properties file in src/main/resources and add this line:
server.port=9090
Now restart the app and open http://localhost:9090/hello. The port changed and the app still works. Handy, right?
5. Practical task: add CRUD
To solidify the material, add a new controller TaskController to your app. This controller will manage a simple list of tasks. To keep things simple we won't use a database yet — we'll handle everything in memory.
Step 1. Helper class Task
Add the following class to the project:
package com.example.helloworldapp;
public class Task {
private Long id;
private String name;
public Task(Long id, String name) {
this.id = id;
this.name = name;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Step 2. Controller implementation
Add a new class TaskController:
package com.example.helloworldapp;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
@RestController
@RequestMapping("/tasks")
public class TaskController {
private final List<Task> tasks = new ArrayList<>();
@GetMapping
public List<Task> getTasks() {
return tasks;
}
@PostMapping
public Task addTask(@RequestBody Task task) {
tasks.add(task);
return task;
}
@DeleteMapping("/{id}")
public void deleteTask(@PathVariable Long id) {
tasks.removeIf(task -> task.getId().equals(id));
}
}
Now you can add, retrieve, and delete tasks via the /tasks URL.
Common mistakes and how to avoid them
- If the app doesn't start, check that the dependencies are present in
pom.xml. You might have forgotten to addspring-boot-starter-web. - If you don't see anything in the browser, make sure your controller method is annotated with
@GetMappingor a similar annotation. - When changing code, make sure you restarted the app. Or use
Spring Boot DevToolsfor automatic reloads. - A common newbie mistake is using the wrong path. For example, a request to
/api/taskswon't work if your controller listens on/tasks.
At this point your Spring Boot application is ready! Now you can build web apps, REST APIs, and much more without drowning in configuration. But this is only the beginning of your adventure. Get ready for lots more possibilities ahead!
GO TO FULL VERSION