Implementing REST API in Spring Boot – Java Fresher Guide
Fast recall
Quick revision
Core answer
spring-boot-starter-web provides Spring MVC and an embedded web server. Spring MVC maps each HTTP request to a controller method, message converters read and write the body, application services perform the business work, and the controller returns the correct HTTP status, headers, and response data.Interview-ready guide
Interview answer
In the common Spring MVC setup, adding spring-boot-starter-web gives the application an embedded server, Spring MVC, and HTTP message conversion. The DispatcherServlet receives a request, Spring finds the matching controller method, and a message converter such as Jackson can turn JSON into a Java value or a Java value back into JSON. Spring Boot configures this foundation; the developer still designs the endpoints and application behavior.
A controller defines the HTTP boundary. @RestController combines controller discovery with response-body handling. @RequestMapping can set a base path, while @GetMapping and @PostMapping select an HTTP method and path. @PathVariable reads a value from the path, and @RequestBody asks Spring to read the request body into a Java object.
For POST /products, Spring converts the JSON body into CreateProductRequest and calls the mapped method. The controller passes the request to ProductService, which performs the create-product use case. When the service returns ProductResponse, the controller sends 201 Created, a Location header for the new resource, and the response object. Jackson then writes that object as JSON.
This separation keeps protocol work in the controller and business rules in the service. A repository is needed only when the service must use persistent storage. Real APIs should also validate the request and translate known failures into useful HTTP responses—for example, 400 Bad Request for invalid input and 404 Not Found for a missing product. Spring supplies the mechanism, but the application must define those rules and mappings.
Implementing REST API in Spring Boot – Java Fresher Guide
Direct Answer
In a typical Spring Boot servlet application, spring-boot-starter-web provides Spring MVC and an embedded web server. Spring MVC maps each HTTP request to a controller method, message converters read and write the body, application services perform the business work, and the controller returns the correct HTTP status, headers, and response data.
Deep Dive & Architecture
A Spring Boot REST API connects an HTTP request to application code and then turns the result back into an HTTP response. This lesson uses the servlet-based Spring MVC stack provided by spring-boot-starter-web. Spring WebFlux is a different stack and is outside this example.
A request does not jump directly into a service. The embedded server accepts it, the DispatcherServlet coordinates Spring MVC, a handler mapping finds the controller method, and message converters read or write the body. Validation can run before the controller method when the application has validation support, constraints, and @Valid.
This flow separates responsibilities: Spring MVC handles the web mechanics, the controller translates HTTP to an application call, and the service performs the use case.
Code Example
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;
import org.springframework.stereotype.Service;
@Service
class ProductService {
private final Map<Long, ProductResponse> products =
new ConcurrentHashMap<>();
private final AtomicLong nextId = new AtomicLong();
ProductResponse create(CreateProductRequest request) {
long id = nextId.incrementAndGet();
ProductResponse product = new ProductResponse(
id, request.name(), request.price());
products.put(id, product);
return product;
}
ProductResponse findById(long id) {
ProductResponse product = products.get(id);
if (product == null) {
throw new ProductNotFoundException(id);
}
return product;
}
}
This in-memory map is only a teaching substitute for storage; it is not a repository implementation. A real service can call ProductRepository without changing the controller's HTTP contract.
Check your understanding
Answer these without looking back at the article.
- What does `@RestController` add to a controller?
- Why should an API accept DTOs instead of JPA entities?
- What belongs in a controller integration test?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.