Java Spring Boot Backend

Getting Started with Spring Boot: Build Your First REST API

Spring Boot is the fastest way to build production-ready Java applications. It removes the boilerplate configuration that made classic Spring so intimidating, letting you focus entirely on writing business logic. In this guide we will go from zero to a working REST API in under 30 minutes — no prior Spring experience needed.

What is Spring Boot?

Spring Boot is an opinionated extension of the Spring Framework that provides auto-configuration, an embedded web server (Tomcat by default), and a curated set of "starter" dependencies. Instead of wiring hundreds of XML beans by hand, you declare what you need and Spring Boot wires everything automatically.

"Spring Boot makes it easy to create stand-alone, production-grade Spring-based applications that you can just run." — Spring.io

Step 1 — Create a New Project

The easiest way is Spring Initializr. Select Maven, Java 17, and add the Spring Web dependency. Download and unzip the project, then open it in IntelliJ IDEA or VS Code.

Your generated pom.xml will include this starter automatically:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
</dependency>

Step 2 — Understand Key Annotations

Spring Boot uses annotations to express intent with almost zero configuration:

  • @SpringBootApplication — marks the entry point; combines @Configuration, @EnableAutoConfiguration, and @ComponentScan
  • @RestController — declares a class as a REST controller; combines @Controller and @ResponseBody
  • @GetMapping / @PostMapping — maps HTTP verbs to handler methods
  • @RequestParam — binds a query parameter from the URL
  • @PathVariable — extracts a segment from the URL path
  • @RequestBody — deserialises the request JSON into a Java object

Step 3 — Write Your First Controller

Create a file HelloController.java inside your main package and paste the following:

package com.example.demo;

import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api")
public class HelloController {

    // GET /api/hello  →  "Hello, World!"
    @GetMapping("/hello")
    public String hello() {
        return "Hello, World!";
    }

    // GET /api/greet?name=Dhiraj  →  "Hello, Dhiraj!"
    @GetMapping("/greet")
    public String greet(@RequestParam(defaultValue = "Guest") String name) {
        return "Hello, " + name + "!";
    }

    // GET /api/user/42  →  "User ID: 42"
    @GetMapping("/user/{id}")
    public String getUser(@PathVariable Long id) {
        return "User ID: " + id;
    }
}

Returning JSON Instead of Plain Text

Replace the String return type with a record (Java 16+) or a POJO. Spring Boot will serialise it to JSON automatically via Jackson:

// Define the model
public record UserResponse(Long id, String name, String email) {}

// Use it in the controller
@GetMapping("/user/{id}")
public UserResponse getUser(@PathVariable Long id) {
    return new UserResponse(id, "Dhiraj Roy", "dhiraj@example.com");
}

Hitting GET /api/user/1 now returns:

{
  "id": 1,
  "name": "Dhiraj Roy",
  "email": "dhiraj@example.com"
}

Step 4 — Run the Application

Spring Boot's embedded Tomcat means no external server to install or configure:

  1. Open a terminal in the project root.
  2. Run ./mvnw spring-boot:run (Linux / macOS) or mvnw.cmd spring-boot:run (Windows).
  3. Wait for the log line: Tomcat started on port 8080.
  4. Open your browser and visit http://localhost:8080/api/hello.
  5. You should see Hello, World! — your first Spring Boot API is live.

Project Structure at a Glance

Understanding where files live will save you hours of confusion:

src/
├── main/
│   ├── java/com/example/demo/
│   │   ├── DemoApplication.java      ← entry point (@SpringBootApplication)
│   │   ├── HelloController.java      ← your REST controller
│   │   └── UserResponse.java         ← your response model
│   └── resources/
│       └── application.properties    ← config (port, DB, etc.)
└── test/
    └── java/com/example/demo/
        └── HelloControllerTest.java  ← unit / integration tests

To change the default port from 8080, add one line to application.properties:

server.port=9090
Pro tip: Enable hot reload during development by adding the spring-boot-devtools dependency. The server restarts automatically whenever you save a file — no manual restart needed.

Key Takeaways

  • Spring Boot eliminates XML config with auto-configuration
  • @RestController + @GetMapping is all you need for a basic endpoint
  • Return a Java record or POJO and Spring Boot serialises it to JSON automatically
  • The embedded Tomcat server means a single mvnw spring-boot:run command gets you running
  • Use application.properties for all environment-specific configuration

What's Next?

You now have a working REST API. The logical next steps are connecting to a database with Spring Data JPA, adding request validation with spring-boot-starter-validation, and securing your endpoints with Spring Security. Each of these follows the same pattern: add a starter → Spring Boot auto-configures it → you focus on the logic.

Check out the archive for more backend guides, or drop a question via the contact page — happy to help you level up.

Dhiraj Roy
Dhiraj Roy

Backend developer & tech writer. Writing about Java, Spring Boot, Python, and AI at Digital Drift.