Spring REST APIs
Modern applications do not work in isolation. Web apps, mobile apps, and other services all communicate using APIs. In this lesson, you will learn how to build REST APIs using Spring Boot.
This lesson is extremely important because most Java backend jobs today require strong REST API knowledge.
What Is a REST API?
REST stands for Representational State Transfer. A REST API allows different systems to communicate using HTTP requests.
Instead of returning HTML pages, REST APIs usually return data in JSON format.
For example:
- A frontend sends a request
- The backend processes it
- The backend returns data as JSON
Real-World Example
Consider a food delivery application:
- Mobile app requests restaurant data
- Backend API fetches data from database
- API returns restaurant list as JSON
Spring REST APIs make this communication simple and reliable.
Spring REST Architecture
A typical Spring REST API contains:
- Controller – handles HTTP requests
- Service – contains business logic
- Repository – interacts with database
In this lesson, we focus mainly on the Controller layer.
@RestController Annotation
In Spring Boot, REST APIs are created using the
@RestController annotation.
This annotation tells Spring that the class will return data, not views.
@RestController
public class HelloController {
@GetMapping("/hello")
public String sayHello() {
return "Hello from Spring REST API";
}
}
When you open /hello in the browser,
you will see plain text instead of an HTML page.
Understanding HTTP Methods
REST APIs use standard HTTP methods:
- GET – Fetch data
- POST – Create new data
- PUT – Update existing data
- DELETE – Remove data
Spring provides annotations for each method.
GET Request Example
Fetching user data using GET:
@GetMapping("/users")
public List getUsers() {
return List.of("Alice", "Bob", "Charlie");
}
This API returns data in JSON format automatically.
POST Request Example
Creating new data using POST:
@PostMapping("/users")
public String addUser(@RequestBody String user) {
return "User added: " + user;
}
Spring automatically converts JSON request bodies into Java objects.
@RequestBody and @PathVariable
Spring REST APIs use annotations to extract data from requests:
@RequestBody– reads JSON body@PathVariable– reads URL values
Example:
@GetMapping("/users/{id}")
public String getUser(@PathVariable int id) {
return "User ID: " + id;
}
JSON Response Example
Spring automatically converts Java objects to JSON.
@GetMapping("/profile")
public Map getProfile() {
Map profile = new HashMap<>();
profile.put("name", "John");
profile.put("role", "Developer");
return profile;
}
No manual JSON conversion is required.
Why Spring REST APIs Are Popular
- Easy to build and maintain
- Fast development with annotations
- Automatic JSON handling
- Industry-standard approach
This is why Spring Boot + REST is used in most enterprise systems.
Industry Usage
Spring REST APIs power:
- E-commerce platforms
- Banking systems
- Mobile app backends
- Microservices architecture
Mastering REST APIs significantly improves your backend career opportunities.
What Comes Next?
In the next lesson, you will learn how Spring connects APIs to databases using Hibernate.
This will allow your APIs to store and retrieve real data.