How Leap Mvc Framework Works? How Http Asking Is Processed?

One of the oft asked Spring MVC Interview questions is almost explaining the menstruum of spider web asking i.e. how an HTTP asking is processed from starting fourth dimension to end. In other words, explaining the flow of asking inwards Spring MVC. Since many of my readers enquire this enquiry fourth dimension in addition to again, I idea to summarize the menstruum of asking processing inwards a curt article. It all starts alongside the client, which sends a asking to a specific URL. When that asking hitting the spider web container e.g. Tomcat it hold off into web.xml in addition to detect the Servlet or Filter which is mapped to that item URL. It the delegate that Servlet or Filter to procedure the request. Since Spring MVC is built on top of Servlet, this is every bit good the initial menstruum of asking inwards whatever Spring MVC based Java spider web application.

Remember, Web container e.g. Tomcat is responsible for creating Servlet in addition to Filter instances in addition to invoking their diverse life-cycle methods e.g. init(), service(), destroy(). In the instance of HTTP request, HttpServlet handles that in addition to depending upon the HTTP asking method diverse doXXX() method is invoked past times container e.g. doGet() to procedure GET asking in addition to doPost() to procedure POST request.

If y'all remember, to enable Spring MVC, nosotros demand to declare the DispatcherServlet from Spring MVC jounce into web.xml. This Servlet listens for a URL blueprint * every bit shown inwards below web.xml, which way all asking is mapped to DispatcherServlet.

Though it is non mandatory, y'all tin accept other servlet mapped to other URL if y'all desire to, simply if y'all are using Spring MVC to railroad train spider web application or RESTful spider web service, it brand feel to transcend through all asking via DispatcherServlet.


Here is the web.xml configuration for Spring MVC, y'all tin come across that DispatcherServlet is mapped to all asking using URL blueprint *
<web-app>  <!-- The front end controller of this Spring Web application, responsible  for treatment all application requests --> <servlet> <servlet-name>Spring MVC Dispatcher Servlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/config/web-application-config.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>  <servlet-mapping> <servlet-name>example</servlet-name> <url-pattern>*</url-pattern> </servlet-mapping>  </web-app> 

The URL blueprint is important, if the asking matches the URL blueprint of DispatcherServlet in addition to then it volition live on processed past times Spring MVC otherwise not. The DispatcherServlet the passes the asking to a specific controller depending on the URL requested. How does DispatcherServlet know which asking needs to live on passed to which controller?


Well, it uses the @RequestMapping annotation or Spring MVC configuration file to detect out mapping of asking URL to dissimilar controllers. It tin every bit good purpose specific asking processing annotations e.g. @GetMapping or @PostMapping. Controller classes are every bit good identified using @Controller in addition to @RestController (in the instance of RESTful Web Services) annotations. See REST alongside Spring course of education past times Eugen to larn how to railroad train RESTful Web Service using Spring inwards depth.

For example, below cast is a Controller which volition procedure whatever asking having URI "/appointments". It every bit good has @GetMapping, which way that method volition live on invoked when a GET asking is received for this URL. The method annotated alongside @PostMapping volition live on invoked if the customer sends a POST request to the "/appointments" URI.
@Controller @RequestMapping("/appointments") public class AppointmentsController {  @GetMapping public Map get() { return appointmentBook.getAppointmentsForToday(); }   @PostMapping public String add(@Valid AppointmentForm appointment, BindingResult result) { if (result.hasErrors()) { return "appointments/new"; } appointmentBook.addAppointment(appointment); return "redirect:/appointments"; } }

After processing the request, Controller returns a logical persuasion name in addition to model to DispatcherServlet and it consults persuasion resolvers until an actual View is determined to homecoming the output. DispatcherServlet in addition to then contacts the chosen persuasion e.g. Freemarker or JSP alongside model information in addition to it renders the output depending on the model data.

This Rendered output is returned to the customer every bit HTTP response. On it's way dorsum it tin transcend to whatever configured Filter every bit good e.g. Spring Security filter chain or Filters configured to convert the response to JSON or XML.

The DispatcherServlet from Spring MVC framework is an implementation of Front Controller Pattern (see Patterns of Enterprise Application Architecture) in addition to it's every bit good a Single signal of entry - grip all incoming requests, simply 1 time again that depends upon your URL blueprint mapping in addition to your application.

It delegates requests for farther processing to additional components e.g. Controllers, Views, View Resolvers, handler mappers, exception handlers etc. It tin every bit good map straight to /, simply in addition to then the exception for treatment static resources needs to live on configured. If y'all hold off at the web.xml configuration it every bit good pre-loaded using the load-on-startup tag.



Spring MVC locomote Flow

It's been often said that a moving-picture exhibit is worth a G words in addition to this is real truthful inwards the instance of agreement organization architecture in addition to workflow of your application. Whatever I accept said inwards higher upwards article, tin live on easily inferred past times looking at next diagram which explains workflow of Spring MVC framework:

RESTful Web Service asking is every bit good non real dissimilar from this. It follows the same path simply inwards the instance of REST, the Controller methods are annotated with @ResponseBody which way it doesn't homecoming a logical persuasion mention to DispatcherServlet, instead it write the output straight to HTTP response body. See Spring REST mass to larn to a greater extent than almost how to railroad train RESTful Web Services using Spring.

 is almost explaining the menstruum of spider web asking i How Spring MVC Framework works? How HTTP Request is processed?


In summary, hither is the menstruum of an HTTP asking inwards Java application created using Spring MVC framework:

1) Client sends an HTTP asking to a specific URL

2) DispatcherServlet of Spring MVC receives the request

2) It passes the asking to a specific controller depending on the URL requested using @Controller in addition to @RequestMapping annotations.

3) Spring MVC Controller in addition to then returns a logical persuasion mention in addition to model to DispatcherServlet.

4) DispatcherServlet consults persuasion resolvers until actual View is determined to homecoming the output

5) DispatcherServlet contacts the chosen persuasion (e.g. Thymeleaf, Freemarker, JSP) alongside model information in addition to it renders the output depending on the model data

6) The rendered output is returned to the customer every bit response

That's all almost what is the menstruum of Spring MVC or how an HTTP asking is processed past times Spring MVC. This is real basic simply of import noesis almost Spring MVC framework in addition to every Java in addition to Spring developer should live on familiar alongside this. If y'all know how your HTTP asking is processed in addition to then y'all tin non entirely empathise the issues ameliorate simply every bit good troubleshoot in addition to then easily in addition to quickly.

Further Reading
Spring Framework 5: Beginner to Guru
Spring Master Class - Beginner to Expert
Spring Certification
5 Best books to larn Spring MVC
Spring Interview Questions

Thanks a lot for reading this article thence far. If y'all similar this article in addition to then delight portion alongside your friends in addition to colleagues. If y'all accept whatever enquiry or proposition in addition to then delight drib a banking concern annotation in addition to I'll attempt to reply your question.

P.S. - If y'all desire to larn how to railroad train RESTful Web Service using Spring MVC inwards depth, I propose y'all bring together the REST alongside Spring certification class past times Eugen Paraschiv. One of the best course of education to larn REST alongside Spring MVC. 

Belum ada Komentar untuk "How Leap Mvc Framework Works? How Http Asking Is Processed?"

Posting Komentar

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel