Controllers & Routing
2 min read · Updated November 7, 2026
Table of Contents
1. What a Controller Does
A controller receives an HTTP request, hands it to something that does the real work, and returns the HTTP response. It should stay thin — no business rules, no database code. Right now we have nothing to hand off to, so this part returns hard-coded data; Part 4 moves the logic out into a service.
2. Mapping Routes
@RestController marks a class as a web controller whose return values are written
straight to the response as JSON. @RequestMapping sets a path prefix for the whole
class, and one annotation per HTTP method maps a route:
| Annotation | HTTP method | Route in this project |
|---|---|---|
@GetMapping | GET | /tasks, /tasks/{id} |
@PostMapping | POST | /tasks |
@PatchMapping | PATCH | /tasks/{id} |
@DeleteMapping | DELETE | /tasks/{id} |
// src/main/java/com/example/taskapi/tasks/TaskController.java
package com.example.taskapi.tasks;
import java.util.List;
import java.util.Map;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/tasks")
public class TaskController {
@GetMapping
public List<Map<String, Object>> findAll() {
return List.of(
Map.of("id", 1, "title", "Learn Spring Boot", "done", false),
Map.of("id", 2, "title", "Build my first API", "done", false));
}
}
Spring converts the returned List to JSON using Jackson — you never write
serialization code.
3. Path Variables and Request Bodies
The other routes need input from the request. Two annotations cover almost everything:
@PathVariable— a value from the URL:/tasks/{id}with@PathVariable Long id. Spring converts the text to aLongfor you, and answers400itself if it isn't a number.@RequestBody— the JSON body, converted into a Java object.
Add the single-task route and a create route that just echoes what it receives:
@GetMapping("/{id}")
public Map<String, Object> findOne(@PathVariable Long id) {
return Map.of("id", id, "title", "Learn Spring Boot", "done", false);
}
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public Map<String, Object> create(@RequestBody Map<String, Object> body) {
return body;
}
@ResponseStatus(HttpStatus.CREATED) makes a successful POST answer 201 instead
of the default 200. (Add the imports your editor suggests.) PATCH and DELETE
follow the same shape and arrive for real in Part 4.
4. Trying It
./gradlew bootRun
curl http://localhost:8080/tasks
curl http://localhost:8080/tasks/7
curl -X POST http://localhost:8080/tasks -H "Content-Type: application/json" \
-d '{"title":"Try POST"}'
curl http://localhost:8080/tasks/abc
The first two return JSON, the third echoes your body with status 201, and the last
returns 400 — Spring refused to turn abc into a number before your code ran.
5. Beginner Pitfalls
@Controllerinstead of@RestController. Plain@Controllertreats a returned string as a view name and gives you a 404 or a template error. For a JSON API, always@RestController.- Forgetting
Content-Type: application/jsononcurl -X POST. Without it Spring answers415 Unsupported Media Type, because it doesn't know how to read the body. - Putting logic in the controller. Anything beyond "take the request, call one method, return the result" belongs in a service (Part 4).
Official docs:
Was this chapter helpful?
Get new tutorials
Join the Telegram channel to hear when a new chapter or series is published, or follow the feeds.
Buy Me a Coffee
Found this tutorial helpful? Consider buying me a coffee to support more free content!