Spring Boot 3.3.1 Migration: HandlerInterceptor and WebMvcConfigurer Replacements
Interceptor Changes in Spring MVC
The abstract class HandlerInterceptorAdapter was removed in Spring Boot 3.3.1. Earlier, you could extend this class; now you must implement the HandlerInterceptor interface directly.
Before (Spring Boot 2.x):
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import example.core.RequestContext;
import example.core.ContextHelper;
import example.core.ServiceValidator;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class RequestTokenInterceptor extends HandlerInterceptorAdapter {
private final ServiceValidator validator = new ServiceValidator();
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
RequestContext ctx = ContextHelper.extractContext(request);
String token = request.getHeader("X-Auth-Token");
try {
if (!validator.isValid(ctx, token)) {
log.warn("Token validation failed for [{}]", ctx.getPath());
response.sendError(HttpServletResponse.SC_FORBIDDEN);
return false;
}
return true;
} catch (Exception ex) {
log.error("Interceptor error", ex);
return false;
}
}
}
After (Spring Boot 3.x):
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
import example.core.RequestContext;
import example.core.ContextHelper;
import example.core.ServiceValidator;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class RequestTokenInterceptor implements HandlerInterceptor {
private final ServiceValidator validator = new ServiceValidator();
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
RequestContext ctx = ContextHelper.extractContext(request);
String token = request.getHeader("X-Auth-Token");
try {
if (!validator.isValid(ctx, token)) {
log.warn("Token validation failed for [{}]", ctx.getPath());
response.sendError(HttpServletResponse.SC_FORBIDDEN);
return false;
}
return true;
} catch (Exception ex) {
log.error("Interceptor error", ex);
return false;
}
}
}
Custom MVC Configuration
The WebMvcConfigurerAdapter class has also been removed. To customize Spring MVC configuration, implement the WebMvcConfigurer interface instead. Unlike the old adapter, this itnerface provides default methods, so you only need to oevrride what you need, and Spring Boot’s auto‑configuration remains active. Alternatively, you can extend WebMvcConfigurationSupport, but that disables auto‑configuration and requires manual setup of many defaults.
Before (Spring Boot 2.x):
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import example.formatting.TrimmingFormatter;
import example.formatting.DateFormatter;
import java.time.LocalDate;
@Configuration
public class FormatterConfig extends WebMvcConfigurerAdapter {
@Override
public void addFormatters(FormatterRegistry registry) {
registry.addFormatterForFieldType(String.class, new TrimmingFormatter());
registry.addFormatterForFieldType(LocalDate.class, new DateFormatter());
}
}
After (Spring Boot 3.x):
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import example.formatting.TrimmingFormatter;
import example.formatting.DateFormatter;
import java.time.LocalDate;
@Configuration
public class FormatterConfig implements WebMvcConfigurer {
@Override
public void addFormatters(FormatterRegistry registry) {
registry.addFormatterForFieldType(String.class, new TrimmingFormatter());
registry.addFormatterForFieldType(LocalDate.class, new DateFormatter());
}
}