Showing posts with label Spring MVC. Show all posts
Showing posts with label Spring MVC. Show all posts

Friday, March 30, 2018

How Spring MVC supports REST Resources?

Spring has long had some of the ingredients needed for exposing REST resources.
Starting with version 3.0, however, Spring began adding enhancements to Spring MVC
to provide first-class REST support.

Spring supports the creation of REST resources in the following ways:
  • Controllers can handle requests for all HTTP methods, including the four primary REST methods: GET, PUT, DELETE, and POST. Spring 3.2 and higher also supports the PATCH method.
  • The @PathVariable annotation enables controllers to handle requests for parameterized URLs (URLs that have variable input as part of their path).
  • Resources can be represented in a variety of ways using Spring views and view resolvers, including View implementations for rendering model data as XML,JSON, Atom, and RSS.
  • The representation best suited for the client can be chosen using Content Negotiating ViewResolver
  • View-based rendering can be bypassed altogether using the @ResponseBody annotation and various HttpMethodConverter implementations.
  • Similarly, the @RequestBody annotation, along with HttpMethodConverter
    implementations, can convert inbound HTTP data into Java objects passed in to
    a controller’s handler methods.
  • Spring applications can consume REST resources using RestTemplate.
     

Sunday, March 12, 2017

Spring MVC Architecture

In this article we will discuss and learn about Spring MVC architecture.First let us know what is MVC. MVC is an open architecture with multi tiers.MVC is a design pattern stands for Model,view and Controller. It is used to design web applications in standard manner that means it will provide a pattern to design web applications.It can able to provide the roles and responsibilities of the components which we used in MVC pattern or architecture.

Why need to use Spring MVC architecture:

We are using MVC design pattern to develop web applications.It is good to develop your applications where application data and it clearly separates business,navigation and presentation logic proven mechanism for building a thin,clean web-tier.

Spring MVC Components:

Controller:
It handles the navigation logic and and interacts with the service tier for business logic

Model:

 It acts as Contract between the Controller and the View.Model is also responsible contains the data needed to render the view  

View:

This component job is to pull the data from the model and renders the response to the client.


Now we will see the flow of Spring MVC architecture:



Step 1: Whenever we submitting the request from the client(Browser) that request will received by the Dispatcher Servlet. It is not necessary to write any single servlet. Since Spring provides pre-written servlet. This servlet is called Dispatcher Servlet. So the main purpose of Dispatcher Servlet is received all incoming requests and coordinating the different subsystems of Spring MVC. The Dispatcher Servlet also creates webApplicationContext,which contains web specific component such as Controllers and ViewResolver.

Step 2: In this step Dispatcher Servlet will take the help of Handler Mapping and get to know the Controller class name associated with the given request. So request transfer to the Controller.

Step 3: In this step Controller will process the request by executing appropriate methods and returns ModeAndView object(contains Model data and view name)back to the Dispatcher Servlet.

Step 4: The Front Controller then tries to resolve the view(Which can be JSP,Freemaker,Velocity etc) by consulting the View Resolver object.

Step 5: In this step the selected view is then rendered.

Step 6: Finally the rendered view back to client.

Watch video for More details: Spring MVC architecture


See also:

Spring Interview Questions
Struts Interview Questions
Hibernate Interview Questions
JSP Interview Questions
MVC Architecture using JSP and Servlet

High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...