Spring Boot services · Chapter 8 of 41

How Spring handles requests

By the end of this chapter you can map requests to controller methods by path, HTTP method, path variables, query parameters and headers, and tell which 404, 405 and 400 answers Spring sends before your code runs. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: REST APIs
  • 5 tasks
  • Spring Boot
  • In the all labs plan

The first 2 chapters of Spring Boot services are free. This one comes with the all labs plan.

Why you need this

The first station managers are calling Tapri Cloud, and their requests are messy: a word where a number belongs, a DELETE just to see what happens, a header forgotten. Ravi forwards the log: "Some get 404, some 405, some

  1. Which ones are our fault?"

Without knowing how Spring picks a method, you cannot tell a typo from a bug. And without typed parameters you would cut the URL text apart yourself, and turn "chai" into a number by hand.

What you build: 5 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    StationController.java: put every station endpoint under /stations

    Map the whole StationController to /stations with one annotation on the class, so its two methods answer GET /stations and GET /stations/{code}. Leave the methods as they are.

    GET /stations answers 200 with all five stations in their order (SBC first), and GET /stations/NDLS answers 200 with {"code":"NDLS","city":"New Delhi"}.

  2. Task 2

    MenuController.java: look up a menu item by its number

    Make GET /menu/{id} answer 200 with the item that has this id, as JSON with id, name, category and price: GET /menu/4 is Bun maska at 50, GET /menu/1 is Masala chai. An id that is not on the menu (GET /menu/99) answers 404 Not Found.

    Keep the id an int, so a non-number such as GET /menu/chai is refused by Spring before your method runs.

  3. Task 3

    MenuController.java: search the menu with an optional category and a default maximum price

    Make GET /menu/search answer 200 with the items of a category that cost at most maxPrice (a price equal to it counts), in menu order. Both query parameters are optional: without category every category matches, and without maxPrice the limit is 100.

    No parameters gives all 5 items; category=chai gives Masala, Cutting and Elaichi chai; maxPrice=30 gives Cutting chai and Parle-G; category=snack with maxPrice=50 gives Bun maska and Parle-G.

  4. Task 4

    OrderController.java: take an order with POST /orders, reading the station from the X-Station header

    Map OrderController.order to POST /orders (no extra path). It reads the station from the X-Station header and item and qty from the query, looks the item up in MenuController.MENU, and answers 200 with plain text: POST /orders?item=1&qty=2 with X-Station: SBC answers "SBC: 2 x Masala chai = 80".

    qty is optional and 1 by default: POST /orders?item=4 with X-Station: NDLS answers "NDLS: 1 x Bun maska = 50". Without the X-Station header the answer is 400 Bad Request.

  5. Task 5

    StatusQuiz.java: predict the status Spring answers for five requests

    In StatusQuiz.ANSWERS, replace every 0 with the status code your app answers for that request: 200, 400, 404 or 405. Keep the five requests exactly as they are.

    The Check sends every request to your app and compares each answer with your prediction.

What you will learn

  • The DispatcherServlet: the front desk that finds the method for every request.
  • Mappings: a prefix on the class, plus @GetMapping, @PostMapping and friends on the methods.
  • Path variables, query parameters and headers: typed, and required unless you say otherwise.
  • 404, 405 and 400: what Spring answers when a request does not fit.

More in REST APIs

Build "How Spring handles requests" yourself

Tapri Cloud: the counters’ Java grows into a Spring Boot service with a database. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Spring Boot services are free. This one comes with the all labs plan.