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
@Component @Converter public class EncryptedStringConverter implements AttributeConverter<String, String> {
Transparent column encryption in Spring & JPA
encryption
aes-gcm
jpa-converter
Advanced
10 steps
python
import time import uuid from django.utils.deprecation import MiddlewareMixin
Attaching per-request context in Django
middleware
request lifecycle
multi-tenancy
Intermediate
7 steps
java
package com.acme.billing.config; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.ConfigurationProperties;
Feature-flagged beans with Spring @ConditionalOnProperty
feature-flags
conditional-beans
strategy-pattern
Intermediate
5 steps
java
public static Map<String, String> parseCookieHeader(String header) { Map<String, String> cookies = new LinkedHashMap<>(); if (header == null || header.isBlank()) { return cookies;
Parsing an HTTP Cookie header in Java
string-parsing
http
url-decoding
Intermediate
6 steps
php
<?php final class RotatingFileLogger {
How a rotating file logger works in PHP
logging
file-rotation
io
Intermediate
9 steps
java
public class TimedSocketReader { private static final int READ_TIMEOUT_MS = 5_000; private static final int CONNECT_TIMEOUT_MS = 3_000;
Reading a socket with connect and read timeouts
sockets
timeouts
io
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.