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

One of the often asked Spring MVC Interview questions is close explaining the time period of spider web asking i.e. how an HTTP asking is processed from kickoff to end. In other words, explaining the flow of asking inward Spring MVC. Since many of my readers enquire this enquiry fourth dimension together with again, I idea to summarize the time period of asking processing inward a brusque article. It all starts amongst the client, which sends a asking to a specific URL. When that asking hitting the spider web container e.g. Tomcat it await into web.xml together with honor 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 time period of asking inward whatever Spring MVC based Java spider web application.

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

If you lot remember, to enable Spring MVC, nosotros demand to declare the DispatcherServlet from Spring MVC offend into web.xml. This Servlet listens for a URL designing * every bit shown inward below web.xml, which way all asking is mapped to DispatcherServlet.

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


Here is the web.xml configuration for Spring MVC, you lot tin run across that DispatcherServlet is mapped to all asking using URL designing *
<web-app>  <!-- The forepart 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 designing is important, if the asking matches the URL designing of DispatcherServlet hence it volition endure processed yesteryear 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 endure passed to which controller?


Well, it uses the @RequestMapping notation or Spring MVC configuration file to honor out mapping of asking URL to dissimilar controllers. It tin every bit good role specific asking processing annotations e.g. @GetMapping or @PostMapping. Controller classes are every bit good identified using @Controller together with @RestController (in the instance of RESTful Web Services) annotations. See REST amongst Spring course of written report yesteryear Eugen to acquire how to railroad train RESTful Web Service using Spring inward 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 endure invoked when a GET asking is received for this URL. The method annotated amongst @PostMapping volition endure 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 together with model to DispatcherServlet and it consults persuasion resolvers until an actual View is determined to homecoming the output. DispatcherServlet hence contacts the chosen persuasion e.g. Freemarker or JSP amongst model information together with 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 overstep 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) together with it's every bit good a Single betoken of entry - handgrip all incoming requests, simply i time again that depends upon your URL designing mapping together with 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 hence the exception for treatment static resources needs to endure configured. If you lot await at the web.xml configuration it every bit good pre-loaded using the load-on-startup tag.



Spring MVC move Flow

It's been often said that a film is worth a thou words together with this is really truthful inward the instance of agreement organisation architecture together with workflow of your application. Whatever I convey said inward to a higher house article, tin endure easily inferred yesteryear looking at next diagram which explains workflow of Spring MVC framework:

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

 is close explaining the time period of spider web asking i How Spring MVC Framework works? How HTTP Request is processed?


In summary, hither is the time period of an HTTP asking inward 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 together with @RequestMapping annotations.

3) Spring MVC Controller hence returns a logical persuasion advert together with 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) amongst model information together with it renders the output depending on the model data

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

That's all close what is the time period of Spring MVC or how an HTTP asking is processed yesteryear Spring MVC. This is really basic simply of import noesis close Spring MVC framework together with every Java together with Spring developer should endure familiar amongst this. If you lot know how your HTTP asking is processed hence you lot tin non alone empathise the issues improve simply every bit good troubleshoot hence easily together with quickly.

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

Thanks a lot for reading this article hence far. If you lot similar this article hence delight part amongst your friends together with colleagues. If you lot convey whatever enquiry or proffer hence delight drib a banking concern complaint together with I'll endeavor to reply your question.

P.S. - If you lot desire to acquire how to railroad train RESTful Web Service using Spring MVC inward depth, I propose you lot bring together the REST amongst Spring certification class yesteryear Eugen Paraschiv. One of the best course of written report to acquire REST amongst Spring MVC. 

Sumber https://javarevisited.blogspot.com/

0 Response to "How Saltation Mvc Framework Works? How Http Asking Is Processed?"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel