A study note based on Spring MVC Documentation. Spring MVC is built on the Servlet API. The current version is 5.3.0.
DispatcherServlet, provides a shared algorithm for request processing, while actual work is performed by configurable delegate components. Both the
DispatcherServlet and its delegate components need to be declared and mapped. For example:
2 Special Bean Types
DispatcherServlet checks the
WebApplicationContext for special beans and delegates to special beans to process requests and render the appropriate responses.
HandlerMapping: Map a request to a handler along with a list of interceptors for pre- and post-processing. The mapping is based on some criteria. Two main implementations are
SimpleUrlHandlerMapping: maitains explicit registrations of URI path patterns to handlers.
DispatcherServletto invoke a handler mapped to a request. It hides the handler invokation details.
HandlerExceptionsResolver: strategy to resolve exceptions. Exceptions can be handled by mapping class names to error view names, mapping exception to HTTP status codes etc.
ViewResolver: Resolve logic string-vased view names returned from a handler to an actual
LocalResolver: resolve the
ThemeResolver: resolve themes.
MultipartResolver: parse a multi-part request such as file upload.
FlashMapManager: store and retrieve the input and output
FlashMapthat can be used to pass attributes from one request to another, usually across redirect.
WebApplicationContext is searched for and bound in the request as an attribute that the controller and other elements in the process can use.
An appropriate handler is searched for. If a handler is found, the execution chain associated with the handler (preprocessors, postprocessors, and controllers) is run to prepare a model for rendering. Alternatively, for annotated controllers, the response can be rendered (within the
HandlerAdapter) instead of returning a view.
If a model is returned, the view is rendered. If no model is returned (maybe due to a preprocessor or postprocessor intercepting the request, perhaps for security reasons), no view is rendered, because the request could already have been fulfilled.
HandlerMapping implementations support handler interceptors that are useful when you want to apply specific functionality to certain requests — for example, checking for a principal. Interceptors must implement
HandlerInterceptor that provide three methods:
4 Annotated Controllers
Spring MVC provides an annotation-based programming model where
@RestController components use annotations to express request mappings, request input, exception handling, and more.
You can use the
@RequestMapping, or the more specific
@PostMapping annotations to map requests to controllers methods. It has various attributes to match by URL, HTTP method, request parameters, headers, and media types. You can use it at the class level to express shared mappings or at the method level to narrow down to a specific endpoint mapping.
@RequestMapping handler methods have a flexible signature and can choose from a range of supported controller method arguments and return values. Arugments include
@CookieValue etc. Return values could be
5 Functional Endpoints
Spring Web MVC includes
WebMvc.fn, a lightweight functional programming model in which functions are used to route and handle requests and contracts are designed for immutability. It runs on the same
In WebMvc.fn, Incoming requests are routed to a handler function with a RouterFunction: a function that takes ServerRequest and returns an optional
HandlerFunction is a function that takes
ServerRequest and returns a
ServerResponse. For example:
ServerRequest provides access to the HTTP method, URI, headers, body and query parameters.
ServerResponse provides access to the HTTP response and, since it is immutable, you can use a build method to create it. You can use the builder to set the response status, to add response headers, or to provide a body.
You can filter handler functions by using the
after methods on the routing function builder.