Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Illustrating the processing flow and mechanism of Spring:HTTP requests [1]

2025-03-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

In 2003, veteran brother first came to ZTE to start a graduate internship. Spring was born that year. Version 1.0 was released in March 2004, and it has been more than 15 years now. From monolithic hierarchical architecture to cloud original micro service architecture, it has never been shaken in the top position of JAVA application framework. It brings us great value, which can not only reduce the difficulty of development, but also improve the efficiency of development. But time this pig knife not only changed the veteran brother, but also did not let go of Spring, we have become more and more powerful.

Before the launch of Spring Boot / Spring Cloud, it has evolved into 5 major versions and numerous small versions, and its functions and ecology are becoming more and more rich. But for friends who are new to Spring, this is not fair, unlike veterans who can grow up with it, now this technology stack is already very huge, is there any shortcut to eat through this giant in a short period of time? Yes, just like when the DOS operating system was installed on a floppy disk, with a total of only a few MB, now there are dozens or hundreds of GB at any time, but the operating system kernel is very small, and its principle and mechanism is those in textbooks that give birth to two, two, three and three things in a lifetime. Only by mastering these stable "one, two, three", that is, the core principle mechanism (for example: IOC\ AOP\ ORM, etc.), then our learning can achieve twice the result with half the effort.

Just like we bought a rough house, we must decorate before moving in, in which the wiring of water, electricity, gas, network and other pipelines must go first, and different pipelines have different ways, some walk the floor, some walk on the wall, and so on. Only when the pipes are laid properly can we lay floor tiles, ceiling ceilings, paint wall stickers, and so on. As an owner, if you do not know the alignment of these pipes, when there are power outages, power outages or leaks, you have no idea how to analyze, locate and repair them. Veteran elder brother in the company often received questions for help, because the partners do not know these principles and mechanisms, quite simple problem analysis and positioning for many days still have no clue. If we follow the principle and mechanism, then we can do it easily.

In fact, these underlying principles and mechanisms are not complicated, just that we do not know, or lack of good data, the veteran is going to analyze these principles and mechanisms to everyone by means of illustration. This series of articles will focus on the whole process of Spring processing HTTP requests, and help you understand the pipeline wiring in Spring, a skyscraper, so that you can get twice the result with half the effort and make it easier to use. It will include the following aspects:

The whole process of HTTP request processing, including browser, Web server, application Spring, etc.; the interface, cooperation mechanism and configuration rules between Web server and application Spring; the mechanism of Spring handling HTTP requests, including Dispatcher, Controller, View, Model, Service, DAO, etc.; the subprocesses of HTTP request processing in different application architecture scenarios, including JSP, front-end separation, etc. Description of configuration files related to HTTP request processing, including Web, Bootstrap, Spring MVC, Application Context, etc.; description of common problems in HTTP request processing, etc.

Suitable for readers: development, testing, architecture, etc.

1. Overview of HTTP request processing process

The cyberspace built by computing institutions is a virtual world, which is an extension of our human brainpower. Although the virtual world is both abstract and illusory, it is ultimately based on our experience in building the physical world. I have been in the IT world for a long time. Veteran, I have found a way to understand the virtual world, that is, to find a prototype in the real world. The prototype is more vivid and vivid, and we are also very familiar with it. Through the prototype, we can more accurately understand the mechanism behind computer technology. What real-world prototypes can we use in the process of handling HTTP requests by clients (or browsers), Web containers, Web applications, Spring frameworks, and so on? We can compare it to the process of delivering documents and parcels by logistics express.

Computer network is like the traffic network in the real world, the main responsibility of the network extending in all directions is to carry goods. In the OSI seven-layer or TCP/IP four-layer network protocol model, HTTP (Hyper Text Transfer Protocol) and FTP (File Transfer Protocol), SMTP (Simple Mail Transfer Protocol), POP3 (Post Office Protocol 3), IMAP4 (Internet Mail Access Protocol) and other protocols belong to the application layer protocols. These protocols are like logistics express companies transporting different types of goods. FTP system is dedicated to delivering documents, while SMTP, POP3 and IMAP4 are dedicated to delivering mail. The HTTP protocol was originally used for hypertext. However, with the vigorous development of the World wide Web (WWW), the HTTP protocol system has been the most extensive development. It has a very good design, brings flexibility, expansibility and ease of use, and can carry various types of information transmission. Just like the Internet logistics companies are getting bigger and bigger, they can cover more and more businesses.

HTTP request or response is like a parcel, which is divided into header and style, in which the header is the information load carried, and the header is like the express form we usually fill in when mailing things, including the address of the recipient, service level requirements, description of mailed items, and so on. For the topic of this article, we will only focus on the analysis of the recipient address. The HTTP URL of a RESTful-style service interface API is as follows:

Http:// {ip}: {port} / {context-path} / {service-name} / {method-name}

To take a specific example, for example, we are going to deliver a document to the following address to complete user registration:

Http://201.187.10.21:8080/spring-demo/user/register

The above format is for us to read, and eventually the information has to be filled in the HTTP message. The real effect is as follows:

POST / spring-demo/user/register HTTP/1.1

Accept: application/json

Accept-Language: zh-cn

User-Agent: Mozilla/5.0

Host: 201.187.10.21:8080

Content-Length: 112

Connection: Keep-Alive

Cache-Control: no-cache

Cookie: JSESSIONID=12DII6898EFY998APBE17B

Name=itlaobingge&password=123456&sex=male&age=30

Next, let's parse the HTTP URL paragraph by paragraph to see the meaning of each piece of information:

Ip:IP address information for addressing different computers in a particular network. Port: Port information for addressing different Web containers on the same computer, which can run multiple Web container instances. Context-path: context path, which is used to address different applications within the same Web container. Multiple applications can be deployed in one Web container instance. Service-name: service name, which is used to address different services within the same application. There are usually multiple services within an application. Method-name: method name, which is used to address different methods within the same service. A service usually contains multiple methods.

The above recipient address is filled in by the client (or browser). The HTTP transmission system will deliver the mail packet according to the recipient address information, find a specific computer based on the IP address or domain name information, and the computer will deliver the mail packet to the specific Web container instance according to the port information. Next, the Web container will take over the packet delivery task, and it will find the specific application based on the context path. If the current application is built using the Spring framework, Spring will take over the parcel delivery task, and it will find a service method that can actually handle the payload information in the package based on the service name and method name. The service method will process the information in the parcel in time, and then load the resulting information into the new parcel and return it to the client (or browser). As shown in the following figure, this is the process of HTTP requests traversing networks, computers, Web containers, Web applications, and Spring frameworks.

The main value of this article is to help you sort out the end-to-end framework of the whole process, that is, the global perspective or God perspective. With this framework, we can according to our own needs to find clues to find the relevant node information to study, so as not to fall into the details can not find the direction. Of course, considering that our work and study conditions are different and the problems we usually encounter are also different, this article can not cover the problems encountered by everyone. You are welcome to leave messages and questions, and you are welcome to follow my blog or the official name "IT veteran brother" to communicate and interact. I will try my best to answer your questions as soon as possible. Thank you!

The index of other articles in this series is as follows:

Diagram Spring:HTTP request processing flow and mechanism [2] Diagram Spring:HTTP request processing flow and mechanism [3] Diagram Spring:HTTP request processing flow and mechanism [4] Diagram Spring:HTTP request processing flow and mechanism [5]

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report