[JAVA] Understand the rough flow from request to response in SpringWebMVC

When using Spring, I often had a lot of trouble if I didn't understand the processing flow to some extent, so I will summarize the contents of reading the source as my own memo.

DispatcherServlet All requests carry out the processing of this Servlet. To follow the process flow, I think you should look at the source from the doService method.

In order for this Servlet to call subsequent processing (Controller etc.) or to generate a response using JSP etc. You will need the following beans.

The general processing flow is as follows.

  1. Use HandlerMapping to get the subsequent processing from the request.
  2. Use the HandlerAdapter to execute the process acquired in 1. 3-1. If an exception does not occur as a result of executing process 2, use ViewResolver to acquire View from the process result of 2 and execute drawing process. 3-2. If an exception occurs as a result of executing process 2, acquire and execute the method that handles the exception that occurred in HandlerExceptionResolver.

HandlerMapping Get the subsequent processing from the request with the getHandle method and return the HandlerExecutionChain. The HandlerExecutionChain can hold not only the handler (subsequent Controller method) but also multiple Interceptors. DispatcherServlet uses this HandlerExecutionChain to execute Interceptor and handler.

A class often used to implement this interface is the RequestMappingHanlderMapping class. This class connects requests and processes based on the contents of @RequestMapping of the Controller class.

HandlerAdapter Adatpter class for executing the handler obtained by HandlerMapping. Processes nicely before and after handler execution. (Roughly)

A class often used to implement this interface is the RequestMappingHandlerAdapter class. It stores request parameters in an object, validates them, and generates a ModelAndView from the return value of a handler. (Strictly speaking, processing is delegated to HandlerMethodArgumentResolver and HandlerMethodReturnValueHandler.)

HandlerExceptionResolver When an exception occurs, it connects the exception with subsequent processing.

A class often used to implement this interface is the ExceptionHandlerExceptionResolver class. Methods with @ExceptionHandler in Contoller and ControllerAdvice will be executed from this class. By the way, as you can see by checking the contents, if there is a @ExceptionHandler that handles the same exception in Controller and ControllerAdvice, the Controller side has priority.

ViewResolver You can get the View class from ViewName.

A class often used to implement this interface is the InternalResourceViewResolver class. This class can get JstlView for drawing JSP from ViewName. Also, if the prefix "redirect:" is set in ViewName, a RedirectView for redirect processing will be generated.

LocaleResolver It seems that you can get the Locale from the request. Since I have never used it, I do not know the details, but if you use SessionLocaleResolver, you can hold Locale information in Session, so it seems that you can manage Locale for each user.

ThemeResolver It seems that you can get Theme from the request. I haven't used Spring's Theme in the first place, so I don't know the details. ..

MultipartResolver An interface for generating a Multipart File from a multipart / form-data request. If you need a function such as file upload, you need to define this Resolver as a bean.

FlashMapManager An interface for managing FlashScope. FlashScope is a scope for holding values until the redirect destination. Normally, SessionFlashMapManager is used and Session is used to realize FlashScope. I have written about the mechanism here, so please refer to it if you like. Pass the value to the redirect destination without using Redirect Attributes in Spring MVC

Recommended Posts

Understand the rough flow from request to response in SpringWebMVC
Confirmation and refactoring of the flow from request to controller in [httpclient]
I want to understand the flow of Spring processing request parameters
Java reference to understand in the figure
Tokoro I rewrote in the migration from Wicket 7 to 8
Understand the characteristics of Scala in 5 minutes (Introduction to Scala)
Java classes and instances to understand in the figure
How to determine the look-ahead request (Prefetch) from the browser
What I did in the version upgrade from Ruby 2.5.2 to 2.7.1
Change Spring Boot REST API request / response from CamelCase to SankeCase
What I did in the migration from Spring Boot 1.4 series to 2.0 series
What I did in the migration from Spring Boot 1.5 series to 2.0 series
[Android] I want to get the listener from the button in ListView
Pass the i18n locale to JavaScript
[java8] To understand the Stream API
The road from JavaScript to Java
How to get the setting value (property value) from the database in Spring Framework
[Rails] Articles for beginners to organize and understand the flow of form_with
What is CHECKSTYLE: OFF found in the Java source? Checkstyle to know from
Shorten the UUID to base64 in Swift.
Investigate the replacement from Docker to Podman.
[HTTP] Status code included in the HTTP response
Convert request parameter to Enum in Spring
[Ruby] From the basics to the inject method
To write Response data directly in Spring
[Java] Flow from source code to execution
How to get the date in java
Understand in 5 minutes !! How to use Docker
03. I sent a request from Spring Boot to the zip code search API
What is ... (3 dots) found in the Java source? Variadic arguments to know from