You define REST endpoints in Spring Boot by creating a class annotated with @RestController and mapping methods to HTTP verbs with @GetMapping, @PostMapping, @PutMapping, @PatchMapping and @DeleteMapping. Spring Boot auto-configures Spring MVC and Jackson, so returned objects are serialized to JSON without extra setup.
The example below builds a complete CRUD resource with validation, proper status codes and a consistent error format. It targets Spring Boot 3 and later on Java 17 or newer.
Add the web and validation starters. In Maven:
The web starter brings Spring MVC, an embedded Tomcat server and Jackson. The validation starter brings Jakarta Bean Validation with Hibernate Validator. Nothing else is needed to start serving endpoints.
Java records make compact, immutable request and response types, and Jackson supports them.
Using separate request and response types keeps clients from setting fields such as id and keeps your persistence entity out of the public API contract.
Points worth noting:
@Autowired when there is a single constructor.@Valid triggers Bean Validation on the body. A failure raises MethodArgumentNotValidException, which Spring turns into a 400 response.POST returns 201 Created with a Location header, and DELETE returns 204 No Content.| Operation | Method | Path | Success status |
|---|---|---|---|
| List | GET | /api/products | 200 |
| Read one | GET | /api/products/{id} | 200 |
| Create | POST | /api/products | 201 |
| Replace | PUT | /api/products/{id} | 200 |
| Partial update | PATCH | /api/products/{id} | 200 |
| Delete | DELETE | /api/products/{id} | 204 |
Use plural nouns for collections, keep verbs out of the path, and let the HTTP method express the action.
Spring Framework 6 supports RFC 9457 (previously RFC 7807) problem details through the ProblemDetail class. Enable it for built-in Spring MVC exceptions with a property and add a handler for your own.
A missing product now returns 404 with an application/problem+json body containing status, title and detail, instead of an HTML error page or a stack trace.
Run the app and call it with curl.
For automated tests, use @WebMvcTest(ProductController.class) with MockMvc (or MockMvcTester in newer versions) and a mocked ProductService, which starts only the web layer and runs quickly.
@RestController combines @Controller and @ResponseBody, so every method writes its return value to the response body. A plain @Controller resolves return values as view names.
Return a ResponseEntity with the status you want, or annotate the method with @ResponseStatus. Throw an exception handled by @RestControllerAdvice for error statuses.
Configure Jackson through spring.jackson.* properties or annotate fields with @JsonProperty and @JsonFormat. Spring Boot writes java.time types as ISO-8601 strings by default.
No. Return DTOs such as records. Entities expose internal fields, can trigger lazy-loading errors during serialization and tie your API to your database schema.
Annotate a class with @RestController, map methods to HTTP verbs, accept validated records with @Valid @RequestBody, and return meaningful status codes. Centralise errors in a @RestControllerAdvice that produces ProblemDetail responses, and test the web layer in isolation with @WebMvcTest.