4. HTTP request processing flow in Spring framework
After traversing the Web container and the Web application, the HTTP request is delivered to the Spring framework, and we continue to parse the flow. The interface between Web applications and Spring MVC is done through the configuration file MVC-servlet.xml, which defines the various core components and initial configurations that make up Spring MVC, including: Controller Controller, View resolver, View View, and so on. Different components perform different functions, and as usual, we take a look at these core components before introducing the Spring framework for handling HTTP requests.
4.1 Introduction to core Components of the Spring Framework
Most of the annotations we application developers use in Spring are @Component, @Controller, @Service, @Repository, etc. These are the core components of Spring. In addition, we will use @requestMapping, @requestParam, @pathVariable, @requestBody and other auxiliary annotations:
-
Model Model: Encapsulates business data, mainly in the form of POJOs.
-
Controller Controller: Schedules and controls specific Service processes and invokes Service logic services. The Controller Controller returns the ModelAndView object containing the Model object and View name to the pre-dispatcher DispatcherServlet after processing the HTTP request.
-
View: Responsible for rendering and presenting Model data and providing forms for users to input various business data.
-
The ViewResolver is responsible for getting an instance object of a particular View subclass based on a known View name.
-
HandlerMapping: registers and searches controllers, held by the ApplicationContext ApplicationContext. Specifically, it has a member property of type HashMap<String, Object> handlerMap, where key is the URI information of the HTTP request, The value can be a string or the HandlerExecutionChain that handles the request. If it is a String, it is treated as the Spring Bean name.
-
Service Service: Is responsible for implementing specific business logic.
-
Data storage Object Repository: Also known as DAO (Data Access Object). No matter what development framework is used, most applications need to interact with databases. DAO encapsulates database Access operations and isolates SQL complexity.
All HTTP requests arriving in Spring MVC are distributed by the pre-dispatcher DispatcherServlet. Before sending requests to a specific Controller, HandlerMapping is used to locate the requests. The process is as follows:
-
The Web container listens on a host-specific port and eventually calls the Servlet’s Service method to handle the HTTP request whenever a request arrives. In Spring Web applications, the Servlet that receives HTTP requests is the pre-dispatcher Servlet.
-
Determine HTTP request types (GET, POST, PUT, PATCH, etc.) in the service method of the pre-dispatcherServlet, and then call doGet(), doPost(), doPut(), etc.
-
Initialize the request context by executing the processRequest() method in the doGet(), doPost(), doPut(), etc.
-
Call the doService() method, further executing the doDispatch() method.
-
Get the mappedHandler and HandlerAdapter of the HTTP request in the doDispatch() method, then initiate a call to the business Controller Controller and subsequent processes, and wait for the result of processing before building the response data.
4.2 Spring Framework processes HTTP requests
- Once the pre-dispatcher Servlet receives the HTTP request, it looks up the appropriate Controller, Controller, to handle the request. It gets the URI by parsing the URL of the HTTP request. According to the URI, the Handler and HandlerInterceptor corresponding to the request are obtained from HandlerMapping and returned as HandlerExecutionChain.
- The pre-dispatcher DispatcherServlet selects the appropriate adapter HandlerAdapter based on the obtained processor Handler. If the adapter HandlerAdapter is successfully obtained, its interceptor method preHandler() takes precedence before calling the Handler Handler.
- The preHandler() method takes the data from the HTTP request and populates it with the Handler’s input parameter, and then starts calling the Handler(Controller) related methods.
- The Controller Controller completes execution and returns a ModelAndView name object, ModelAndView, to the pre-dispatcher DispatcherServlet.
- The pre-dispatchservlet selects an appropriate ViewResolver based on the ModelAndView name object ModelAndView, provided that the ViewResolver has been registered with the Spring IOC container.
- The ViewResolver gets a particular View View based on the name of the View specified in ModelAndView.
- The pre-dispatchServlet populates the model data into the view and returns the render results to the client.
Spring also does some pre-processing based on the configuration while populating the Handler entry parameter:
- HttpMessageConveter: Converts request messages (JSON\XML and other formatted data) into Java objects.
- Data conversion: Converts the data in the HTTP request, for example, converting a String to an Integer or Double.
- Data formatting: Formatting specific data in an HTTP request, such as converting a string to a formatted number or a formatted date.
- Data validation: Verifies the validity of data (length, format, etc.) and stores the validation result in BindingResult or Error.
4.3 Differences in HTTP request Processing flows in Different Application architectures
Spring Web application architecture has experienced multiple stages of development. At first, the mainstream front-end view technology is JSP, and then the three-swordsman framework SSH (Struts\Spring\Hibernate) is evolved on this basis. But at this time, the front and back ends are still coupled together. Whether JSP or SSH, the Spring framework must rely on the ViewResolver and View View to handle HTTP requests.
It has been more than 15 years since Spring was born, and the back-end technologies associated with it have not evolved as fast as the front-end, mainly because of the increasing complexity and workload of the front-end view layer development as the front-end requirements have become more diverse. In this context, more and more front-end engineering solutions are emerging, the most effective of which is front and back end separation, from AngularJS\Backbone.js to now React\Vue, etc. The front-end consists of static resources (HTML, Javascript, CSS) that can be deployed independently on a Web server, eliminating the need for the Spring framework to handle view-related content. The Controller no longer returns the ModelAndView, just the model data.
Today first share here, if you feel valuable, please move your finger to forward to other partners in need. In addition, I will share my experience in career planning, job interviews, skills improvement and influence building in the future. Please pay attention to this column or “IT veteran brother”!
Other articles in this series are indexed below:
- Spring: HTTP Request Processing flow and mechanism [1]
- Spring: HTTP Request Processing and mechanism [2]
- Spring: HTTP Request Processing and mechanism [3]
- Spring: HTTP Request Processing flow and mechanism [5]