java
30 lines · 6 steps
How a Spring HandlerInterceptor times requests
A Spring interceptor stamps each request with a start time and reports how long it took once the response completes.
Explained by
highlit
1@Component
2public class RequestTimingInterceptor implements HandlerInterceptor {
3
4 private static final Logger log = LoggerFactory.getLogger(RequestTimingInterceptor.class);
5 private static final String START_TIME = "requestStartTime";
6 private static final long SLOW_THRESHOLD_MS = 500;
7
8 @Override
9 public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
10 request.setAttribute(START_TIME, System.nanoTime());
11 return true;
12 }
13
14 @Override
15 public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
16 Long start = (Long) request.getAttribute(START_TIME);
17 if (start == null) {
18 return;
19 }
20 long elapsedMs = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start);
21 response.setHeader("X-Response-Time-Ms", Long.toString(elapsedMs));
22
23 String endpoint = request.getMethod() + " " + request.getRequestURI();
24 if (elapsedMs >= SLOW_THRESHOLD_MS) {
25 log.warn("Slow request {} -> {} ({} ms)", endpoint, response.getStatus(), elapsedMs);
26 } else {
27 log.info("{} -> {} ({} ms)", endpoint, response.getStatus(), elapsedMs);
28 }
29 }
30}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Spring's HandlerInterceptor exposes preHandle and afterCompletion hooks that bracket the entire request lifecycle.
- 2Stashing state on the request attribute lets you carry data between the two hooks without shared mutable fields.
- 3afterCompletion always runs even on errors, making it the right place for cleanup and final measurements.
Related explainers
java
public class ThumbnailProcessor { private static final int MAX_CONCURRENCY = 4;
Bounded parallel thumbnail rendering in Java
concurrency
thread-pool
futures
Intermediate
7 steps
java
public class SortedListMerger { public static int[] merge(int[] a, int[] b) { int[] result = new int[a.length + b.length];
Merging two sorted arrays in Java
two-pointers
merging
arrays
Beginner
6 steps
rust
use std::time::Instant; use tracing::info; pub struct Timer {
A scope-guard timer with Drop in Rust
raii
drop-guard
timing
Intermediate
7 steps
java
import java.util.ArrayDeque; import java.util.Deque; public final class RollingAverage {
A rolling average over a sliding window
sliding-window
running-sum
deque
Intermediate
7 steps
java
@Target({ElementType.FIELD, ElementType.PARAMETER}) @Retention(RetentionPolicy.RUNTIME) @Constraint(validatedBy = StrongPasswordValidator.class) @Documented
Building a custom @StrongPassword validator in Spring
bean-validation
annotations
regex
Intermediate
7 steps
java
@Component public class JwtAuthenticationFilter extends OncePerRequestFilter { private final JwtTokenProvider tokenProvider;
How a JWT auth filter works in Spring
authentication
jwt
servlet-filter
Intermediate
8 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/how-a-spring-handlerinterceptor-times-requests-explained-java-c3a8/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.