Professional Documents
Culture Documents
• Configuration
• RESTful Webservices
• The Views render response to the user with the help of the model object
– For example, HTML output
• Controllers are responsible for receiving the request from the user, calling the
back-end services, and passing the resulting model to the right view
3
Spring MVC architecture and behavior
1. The DispatcherServlet first receives the request and consults the HandlerMapping
2. The DispatcherServlet invokes the Controller associated with the request.
The Controller process the request by calling the appropriate service methods and
returns the view name to the DispatcherServlet.
│ It can also be an ModelAndView object containing the model data and the view name.
4
Configuration
• We need to map requests that we want the DispatcherServlet to handle, by using a URL
mapping in the web.xml file (kept in the WebContent/WEB-INF directory)
– Upon initialization of HelloWeb DispatcherServlet, the framework will try to load the application
context from a file named [servlet-name]-servlet.xml located in the application's WebContent/WEB-
INF directory
5
Configuration
• In the MVC configuration file (HelloWeb-
servlet.xml in our example) :
Beans definition override the definitions of any beans defined with the same name in the global scope
InternalResourceViewResolver has rules to resolve the view names. As per the above defined rule, a
logical view named hello is delegated to a view implementation located at /WEB-INF/view/hello.jsp
6
Annotations
• The DispatcherServlet delegates the request to the controllers to execute the
functionality specific to it.
– The @Controller annotation indicates that a particular class is a controller.
– The @RequestMapping annotation is used to map a URL to either an entire class or a particular
handler method.
different requests
The value attribute indicates the URL to which the handler method is
mapped and the method attribute defines the service method to
handle HTTP GET request.
8
View: example with a simple JSP
/WEB-INF/view/hello.jsp
Spring MVC also supports many types of views for different presentation technologies:
│ JSPs, HTML, PDF, Excel worksheets, XML, Velocity templates, XSLT, JSON, etc.
9
Parameters and path variables
• URL: http://localhost:8080/MyApp/user/1234/invoices?date=12-05-2013
• Gets the invoices of the user with id 1234 for the date of December 5th, 2013
• REST-compliant Web services allow the requesting systems to access and manipulate
textual representations of web resources by using a uniform and predefined set of
stateless operations
- a base URL
- media type (JSON is default in Spring 4)
- standard HTTP methods for interaction (e.g., OPTIONS, GET, PUT, POST, and DELETE)
• http://localhost:8080/greeting?na
me=User
• Which is a customized version returning:
• {"id":1,"content":"Hello, User!"}
12
RESTful Web services in Spring
• A Resource representation
class:
– the id field is a unique
identifier for the greeting
– content is the textual
representation of the
greeting
14
Consuming a RESTful Web service
• Let us consider the RESTful service that randomly fetches quotes
about Spring Boot and returns them as a JSON document:
• http://gturnquist-quoters.cfapps.io/api/random
15
• A domain class with the data
we need
– @JsonIgnoreProperties fro
m the Jackson JSON
processing library to
indicate that any properties
not bound in this type
should be ignored
– Specify the variable name
exact same as the key in the
JSON Document returned
from the API.
– In case your variable name
and key in JSON doc are not
matching, use
@JsonPropertyannotation
to specify the exact key of
JSON document
16
• An additional class is needed to embed
the inner quotation itself
17