Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
The Spring Core  ›  Lesson

Spring MVC — From HTTP Request to Method Call

Spring MVC 14 minThe DispatcherServlet receives every request, maps it to a controller method, fills its arguments, and writes the result back as the response.

Spring MVC — From HTTP Request to Method Call

A member's phone app sends GET /books/42. Somewhere in library-service, a Java method must run and its result must come back as JSON. Spring MVC is the machinery in between, and knowing its path lets you debug a 404 or a 415 in seconds instead of hours.

The rule

One front door, the DispatcherServlet, routes every request to a handler method.

Spring MVC is built on the Java Servlet API, the standard way Java servers handle HTTP. Instead of one servlet per URL, Spring registers a single servlet, the DispatcherServlet, as a front controller. Every request enters through it, and it follows the same path each time:

  1. Handler mapping — find the controller method whose annotations match the path and HTTP method.
  2. Argument resolution — fill the method's parameters from the path, query string, headers or body.
  3. Invoke — call your method.
  4. Write the response — for a @RestController, a message converter (Jackson, for JSON) turns the return value into the response body. A classic @Controller instead returns a view name, and a view resolver turns it into an HTML page.
@RestController
@RequestMapping("/books")
public class BookController {
    private final BookRepository books;
    public BookController(BookRepository books) { this.books = books; }

    @GetMapping("/{id}")
    public ResponseEntity<Book> find(@PathVariable long id) {
        return books.findById(id)
                .map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }

    @PostMapping
    public ResponseEntity<Book> add(@RequestBody Book book) {
        Book saved = books.save(book);
        return ResponseEntity.status(HttpStatus.CREATED).body(saved);
    }
}

(For brevity this controller reads the repository directly; anything with real rules would go through a service.) @RestController is @Controller plus @ResponseBody: every method's return value becomes the body rather than a view name.

The shortcut

Read each request like an address, method + path + parts, and match it against the annotations:

  • @GetMapping, @PostMapping, @PutMapping, @DeleteMapping match the HTTP method.
  • @PathVariable takes a piece of the path, like 42 in /books/42.
  • @RequestParam takes a query parameter, like ?author=Le+Guin.
  • @RequestBody takes the JSON body and converts it to an object.
Tracing POST /books with a JSON bodyStep 1 of 4
  1. Front door

    The DispatcherServlet receives POST /books.

When no method matches the path, you get 404. When the path matches but not the HTTP method, 405. When the body's content type cannot be read, 415. Each status tells you which step failed.

Drills

Answer fast — trust the rule.

quick drill

What is the job of the DispatcherServlet?

quick drill

For GET /books/42?format=short, which annotation reads the 42?

quick drill

What does @RestController add compared with @Controller?

quick drill

A client sends DELETE /books/42, but BookController has only @GetMapping("/{id}") for that path. Which status comes back?

Remember

  • One front door, the DispatcherServlet, routes every request to a handler method.
  • Read the request as method + path + parts and match the annotations.
  • Trap: 404, 405 and 415 fail at different steps — read the status before the code.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainTrace a request

Trace GET /books/42 through library-service's BookController from the moment it reaches the server until JSON goes back, naming each step. Then say what status the client gets if book 42 does not exist.