๐Ÿ’ป Exercises โ€“ Week 5: Spring Boot

These exercises are designed to help you build confidence step-by-step in developing backend applications using Spring Boot.

๐Ÿ‘‰ Complete all exercises in order. Do not skip.


๐ŸŽฏ Goal

By completing these exercises, you will:

  • Understand Spring Boot project setup
  • Build REST APIs
  • Apply layered architecture
  • Use Dependency Injection properly
  • Think like a backend developer

๐Ÿงฉ Exercise 1: Create Your First Spring Boot Project

Task

  • Create a Spring Boot project using:

    • Spring Initializr
    • Maven or Gradle
  • Add dependency:

    • Spring Web

Expected Output

  • Project runs successfully
  • Application starts on port 8080
  • No errors

๐Ÿงฉ Exercise 2: Create a Simple REST Endpoint

Task

Create a controller:

@RestController
public class HelloController {

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

Test

Open browser or use curl:

http://localhost:8080/hello

Expected Output

Hello Backend!

๐Ÿงฉ Exercise 3: Build Expense Controller

Task

Create a controller:

  • Endpoint: /expenses
  • Method: GET
  • Return: List of expenses (hardcoded for now)

Example

@GetMapping("/expenses")
public List<String> getExpenses() {
    return List.of("Food", "Travel", "Shopping");
}

๐Ÿงฉ Exercise 4: Add Path Variable

Task

Create API:

GET /expenses/{id}

Example

@GetMapping("/expenses/{id}")
public String getExpense(@PathVariable int id) {
    return "Expense ID: " + id;
}

๐Ÿงฉ Exercise 5: Add POST API

Task

Create API:

POST /expenses

Example

@PostMapping("/expenses")
public String createExpense() {
    return "Expense Created";
}

๐Ÿงฉ Exercise 6: Introduce Service Layer

Task

  • Create ExpenseService class
  • Move logic from controller to service

Example

@Service
public class ExpenseService {

    public List<String> getExpenses() {
        return List.of("Food", "Travel");
    }
}

Update Controller

@RestController
public class ExpenseController {

    private final ExpenseService service;

    public ExpenseController(ExpenseService service) {
        this.service = service;
    }

    @GetMapping("/expenses")
    public List<String> getExpenses() {
        return service.getExpenses();
    }
}

๐Ÿงฉ Exercise 7: Add PUT and DELETE APIs

Task

Create:

  • PUT /expenses/{id}
  • DELETE /expenses/{id}

Expected Behavior

  • Update returns confirmation message
  • Delete returns confirmation message

๐Ÿงฉ Exercise 8: Use Request Body

Task

Accept input using request body.


Example

@PostMapping("/expenses")
public String createExpense(@RequestBody String expense) {
    return "Created: " + expense;
}

๐Ÿงฉ Exercise 9: Add Model Class

Task

Create a model:

public class Expense {
    private int id;
    private String name;
    private double amount;
}

Update APIs to use this object instead of String.


๐Ÿงฉ Exercise 10: Add Configuration

Task

Modify:

application.properties

Add:

server.port=8081
spring.application.name=expense-tracker

Verify

  • Application runs on new port
  • Name is visible in logs

๐Ÿงช Exercise 11: API Testing

Task

Test all APIs using:

  • Postman OR
  • curl

Validate

  • Correct responses
  • Proper status codes
  • Invalid input handling

๐Ÿง  Bonus Challenge (Optional)

๐Ÿ”ฅ Build Mini Expense API

Implement:

  • Add expense
  • Get all expenses
  • Get by ID
  • Update
  • Delete

Use:

  • Controller
  • Service
  • Model

โš ๏ธ Rules

  • Do NOT write all logic in controller
  • Follow clean code practices
  • Use meaningful names
  • Keep methods small

๐Ÿ Completion Checklist

โœ” Spring Boot app created โœ” REST APIs implemented โœ” Service layer added โœ” Dependency Injection used โœ” APIs tested successfully


๐Ÿš€ Next Step

After completing exercises:

โžก๏ธ Move to Assignment for real-world implementation



This site uses Just the Docs, a documentation theme for Jekyll.