In this tutorial, we will look at the difference between two ways of registering HandlerInterceptors.
To register a HandlerInterceptor with a HandlerMapping, we need to use Abstract Handler Mapping# set Interceptors(. . . ) as we saw in the last tutorial.
There's another way to register HandlerInterceptors: implement WebMvcConfigurer in our Java config class, override the addInterceptors method, and then use Interceptor Registry# add Interceptor(. . . ), as seen in this example.
The difference
AbstractHandlerMapping#setInterceptors(...) sets interceptors for only this HandlerMapping.
Registering interceptors via WebMvcConfigurer registers the interceptor globally for all registered HandlerMappings .
Example
In this example, we'll register a HandlerInterceptor with a SimpleUrlHandlerMapping, and also with the default HandlerMappings (via the InterceptorRegistry#addInterceptor method):
Creating an interceptor
package com.logicbig.example;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.HandlerInterceptor;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
public class MyInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle (HttpServletRequest request,
HttpServletResponse response,
Object handler) throws Exception {
System.out.println("preHandle, URI: " + request.getRequestURI());
return true;
}
@Override
public void postHandle (HttpServletRequest request, HttpServletResponse response,
Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("postHandle, URI: " + request.getRequestURI());
}
}
Registering interceptor
package com.logicbig.example;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.HttpRequestHandler;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
import java.util.HashMap;
import java.util.Map;
@EnableWebMvc
@Configuration
public class AppConfig implements WebMvcConfigurer {
@Bean
public SimpleUrlHandlerMapping simpleUrlHandlerMapping () {
SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
Map<String, Object> map = new HashMap<>();
map.put("/app", httpRequestHandler());
mapping.setUrlMap(map);
mapping.setInterceptors(new Object[]{new MyInterceptor()});
return mapping;
}
@Bean
HttpRequestHandler httpRequestHandler () {
return new MyHttpRequestHandler();
}
@Override
public void addInterceptors (InterceptorRegistry registry) {
InterceptorRegistration ir = registry.addInterceptor(new MyInterceptor());
}
@Bean
ExampleController controller () {
return new ExampleController();
}
}
The handler
package com.logicbig.example;
import org.springframework.web.HttpRequestHandler;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
public class MyHttpRequestHandler implements HttpRequestHandler {
@Override
public void handleRequest (HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
System.out.println("handling request in MyHttpRequestHandler");
PrintWriter writer = response.getWriter();
writer.write("response from MyHttpRequestHandler, uri: " + request.getRequestURI());
}
}
The @Controller
package com.logicbig.example;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class ExampleController {
@RequestMapping("/example")
@ResponseBody
public String handleRequest () {
System.out.println("in the controller");
return "response from /example";
}
}
To try examples, run embedded Jetty (configured in pom.xml of example project below):
mvn jetty:run
Output
Accessing the URI '/example' invokes our @Controller via RequestMappingHandlerMapping.
$ curl -s "http://localhost:8080/example" response from /example
Server Output preHandle, URI: /example in the controller postHandle, URI: /example
Now let's try the URI '/app', which invokes our MyHttpRequestHandler via SimpleUrlHandlerMapping:
$ curl -s "http://localhost:8080/app" response from MyHttpRequestHandler, uri: /app
Server Output preHandle, URI: /app handling request in MyHttpRequestHandler postHandle, URI: /app
Example ProjectDependencies and Technologies Used: - spring-webmvc 7.0.6 (Spring Web MVC)
Version Compatibility: 3.2.9.RELEASE - 7.0.6 Version compatibilities of spring-webmvc with this example: Versions in green have been tested.
- jakarta.servlet-api 6.1.0 (Jakarta Servlet API documentation)
- JDK 25
- Maven 3.9.11
|