This article appeared on my website: How to customize interceptors in Spring Boot projects
Servlet filters belong to the Servlet API and have little to do with Spring. In addition to wrapping Web requests with filters, Spring MVC also provides the HandlerInterceptor tool. According to the documentation, the HandlerInterceptor functions like a filter, but the interceptor provides finer control: before the request is responded to, after the request is responded to, before the view is rendered, and after the request is completely finished. We cannot modify the contents of the request using interceptors, but we can suspend the execution of the request by throwing an exception (or returning false).
Common interceptors in Spring MVC are: LocaleChangeInterceptor (for internationalization configuration) and ThemeChangeInterceptor. We can also add our own interceptors, as shown in the demo provided with this article
In actual combat
Adding an interceptor not only defines beans in WebConfiguration, Spring Boot provides the base WebMvcConfigurerAdapter class that our WebConfiguration class in our project needs to inherit.
- Inheritance WebMvcConfigurerAdapter;
- Adding the @bean definition to LocaleChangeInterceptor simply defines an Interceptor Spring Bean, but Spring Boot does not automatically add it to the call chain.
- Interceptors need to be manually added to the call chain.
The complete WebConfiguration code after modification is as follows:
package com.test.bookpub; import org.apache.catalina.filters.RemoteIpFilter; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import org.springframework.web.servlet.i18n.LocaleChangeInterceptor; @Configuration public class WebConfiguration extends WebMvcConfigurerAdapter { @Bean public RemoteIpFilter remoteIpFilter() { return new RemoteIpFilter(); } @Bean public LocaleChangeInterceptor localeChangeInterceptor() { return new LocaleChangeInterceptor(); } @Override public void addInterceptors(InterceptorRegistry registry { registry.addInterceptor(localeChangeInterceptor()); }}Copy the code
Using MVN spring – the boot: run to run the program, and then through httpie visit http://localhost:8080/books? Locale =foo, the following error message is displayed on the terminal.
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.UnsupportedOperationException: Cannot change HTTP accept header - use a different locale resolution strategy] with root causeCopy the code
PS: The error here is not because the locale we entered is incorrect, but because the default locale change policy does not allow changes requested from the browser. This error indicates that the interceptor we defined earlier is working.
Analysis of the
In our example project, we override and rewrite the addInterceptors(InterceptorRegistory registory) method, which is a typical callback function — adding a custom interceptor using the function’s parameter Registry.
During the automatic configuration phase of Spring Boot, Spring Boot scans all instances of WebMvcConfigurer and calls their callbacks sequentially, which means: If we want to logically isolate configuration information, we can define multiple instances of WebMvcConfigurer in the Spring Boot project.
Spring Boot 1.x series
- Automatic configuration of Spring Boot, command-line-runner
- Learn about automatic configuration of Spring Boot
- Use of Spring Boot’s @propertysource annotation in integrating Redis
- How do I customize the HTTP message converter in the Spring Boot project
- Spring Boot integration Mongodb provides Restful interfaces
- Scope for a bean in Spring
- The event dispatcher pattern is used in Spring Boot projects
- Error handling practices when Spring Boot provides RESTful interfaces
- Spring Boot实战之定制自己的starter
- How does a Spring Boot project support both HTTP and HTTPS
- How does the custom Spring Boot Starter set automatic configuration annotations
- Mockito is used in the Spring Boot project
- Test the framework with Spock in the Spring Boot project
*** This blog focuses on topics such as backend technology, JVM troubleshooting and optimization, Java interview questions, personal growth and self-management. It provides readers with the experience of working and growing as a front-line developer. We look forward to your learning here.