Spring Boot

Article 4: Building Your First REST API with Spring Boot

By Utility Zone · 2026-01-27T18:30:19.081028

1. Introduction

This is the moment where Spring Boot starts to feel real.

In this article, you will:

  • Create your first REST API
  • Understand how HTTP requests are handled
  • Return text and JSON responses
  • Test APIs like a backend developer

By the end, you’ll have a working API running locally.


2. What Is a REST API? (Very Brief)

A REST API:

  • Exposes data or functionality over HTTP
  • Uses standard HTTP methods:
    • GET (read)
    • POST (create)
    • PUT (update)
    • DELETE (delete)

Spring Boot makes creating REST APIs extremely simple.


3. Creating Your First Controller

3.1 Create Controller Package

Inside src/main/java, create:

com.example.demo.controller

3.2 Create a Controller Class

package com.example.demo.controller;

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

@RestController
public class HelloController {

    @GetMapping("/hello")
    public String hello() {
        return "Hello from Spring Boot!";
    }
}

What Just Happened?

  • @RestController → Marks class as REST controller
  • @GetMapping → Maps HTTP GET requests
  • Spring automatically converts response to HTTP output

4. Run and Test the API

Start your application and open browser:

http://localhost:8080/hello

Expected output:

Hello from Spring Boot!

🎉 You’ve built your first REST API.


5. Returning JSON Instead of String

5.1 Create a Simple POJO

package com.example.demo.model;

public class Message {

    private String text;

    public Message(String text) {
        this.text = text;
    }

    public String getText() {
        return text;
    }
}

5.2 Return JSON from Controller

@GetMapping("/hello-json")
public Message helloJson() {
    return new Message("Hello JSON response");
}

Spring Boot automatically:

  • Converts object → JSON
  • Sets correct HTTP headers

Output:

{
  "text": "Hello JSON response"
}

6. Using @RequestMapping at Class Level

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

    @GetMapping("/hello")
    public String hello() {
        return "Hello API";
    }
}

Endpoint becomes:

/api/hello

7. Using Path Variables

@GetMapping("/hello/{name}")
public String greet(@PathVariable String name) {
    return "Hello " + name;
}

Test:

/api/hello/Bharat

8. Using Request Parameters

@GetMapping("/greet")
public String greetUser(@RequestParam String name) {
    return "Hello " + name;
}

Test:

/api/greet?name=Bharat

9. HTTP Status Codes (Basic)

@GetMapping("/status")
public ResponseEntity<String> status() {
    return ResponseEntity.ok("Everything is OK");
}

Spring Boot encourages proper HTTP responses.


10. Testing APIs Properly

Recommended tools:

  • Browser (simple GETs)
  • Postman
  • curl

Example:

curl http://localhost:8080/api/hello

11. Common Beginner Mistakes

❌ Forgetting @RestController
❌ Wrong URL path
❌ Port mismatch
❌ Class outside base package

✔ Keep controllers under main package


12. What You Should Understand Before Moving On

You should now know:

  • What a controller is
  • How @GetMapping works
  • How to return JSON
  • How to test APIs locally

13. What’s Next?

➡ Article 5: Dependency Injection – The Heart of Spring

  • @Component
  • @Service
  • @Autowired
  • Constructor injection

Type Next when you’re ready.