How do you create a REST API in Spring Boot?

REST APIs in Spring Boot are typically created using the @RestController annotation, which exposes application functionality through HTTP endpoints. Spring Boot provides annotations for handling different HTTP methods, processing request data, and returning responses that are automatically converted into JSON or XML.

Key Points

Steps to Create a REST API

1. Create a Controller

A controller receives client requests and returns responses.

@RestController
@RequestMapping("/employees")
public class EmployeeController {

}

2. Define API Endpoints

Spring Boot provides dedicated annotations for HTTP methods.

GET

Retrieve data.

@PostMapping

Create data.

@PutMapping

Update data.

@DeleteMapping

Delete data.

Example

@RestController
@RequestMapping("/employees")
public class EmployeeController {

    @GetMapping
    public String getEmployees() {

        return "Employee List";
    }
}

3. Accept Request Data

Use @RequestBody to receive JSON input.

@PostMapping
public Employee createEmployee(
        @RequestBody Employee employee) {

    return employee;
}

Request

{

"id": 101,

name": "John

}

Spring automatically converts JSON into a Java object.

4. Handle Path Variables

Use @PathVariable to read values from the URL.

@GetMapping("/{id}")
public String getEmployee(
        @PathVariable Long id) {

    return "Employee Id: " + id;
}

Request

GET /employees/101

5. Handle Query Parameters

Use @RequestParam to read query parameters.

@GetMapping("/search")
public String searchEmployee(
        @RequestParam String name) {

    return name;
}

Request

GET /employees/search?name=John

6. Call Service Layer

Controllers should delegate business logic to service classes.

@RestController
@RequestMapping("/employees")
public class EmployeeController {

    private final EmployeeService service;

    public EmployeeController(
            EmployeeService service) {

        this.service = service;
    }

    @GetMapping("/{id}")
    public Employee getEmployee(
            @PathVariable Long id) {

        return service.getEmployee(id);
    }
}

This keeps the application clean and maintainable.

How a REST API Request Flows

Client Request

|

@RestController

|

Service Layer

|

Repository Layer

|

Database

|

JSON Response

Spring Boot manages most of this process automatically.

Complete CRUD Example

@RestController
@RequestMapping("/employees")
public class EmployeeController {

    @GetMapping
    public List<Employee> getAllEmployees() {

        return employeeService.getAllEmployees();
    }

    @PostMapping
    public Employee createEmployee(
            @RequestBody Employee employee) {

        return employeeService.save(employee);
    }

    @PutMapping("/{id}")
    public Employee updateEmployee(
            @PathVariable Long id,
            @RequestBody Employee employee) {

        return employeeService.update(id, employee);
    }

    @DeleteMapping("/{id}")
    public void deleteEmployee(
            @PathVariable Long id) {

        employeeService.delete(id);
    }
}

Spring Boot automatically converts returned Java objects into JSON responses.

Example

Suppose an Employee Management System exposes

GET /employees

Returns all employees.

GET /employees/101

Returns employee details.

POST /employees

Creates a new employee.

PUT /employees/101

Updates employee information.

DELETE /employees/101

Deletes an employee.

These endpoints together form a complete REST API.

Benefits of Spring Boot REST APIs

Real-World Example

In an e-commerce application, REST APIs can be used for

Clients such as web applications, mobile applications, and third-party systems interact with these APIs through HTTP requests.

Interview Tip

A concise interview answer is

To create REST APIs in Spring Boot, we use @RestController to define REST endpoints and mapping annotations such as @GetMapping, @PostMapping, @PutMapping, and @DeleteMapping to handle HTTP requests. Request data can be received using @RequestBody, @PathVariable, and @RequestParam. Business logic is delegated to service classes, and Spring Boot automatically converts Java objects into JSON responses.

Last updated: 11 November 2026

Previous question: How do starters simplify the Maven or Gradle configuration?

Next question: What is REST API versioning, and what are the ways to implement it?

More Spring Boot - Level I interview questions

All Spring Boot - Level I questions