Spring Boot services · Chapter 14 of 41

Filters and interceptors

By the end of this chapter you can run code around every request, or in front of a group of endpoints, without touching a single controller. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: REST APIs
  • 4 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 Tapri app reports a failed order, and Kabir cannot find it in the logs: "Which of ten thousand requests was it, and how long did it take?" He needs an id that the app and Tapri Cloud both log. And the Howrah counter is closed for renovation, but Tapri Cloud still takes its orders:

$ curl -i -X POST localhost:8081/orders -H "X-Station: HWH"
HTTP/1.1 201 
Content-Type: text/plain;charset=UTF-8
Content-Length: 18
Date: Wed, 07 Oct 2026 10:19:47 GMT

Order taken at HWH

Neither belongs in a controller. You would copy the same lines into every method, and the next endpoint would forget them.

What you build: 4 tasks

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

  1. Task 1

    TracingFilter.java: give every response an X-Request-Id

    In TracingFilter, give every response (GET /menu, POST /orders and the rest) an X-Request-Id header. When the request has its own X-Request-Id that is not blank, send that same value back (counter-7-0042 answers counter-7-0042). Otherwise make a new random UUID, such as 3ce82773-b27a-4339-aa16-ca2708dd354a, so two requests never share one.

    Also store the id as the request attribute "requestId" for the code after the filter.

  2. Task 2

    CounterHoursInterceptor.java: refuse orders at a closed counter

    In CounterHoursInterceptor.preHandle, refuse a request whose X-Station header names a closed counter (closedStations, from the property tapri.closed-stations: HWH unless set; the Check sets HWH and MAS). Answer 503 Service Unavailable with the plain text body "Counter HWH is closed" (with that station's code), and stop it before the controller, so no order is taken.

    Any other station goes on to the controller: POST /orders with X-Station SBC answers 201 "Order taken at SBC". Register the interceptor in WebConfig.addInterceptors for /orders and everything under it, and nothing else: GET /menu with X-Station HWH still answers 200.

  3. Task 3

    TracingFilter.java: time every request in X-Response-Time

    In TracingFilter, measure how long the rest of the chain takes and send it on every response as an X-Response-Time header: whole milliseconds followed by ms, such as "4ms" or "0ms". The body must arrive unchanged (GET /menu still starts with "Masala chai"; POST /orders still says "Order taken at CSMT").

    A header set after the chain only arrives while the body is still in the buffer, so hold the body back in a ContentCachingResponseWrapper and copy it to the response after setting the header.

  4. Task 4 · interview question

    Interview: put filters, interceptors and the controller in the order a request meets them

    Fill WebConfig.JOURNEY with the six steps one GET request meets, first to last, using exactly these names: "filter in", "filter out", "preHandle", "controller", "postHandle" and "afterCompletion". The Check records the real journey of a request and compares the two lists.

    Be ready to explain where filters and interceptors sit, and when to use each.

What you will learn

  • Filters (OncePerRequestFilter): code from the servlet world that runs around every request.
  • Interceptors (HandlerInterceptor): Spring MVC's hooks for chosen paths, which can stop a request before the controller runs.
  • WebMvcConfigurer.addInterceptors: switching an interceptor on, and choosing its paths.
  • Committed responses: when it is too late to add a header.

More in REST APIs

Build "Filters and interceptors" 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.