[.small]#<<web.adoc#mvc-cors, Same as in Spring MVC>>#
[.small]#<<web.adoc#mvc-cors, Web MVC>>#
Spring WebFlux lets you handle CORS (Cross-Origin Resource Sharing). This section
describes how to do so.
@ -10,7 +10,7 @@ describes how to do so.
@@ -10,7 +10,7 @@ describes how to do so.
[[webflux-cors-intro]]
== Introduction
[.small]#<<web.adoc#mvc-cors-intro, Same as in Spring MVC>>#
[.small]#<<web.adoc#mvc-cors-intro, Web MVC>>#
For security reasons, browsers prohibit AJAX calls to resources outside the current origin.
For example, you could have your bank account in one tab and evil.com in another. Scripts
@ -27,7 +27,7 @@ powerful workarounds based on IFRAME or JSONP.
@@ -27,7 +27,7 @@ powerful workarounds based on IFRAME or JSONP.
[[webflux-cors-processing]]
== Processing
[.small]#<<web.adoc#mvc-cors-processing, Same as in Spring MVC>>#
[.small]#<<web.adoc#mvc-cors-processing, Web MVC>>#
The CORS specification distinguishes between preflight, simple, and actual requests.
To learn how CORS works, you can read
@ -77,7 +77,7 @@ To learn more from the source or to make advanced customizations, see:
@@ -77,7 +77,7 @@ To learn more from the source or to make advanced customizations, see:
[[webflux-cors-controller]]
== `@CrossOrigin`
[.small]#<<web.adoc#mvc-cors-controller, Same as in Spring MVC>>#
[.small]#<<web.adoc#mvc-cors-controller, Web MVC>>#
The {api-spring-framework}/web/bind/annotation/CrossOrigin.html[`@CrossOrigin`]
annotation enables cross-origin requests on annotated controller methods, as the
@ -230,7 +230,7 @@ as the following example shows:
@@ -230,7 +230,7 @@ as the following example shows:
[[webflux-cors-global]]
== Global Configuration
[.small]#<<web.adoc#mvc-cors-global, Same as in Spring MVC>>#
[.small]#<<web.adoc#mvc-cors-global, Web MVC>>#
In addition to fine-grained, controller method-level configuration, you probably want to
define some global CORS configuration, too. You can set URL-based `CorsConfiguration`
@ -299,7 +299,7 @@ as the following example shows:
@@ -299,7 +299,7 @@ as the following example shows:
[[webflux-cors-webfilter]]
== CORS `WebFilter`
[.small]#<<web.adoc#mvc-cors-filter, Same as in Spring MVC>>#
[.small]#<<web.adoc#mvc-cors-filter, Web MVC>>#
You can apply CORS support through the built-in
{api-spring-framework}/web/cors/reactive/CorsWebFilter.html[`CorsWebFilter`], which is a
[.small]#<<web.adoc#webmvc-fn, Same as in Spring MVC>>#
[.small]#<<web.adoc#webmvc-fn, Web MVC>>#
Spring WebFlux includes WebFlux.fn, a lightweight functional programming model in which functions
are used to route and handle requests and contracts are designed for immutability.
@ -12,7 +12,7 @@ the same <<web-reactive.adoc#webflux-reactive-spring-web>> foundation.
@@ -12,7 +12,7 @@ the same <<web-reactive.adoc#webflux-reactive-spring-web>> foundation.
[[webflux-fn-overview]]
== Overview
[.small]#<<web.adoc#webmvc-fn-overview, Same as in Spring MVC>>#
[.small]#<<web.adoc#webmvc-fn-overview, Web MVC>>#
In WebFlux.fn, an HTTP request is handled with a `HandlerFunction`: a function that takes
`ServerRequest` and returns a delayed `ServerResponse` (i.e. `Mono<ServerResponse>`).
@ -112,7 +112,7 @@ Most applications can run through the WebFlux Java configuration, see <<webflux-
@@ -112,7 +112,7 @@ Most applications can run through the WebFlux Java configuration, see <<webflux-
[[webflux-fn-handler-functions]]
== HandlerFunction
[.small]#<<web.adoc#webmvc-fn-handler-functions, Same as in Spring MVC>>#
[.small]#<<web.adoc#webmvc-fn-handler-functions, Web MVC>>#
`ServerRequest` and `ServerResponse` are immutable interfaces that offer JDK 8-friendly
access to the HTTP request and response.
@ -440,7 +440,7 @@ See <<core.adoc#validation-beanvalidation, Spring Validation>>.
@@ -440,7 +440,7 @@ See <<core.adoc#validation-beanvalidation, Spring Validation>>.
[[webflux-fn-router-functions]]
== `RouterFunction`
[.small]#<<web.adoc#webmvc-fn-router-functions, Same as in Spring MVC>>#
[.small]#<<web.adoc#webmvc-fn-router-functions, Web MVC>>#
Router functions are used to route the requests to the corresponding `HandlerFunction`.
Typically, you do not write router functions yourself, but rather use a method on the
@ -639,7 +639,7 @@ We can further improve by using the `nest` method together with `accept`:
@@ -639,7 +639,7 @@ We can further improve by using the `nest` method together with `accept`:
[[webflux-fn-running]]
== Running a Server
[.small]#<<web.adoc#webmvc-fn-running, Same as in Spring MVC>>#
[.small]#<<web.adoc#webmvc-fn-running, Web MVC>>#
How do you run a router function in an HTTP server? A simple option is to convert a router
function to an `HttpHandler` by using one of the following:
@ -745,7 +745,7 @@ The following example shows a WebFlux Java configuration (see
@@ -745,7 +745,7 @@ The following example shows a WebFlux Java configuration (see
[[webflux-fn-handler-filter-function]]
== Filtering Handler Functions
[.small]#<<web.adoc#webmvc-fn-handler-filter-function, Same as in Spring MVC>>#
[.small]#<<web.adoc#webmvc-fn-handler-filter-function, Web MVC>>#
You can filter handler functions by using the `before`, `after`, or `filter` methods on the routing
[.small]#<<web.adoc#mvc-view, Same as in Spring MVC>>#
[.small]#<<web.adoc#mvc-view, Web MVC>>#
The use of view technologies in Spring WebFlux is pluggable. Whether you decide to
use Thymeleaf, FreeMarker, or some other view technology is primarily a matter of a
@ -12,7 +12,7 @@ WebFlux. We assume you are already familiar with <<webflux-viewresolution>>.
@@ -12,7 +12,7 @@ WebFlux. We assume you are already familiar with <<webflux-viewresolution>>.
[[webflux-view-thymeleaf]]
== Thymeleaf
[.small]#<<web.adoc#mvc-view-thymeleaf, Same as in Spring MVC>>#
[.small]#<<web.adoc#mvc-view-thymeleaf, Web MVC>>#
Thymeleaf is a modern server-side Java template engine that emphasizes natural HTML
templates that can be previewed in a browser by double-clicking, which is very
[.small]#<<web.adoc#mvc-view-freemarker, Same as in Spring MVC>>#
[.small]#<<web.adoc#mvc-view-freemarker, Web MVC>>#
https://freemarker.apache.org/[Apache FreeMarker] is a template engine for generating any
kind of text output from HTML to email and others. The Spring Framework has built-in
@ -43,7 +43,7 @@ integration for using Spring WebFlux with FreeMarker templates.
@@ -43,7 +43,7 @@ integration for using Spring WebFlux with FreeMarker templates.
[[webflux-view-freemarker-contextconfig]]
=== View Configuration
[.small]#<<web.adoc#mvc-view-freemarker-contextconfig, Same as in Spring MVC>>#
[.small]#<<web.adoc#mvc-view-freemarker-contextconfig, Web MVC>>#
The following example shows how to configure FreeMarker as a view technology:
@ -98,7 +98,7 @@ returns the view name, `welcome`, the resolver looks for the
@@ -98,7 +98,7 @@ returns the view name, `welcome`, the resolver looks for the
[[webflux-views-freemarker]]
=== FreeMarker Configuration
[.small]#<<web.adoc#mvc-views-freemarker, Same as in Spring MVC>>#
[.small]#<<web.adoc#mvc-views-freemarker, Web MVC>>#
You can pass FreeMarker 'Settings' and 'SharedVariables' directly to the FreeMarker
`Configuration` object (which is managed by Spring) by setting the appropriate bean
@ -151,7 +151,7 @@ the `Configuration` object.
@@ -151,7 +151,7 @@ the `Configuration` object.
[[webflux-view-freemarker-forms]]
=== Form Handling
[.small]#<<web.adoc#mvc-view-freemarker-forms, Same as in Spring MVC>>#
[.small]#<<web.adoc#mvc-view-freemarker-forms, Web MVC>>#
Spring provides a tag library for use in JSPs that contains, among others, a
`<spring:bind/>` element. This element primarily lets forms display values from
@ -162,7 +162,7 @@ with additional convenience macros for generating form input elements themselves
@@ -162,7 +162,7 @@ with additional convenience macros for generating form input elements themselves
[[webflux-view-bind-macros]]
==== The Bind Macros
[.small]#<<web.adoc#mvc-view-bind-macros, Same as in Spring MVC>>#
[.small]#<<web.adoc#mvc-view-bind-macros, Web MVC>>#
A standard set of macros are maintained within the `spring-webflux.jar` file for
FreeMarker, so they are always available to a suitably configured application.
@ -193,7 +193,7 @@ sections of the Spring MVC documentation.
@@ -193,7 +193,7 @@ sections of the Spring MVC documentation.
[[webflux-view-script]]
== Script Views
[.small]#<<web.adoc#mvc-view-script, Same as in Spring MVC>>#
[.small]#<<web.adoc#mvc-view-script, Web MVC>>#
The Spring Framework has a built-in integration for using Spring WebFlux with any
templating library that can run on top of the
@ -219,7 +219,7 @@ TIP: The basic rule for integrating any other script engine is that it must impl
@@ -219,7 +219,7 @@ TIP: The basic rule for integrating any other script engine is that it must impl
[[webflux-view-script-dependencies]]
=== Requirements
[.small]#<<web.adoc#mvc-view-script-dependencies, Same as in Spring MVC>>#
[.small]#<<web.adoc#mvc-view-script-dependencies, Web MVC>>#
You need to have the script engine on your classpath, the details of which vary by script engine:
@ -239,7 +239,7 @@ through https://www.webjars.org/[WebJars].
@@ -239,7 +239,7 @@ through https://www.webjars.org/[WebJars].
[[webflux-view-script-integrate]]
=== Script Templates
[.small]#<<web.adoc#mvc-view-script-integrate, Same as in Spring MVC>>#
[.small]#<<web.adoc#mvc-view-script-integrate, Web MVC>>#
You can declare a `ScriptTemplateConfigurer` bean to specify the script engine to use,
the script files to load, what function to call to render templates, and so on.
@ -389,7 +389,7 @@ for more configuration examples.
@@ -389,7 +389,7 @@ for more configuration examples.
[[webflux-view-httpmessagewriter]]
== JSON and XML
[.small]#<<web.adoc#mvc-view-jackson, Same as in Spring MVC>>#
[.small]#<<web.adoc#mvc-view-jackson, Web MVC>>#
For <<webflux-multiple-representations>> purposes, it is useful to be able to alternate
between rendering a model with an HTML template or as other formats (such as JSON or XML),
@ -594,7 +594,7 @@ The `DefaultServerWebExchange` uses the configured `HttpMessageReader` to parse
@@ -594,7 +594,7 @@ The `DefaultServerWebExchange` uses the configured `HttpMessageReader` to parse
[[webflux-multipart]]
==== Multipart Data
[.small]#<<web.adoc#mvc-multipart, Same as in Spring MVC>>#
[.small]#<<web.adoc#mvc-multipart, Web MVC>>#
`ServerWebExchange` exposes the following method for access to multipart data:
@ -626,7 +626,7 @@ content to `Flux<Part>` without collecting to a `MultiValueMap`.
@@ -626,7 +626,7 @@ content to `Flux<Part>` without collecting to a `MultiValueMap`.
[[webflux-forwarded-headers]]
==== Forwarded Headers
[.small]#<<web.adoc#filters-forwarded-headers, Same as in Spring MVC>>#
[.small]#<<web.adoc#filters-forwarded-headers, Web MVC>>#
As a request goes through proxies (such as load balancers), the host, port, and
scheme may change, and that makes it a challenge, from a client perspective, to create links that point to the correct
@ -657,7 +657,7 @@ filters, and `ForwardedHeaderTransformer` is used instead.
@@ -657,7 +657,7 @@ filters, and `ForwardedHeaderTransformer` is used instead.
[[webflux-filters]]
=== Filters
[.small]#<<web.adoc#filters, Same as in Spring MVC>>#
[.small]#<<web.adoc#filters, Web MVC>>#
In the <<webflux-web-handler-api>>, you can use a `WebFilter` to apply interception-style
logic before and after the rest of the processing chain of filters and the target
@ -668,7 +668,7 @@ the bean declaration or by implementing `Ordered`.
@@ -668,7 +668,7 @@ the bean declaration or by implementing `Ordered`.
[[webflux-filters-cors]]
==== CORS
[.small]#<<web.adoc#filters-cors, Same as in Spring MVC>>#
[.small]#<<web.adoc#filters-cors, Web MVC>>#
Spring WebFlux provides fine-grained support for CORS configuration through annotations on
controllers. However, when you use it with Spring Security, we advise relying on the built-in
@ -680,7 +680,7 @@ See the section on <<webflux-cors>> and the <<webflux-cors-webfilter>> for more
@@ -680,7 +680,7 @@ See the section on <<webflux-cors>> and the <<webflux-cors-webfilter>> for more
[[webflux-exception-handler]]
=== Exceptions
[.small]#<<web.adoc#mvc-ann-customer-servlet-container-error-page, Same as in Spring MVC>>#
[.small]#<<web.adoc#mvc-ann-customer-servlet-container-error-page, Web MVC>>#
In the <<webflux-web-handler-api>>, you can use a `WebExceptionHandler` to handle
exceptions from the chain of `WebFilter` instances and the target `WebHandler`. When using the
@ -711,7 +711,7 @@ The following table describes the available `WebExceptionHandler` implementation
@@ -711,7 +711,7 @@ The following table describes the available `WebExceptionHandler` implementation
[[webflux-codecs]]
=== Codecs
[.small]#<<integration.adoc#rest-message-conversion, Same as in Spring MVC>>#
[.small]#<<integration.adoc#rest-message-conversion, Web MVC>>#
The `spring-web` and `spring-core` modules provide support for serializing and
deserializing byte content to and from higher level objects through non-blocking I/O with
@ -847,7 +847,7 @@ To configure all 3 in WebFlux, you'll need to supply a pre-configured instance o
@@ -847,7 +847,7 @@ To configure all 3 in WebFlux, you'll need to supply a pre-configured instance o
[[webflux-codecs-streaming]]
==== Streaming
[.small]#<<web.adoc#mvc-ann-async-http-streaming, Same as in Spring MVC>>#
[.small]#<<web.adoc#mvc-ann-async-http-streaming, Web MVC>>#
When streaming to the HTTP response (for example, `text/event-stream`,
`application/stream+json`), it is important to send data periodically, in order to
@ -875,7 +875,7 @@ especially the section on <<core#databuffers-using, Using DataBuffer>>.
@@ -875,7 +875,7 @@ especially the section on <<core#databuffers-using, Using DataBuffer>>.
[[webflux-logging]]
=== Logging
[.small]#<<web.adoc#mvc-logging, Same as in Spring MVC>>#
[.small]#<<web.adoc#mvc-logging, Web MVC>>#
DEBUG level logging in Spring WebFlux is designed to be compact, minimal, and
human-friendly. It focuses on high value bits of information that are useful over and
@ -907,7 +907,7 @@ while a fully formatted prefix based on that ID is available from
@@ -907,7 +907,7 @@ while a fully formatted prefix based on that ID is available from
[[webflux-logging-sensitive-data]]
==== Sensitive Data
[.small]#<<web.adoc#mvc-logging-sensitive-data, Same as in Spring MVC>>#
[.small]#<<web.adoc#mvc-logging-sensitive-data, Web MVC>>#
`DEBUG` and `TRACE` logging can log sensitive information. This is why form parameters and
headers are masked by default and you must explicitly enable their logging in full.
@ -967,7 +967,7 @@ The following example shows how to do so for client-side requests:
@@ -967,7 +967,7 @@ The following example shows how to do so for client-side requests:
[[webflux-dispatcher-handler]]
== `DispatcherHandler`
[.small]#<<web.adoc#mvc-servlet, Same as in Spring MVC>>#
[.small]#<<web.adoc#mvc-servlet, Web MVC>>#
Spring WebFlux, similarly to Spring MVC, is designed around the front controller pattern,
where a central `WebHandler`, the `DispatcherHandler`, provides a shared algorithm for
@ -1010,7 +1010,7 @@ The resulting `HttpHandler` is ready for use with a <<webflux-httphandler, serve
@@ -1010,7 +1010,7 @@ The resulting `HttpHandler` is ready for use with a <<webflux-httphandler, serve
[[webflux-special-bean-types]]
=== Special Bean Types
[.small]#<<web.adoc#mvc-servlet-special-bean-types, Same as in Spring MVC>>#
[.small]#<<web.adoc#mvc-servlet-special-bean-types, Web MVC>>#
The `DispatcherHandler` delegates to special beans to process requests and render the
appropriate responses. By "`special beans,`" we mean Spring-managed `Object` instances that
@ -1052,7 +1052,7 @@ there are also some other beans detected at a lower level (see
@@ -1052,7 +1052,7 @@ there are also some other beans detected at a lower level (see
[[webflux-framework-config]]
=== WebFlux Config
[.small]#<<web.adoc#mvc-servlet-config, Same as in Spring MVC>>#
[.small]#<<web.adoc#mvc-servlet-config, Web MVC>>#
Applications can declare the infrastructure beans (listed under
<<webflux-web-handler-api-special-beans, Web Handler API>> and
@ -1067,7 +1067,7 @@ many extra convenient options.
@@ -1067,7 +1067,7 @@ many extra convenient options.
[[webflux-dispatcher-handler-sequence]]
=== Processing
[.small]#<<web.adoc#mvc-servlet-sequence, Same as in Spring MVC>>#
[.small]#<<web.adoc#mvc-servlet-sequence, Web MVC>>#
`DispatcherHandler` processes requests as follows:
@ -1118,7 +1118,7 @@ as a `HandlerResult`, along with some additional context, and passed to the firs
@@ -1118,7 +1118,7 @@ as a `HandlerResult`, along with some additional context, and passed to the firs
[[webflux-dispatcher-exceptions]]
=== Exceptions
[.small]#<<web.adoc#mvc-exceptionhandlers, Same as in Spring MVC>>#
[.small]#<<web.adoc#mvc-exceptionhandlers, Web MVC>>#
The `HandlerResult` returned from a `HandlerAdapter` can expose a function for error
handling based on some handler-specific mechanism. This error function is called if:
@ -1141,7 +1141,7 @@ See also <<webflux-ann-controller-exceptions>> in the "`Annotated Controller`" s
@@ -1141,7 +1141,7 @@ See also <<webflux-ann-controller-exceptions>> in the "`Annotated Controller`" s
[[webflux-viewresolution]]
=== View Resolution
[.small]#<<web.adoc#mvc-viewresolver, Same as in Spring MVC>>#
[.small]#<<web.adoc#mvc-viewresolver, Web MVC>>#
View resolution enables rendering to a browser with an HTML template and a model without
tying you to a specific view technology. In Spring WebFlux, view resolution is
@ -1152,7 +1152,7 @@ instance. The `View` is then used to render the response.
@@ -1152,7 +1152,7 @@ instance. The `View` is then used to render the response.
[[webflux-viewresolution-handling]]
==== Handling
[.small]#<<web.adoc#mvc-handling, Same as in Spring MVC>>#
[.small]#<<web.adoc#mvc-handling, Web MVC>>#
The `HandlerResult` passed into `ViewResolutionResultHandler` contains the return value
from the handler and the model that contains attributes added during request
@ -1188,7 +1188,7 @@ See <<webflux-view>> for more on the view technologies integrated with Spring We
@@ -1188,7 +1188,7 @@ See <<webflux-view>> for more on the view technologies integrated with Spring We
[[webflux-redirecting-redirect-prefix]]
==== Redirecting
[.small]#<<web.adoc#mvc-redirecting-redirect-prefix, Same as in Spring MVC>>#
[.small]#<<web.adoc#mvc-redirecting-redirect-prefix, Web MVC>>#
The special `redirect:` prefix in a view name lets you perform a redirect. The
`UrlBasedViewResolver` (and sub-classes) recognize this as an instruction that a
@ -1203,7 +1203,7 @@ operate in terms of logical view names. A view name such as
@@ -1203,7 +1203,7 @@ operate in terms of logical view names. A view name such as
[[webflux-multiple-representations]]
==== Content Negotiation
[.small]#<<web.adoc#mvc-multiple-representations, Same as in Spring MVC>>#
[.small]#<<web.adoc#mvc-multiple-representations, Web MVC>>#
`ViewResolutionResultHandler` supports content negotiation. It compares the request
media types with the media types supported by each selected `View`. The first `View`
@ -1220,7 +1220,7 @@ always selected and used if they match the requested media type.
@@ -1220,7 +1220,7 @@ always selected and used if they match the requested media type.
[[webflux-controller]]
== Annotated Controllers
[.small]#<<web.adoc#mvc-controller, Same as in Spring MVC>>#
[.small]#<<web.adoc#mvc-controller, Web MVC>>#
Spring WebFlux provides an annotation-based programming model, where `@Controller` and
`@RestController` components use annotations to express request mappings, request input,
@ -1258,7 +1258,7 @@ In the preceding example, the method returns a `String` to be written to the res
@@ -1258,7 +1258,7 @@ In the preceding example, the method returns a `String` to be written to the res
[[webflux-ann-controller]]
=== `@Controller`
[.small]#<<web.adoc#mvc-ann-controller, Same as in Spring MVC>>#
[.small]#<<web.adoc#mvc-ann-controller, Web MVC>>#
You can define controller beans by using a standard Spring bean definition.
The `@Controller` stereotype allows for auto-detection and is aligned with Spring general support
@ -1302,7 +1302,7 @@ directly to the response body versus view resolution and rendering with an HTML
@@ -1302,7 +1302,7 @@ directly to the response body versus view resolution and rendering with an HTML
[[webflux-ann-requestmapping]]
=== Request Mapping
[.small]#<<web.adoc#mvc-ann-requestmapping, Same as in Spring MVC>>#
[.small]#<<web.adoc#mvc-ann-requestmapping, Web MVC>>#
The `@RequestMapping` annotation is used to map requests to controllers methods. It has
various attributes to match by URL, HTTP method, request parameters, headers, and media
@ -1366,7 +1366,7 @@ The following example uses type and method level mappings:
@@ -1366,7 +1366,7 @@ The following example uses type and method level mappings:
[[webflux-ann-requestmapping-uri-templates]]
==== URI Patterns
[.small]#<<web.adoc#mvc-ann-requestmapping-uri-templates, Same as in Spring MVC>>#
[.small]#<<web.adoc#mvc-ann-requestmapping-uri-templates, Web MVC>>#
You can map requests by using glob patterns and wildcards:
@ -1480,7 +1480,7 @@ explicit, and less vulnerable to URL path based exploits.
@@ -1480,7 +1480,7 @@ explicit, and less vulnerable to URL path based exploits.
[[webflux-ann-requestmapping-pattern-comparison]]
==== Pattern Comparison
[.small]#<<web.adoc#mvc-ann-requestmapping-pattern-comparison, Same as in Spring MVC>>#
[.small]#<<web.adoc#mvc-ann-requestmapping-pattern-comparison, Web MVC>>#
When multiple patterns match a URL, they must be compared to find the best match. This is done
with `PathPattern.SPECIFICITY_COMPARATOR`, which looks for patterns that are more specific.
@ -1495,7 +1495,7 @@ sorted last instead. If two patterns are both catch-all, the longer is chosen.
@@ -1495,7 +1495,7 @@ sorted last instead. If two patterns are both catch-all, the longer is chosen.
[[webflux-ann-requestmapping-consumes]]
==== Consumable Media Types
[.small]#<<web.adoc#mvc-ann-requestmapping-consumes, Same as in Spring MVC>>#
[.small]#<<web.adoc#mvc-ann-requestmapping-consumes, Web MVC>>#
You can narrow the request mapping based on the `Content-Type` of the request,
as the following example shows:
@ -1530,7 +1530,7 @@ TIP: `MediaType` provides constants for commonly used media types -- for example
@@ -1530,7 +1530,7 @@ TIP: `MediaType` provides constants for commonly used media types -- for example
[[webflux-ann-requestmapping-produces]]
==== Producible Media Types
[.small]#<<web.adoc#mvc-ann-requestmapping-produces, Same as in Spring MVC>>#
[.small]#<<web.adoc#mvc-ann-requestmapping-produces, Web MVC>>#
You can narrow the request mapping based on the `Accept` request header and the list of
content types that a controller method produces, as the following example shows:
@ -1567,7 +1567,7 @@ TIP: `MediaType` provides constants for commonly used media types -- e.g.
@@ -1567,7 +1567,7 @@ TIP: `MediaType` provides constants for commonly used media types -- e.g.
[[webflux-ann-requestmapping-params-and-headers]]
==== Parameters and Headers
[.small]#<<web.adoc#mvc-ann-requestmapping-params-and-headers, Same as in Spring MVC>>#
[.small]#<<web.adoc#mvc-ann-requestmapping-params-and-headers, Web MVC>>#
You can narrow request mappings based on query parameter conditions. You can test for the
presence of a query parameter (`myParam`), for its absence (`!myParam`), or for a
@ -1619,7 +1619,7 @@ You can also use the same with request header conditions, as the follwing exampl
@@ -1619,7 +1619,7 @@ You can also use the same with request header conditions, as the follwing exampl
[[webflux-ann-requestmapping-head-options]]
==== HTTP HEAD, OPTIONS
[.small]#<<web.adoc#mvc-ann-requestmapping-head-options, Same as in Spring MVC>>#
[.small]#<<web.adoc#mvc-ann-requestmapping-head-options, Web MVC>>#
`@GetMapping` and `@RequestMapping(method=HttpMethod.GET)` support HTTP HEAD
transparently for request mapping purposes. Controller methods need not change.
@ -1640,7 +1640,7 @@ is not necessary in the common case.
@@ -1640,7 +1640,7 @@ is not necessary in the common case.
[[webflux-ann-requestmapping-composed]]
==== Custom Annotations
[.small]#<<web.adoc#mvc-ann-requestmapping-composed, Same as in Spring MVC>>#
[.small]#<<web.adoc#mvc-ann-requestmapping-composed, Web MVC>>#
Spring WebFlux supports the use of <<core.adoc#beans-meta-annotations, composed annotations>>
for request mapping. Those are annotations that are themselves meta-annotated with
@ -1661,7 +1661,7 @@ you can check the custom attribute and return your own `RequestCondition`.
@@ -1661,7 +1661,7 @@ you can check the custom attribute and return your own `RequestCondition`.
[[webflux-ann-requestmapping-registration]]
==== Explicit Registrations
[.small]#<<web.adoc#mvc-ann-requestmapping-registration, Same as in Spring MVC>>#
[.small]#<<web.adoc#mvc-ann-requestmapping-registration, Web MVC>>#
You can programmatically register Handler methods, which can be used for dynamic
registrations or for advanced cases, such as different instances of the same handler
@ -1718,7 +1718,7 @@ under different URLs. The following example shows how to do so:
@@ -1718,7 +1718,7 @@ under different URLs. The following example shows how to do so:
[[webflux-ann-methods]]
=== Handler Methods
[.small]#<<web.adoc#mvc-ann-methods, Same as in Spring MVC>>#
[.small]#<<web.adoc#mvc-ann-methods, Web MVC>>#
`@RequestMapping` handler methods have a flexible signature and can choose from a range of
supported controller method arguments and return values.
[.small]#<<web.adoc#mvc-ann-arguments, Same as in Spring MVC>>#
[.small]#<<web.adoc#mvc-ann-arguments, Web MVC>>#
The following table shows the supported controller method arguments.
@ -1847,7 +1847,7 @@ and others) and is equivalent to `required=false`.
@@ -1847,7 +1847,7 @@ and others) and is equivalent to `required=false`.
[[webflux-ann-return-types]]
==== Return Values
[.small]#<<web.adoc#mvc-ann-return-types, Same as in Spring MVC>>#
[.small]#<<web.adoc#mvc-ann-return-types, Web MVC>>#
The following table shows the supported controller method return values. Note that reactive
types from libraries such as Reactor, RxJava, <<webflux-reactive-libraries, or other>> are
@ -1921,7 +1921,7 @@ generally supported for all return values.
@@ -1921,7 +1921,7 @@ generally supported for all return values.
[[webflux-ann-typeconversion]]
==== Type Conversion
[.small]#<<web.adoc#mvc-ann-typeconversion, Same as in Spring MVC>>#
[.small]#<<web.adoc#mvc-ann-typeconversion, Web MVC>>#
Some annotated controller method arguments that represent String-based request input (for example,
`@RequestParam`, `@RequestHeader`, `@PathVariable`, `@MatrixVariable`, and `@CookieValue`)
@ -1935,7 +1935,7 @@ can be customized through a `WebDataBinder` (see <<mvc-ann-initbinder>>) or by r
@@ -1935,7 +1935,7 @@ can be customized through a `WebDataBinder` (see <<mvc-ann-initbinder>>) or by r
[[webflux-ann-matrix-variables]]
==== Matrix Variables
[.small]#<<web.adoc#mvc-ann-matrix-variables, Same as in Spring MVC>>#
[.small]#<<web.adoc#mvc-ann-matrix-variables, Web MVC>>#
https://tools.ietf.org/html/rfc3986#section-3.3[RFC 3986] discusses name-value pairs in
path segments. In Spring WebFlux, we refer to those as "`matrix variables`" based on an
@ -2070,7 +2070,7 @@ To get all matrix variables, use a `MultiValueMap`, as the following example sho
@@ -2070,7 +2070,7 @@ To get all matrix variables, use a `MultiValueMap`, as the following example sho
[[webflux-ann-requestparam]]
==== `@RequestParam`
[.small]#<<web.adoc#mvc-ann-requestparam, Same as in Spring MVC>>#
[.small]#<<web.adoc#mvc-ann-requestparam, Web MVC>>#
You can use the `@RequestParam` annotation to bind query parameters to a method argument in a
controller. The following code snippet shows the usage:
@ -2145,7 +2145,7 @@ with `@RequestParam`.
@@ -2145,7 +2145,7 @@ with `@RequestParam`.
[[webflux-ann-requestheader]]
==== `@RequestHeader`
[.small]#<<web.adoc#mvc-ann-requestheader, Same as in Spring MVC>>#
[.small]#<<web.adoc#mvc-ann-requestheader, Web MVC>>#
You can use the `@RequestHeader` annotation to bind a request header to a method argument in a
controller.
@ -2206,7 +2206,7 @@ example, a method parameter annotated with `@RequestHeader("Accept")` may be of
@@ -2206,7 +2206,7 @@ example, a method parameter annotated with `@RequestHeader("Accept")` may be of
[[webflux-ann-cookievalue]]
==== `@CookieValue`
[.small]#<<web.adoc#mvc-ann-cookievalue, Same as in Spring MVC>>#
[.small]#<<web.adoc#mvc-ann-cookievalue, Web MVC>>#
You can use the `@CookieValue` annotation to bind the value of an HTTP cookie to a method argument
in a controller.
@ -2247,7 +2247,7 @@ Type conversion is applied automatically if the target method parameter type is
@@ -2247,7 +2247,7 @@ Type conversion is applied automatically if the target method parameter type is
[[webflux-ann-modelattrib-method-args]]
==== `@ModelAttribute`
[.small]#<<web.adoc#mvc-ann-modelattrib-method-args, Same as in Spring MVC>>#
[.small]#<<web.adoc#mvc-ann-modelattrib-method-args, Web MVC>>#
You can use the `@ModelAttribute` annotation on a method argument to access an attribute from the
model or have it instantiated if not present. The model attribute is also overlain with
@ -2394,7 +2394,7 @@ with `@ModelAttribute`.
@@ -2394,7 +2394,7 @@ with `@ModelAttribute`.
[[webflux-ann-sessionattributes]]
==== `@SessionAttributes`
[.small]#<<web.adoc#mvc-ann-sessionattributes, Same as in Spring MVC>>#
[.small]#<<web.adoc#mvc-ann-sessionattributes, Web MVC>>#
`@SessionAttributes` is used to store model attributes in the `WebSession` between
requests. It is a type-level annotation that declares session attributes used by a
@ -2479,7 +2479,7 @@ as the following example shows:
@@ -2479,7 +2479,7 @@ as the following example shows:
[[webflux-ann-sessionattribute]]
==== `@SessionAttribute`
[.small]#<<web.adoc#mvc-ann-sessionattribute, Same as in Spring MVC>>#
[.small]#<<web.adoc#mvc-ann-sessionattribute, Web MVC>>#
If you need access to pre-existing session attributes that are managed globally
(that is, outside the controller -- for example, by a filter) and may or may not be present,
@ -2515,7 +2515,7 @@ workflow, consider using `SessionAttributes`, as described in
@@ -2515,7 +2515,7 @@ workflow, consider using `SessionAttributes`, as described in
[[webflux-ann-requestattrib]]
==== `@RequestAttribute`
[.small]#<<web.adoc#mvc-ann-requestattrib, Same as in Spring MVC>>#
[.small]#<<web.adoc#mvc-ann-requestattrib, Web MVC>>#
Similarly to `@SessionAttribute`, you can use the `@RequestAttribute` annotation to
access pre-existing request attributes created earlier (for example, by a `WebFilter`),
@ -2544,7 +2544,7 @@ as the following example shows:
@@ -2544,7 +2544,7 @@ as the following example shows:
[[webflux-multipart-forms]]
==== Multipart Content
[.small]#<<web.adoc#mvc-multipart-forms, Same as in Spring MVC>>#
[.small]#<<web.adoc#mvc-multipart-forms, Web MVC>>#
As explained in <<webflux-multipart>>, `ServerWebExchange` provides access to multipart
content. The best way to handle a file upload form (for example, from a browser) in a controller
@ -2745,7 +2745,7 @@ To access multipart data sequentially, in streaming fashion, you can use `@Reque
@@ -2745,7 +2745,7 @@ To access multipart data sequentially, in streaming fashion, you can use `@Reque
[[webflux-ann-requestbody]]
==== `@RequestBody`
[.small]#<<web.adoc#mvc-ann-requestbody, Same as in Spring MVC>>#
[.small]#<<web.adoc#mvc-ann-requestbody, Web MVC>>#
You can use the `@RequestBody` annotation to have the request body read and deserialized into an
`Object` through an <<webflux-codecs,HttpMessageReader>>.
@ -2819,7 +2819,7 @@ example uses a `BindingResult` argument`:
@@ -2819,7 +2819,7 @@ example uses a `BindingResult` argument`:
[[webflux-ann-httpentity]]
==== `HttpEntity`
[.small]#<<web.adoc#mvc-ann-httpentity, Same as in Spring MVC>>#
[.small]#<<web.adoc#mvc-ann-httpentity, Web MVC>>#
`HttpEntity` is more or less identical to using <<webflux-ann-requestbody>> but is based on a
container object that exposes request headers and the body. The following example uses an
@ -2845,7 +2845,7 @@ container object that exposes request headers and the body. The following exampl
@@ -2845,7 +2845,7 @@ container object that exposes request headers and the body. The following exampl
[[webflux-ann-responsebody]]
==== `@ResponseBody`
[.small]#<<web.adoc#mvc-ann-responsebody, Same as in Spring MVC>>#
[.small]#<<web.adoc#mvc-ann-responsebody, Web MVC>>#
You can use the `@ResponseBody` annotation on a method to have the return serialized
to the response body through an <<webflux-codecs, HttpMessageWriter>>. The following
@ -2888,7 +2888,7 @@ configure or customize message writing.
@@ -2888,7 +2888,7 @@ configure or customize message writing.
[[webflux-ann-responseentity]]
==== `ResponseEntity`
[.small]#<<web.adoc#mvc-ann-responseentity, Same as in Spring MVC>>#
[.small]#<<web.adoc#mvc-ann-responseentity, Web MVC>>#
`ResponseEntity` is like <<webflux-ann-responsebody>> but with status and headers. For example:
@ -2925,7 +2925,7 @@ Spring offers support for the Jackson JSON library.
@@ -2925,7 +2925,7 @@ Spring offers support for the Jackson JSON library.
[[webflux-ann-jsonview]]
===== JSON Views
[.small]#<<web.adoc#mvc-ann-jackson, Same as in Spring MVC>>#
@ -3003,7 +3003,7 @@ controller method. Use a composite interface if you need to activate multiple vi
@@ -3003,7 +3003,7 @@ controller method. Use a composite interface if you need to activate multiple vi
[[webflux-ann-modelattrib-methods]]
=== `Model`
[.small]#<<web.adoc#mvc-ann-modelattrib-methods, Same as in Spring MVC>>#
[.small]#<<web.adoc#mvc-ann-modelattrib-methods, Web MVC>>#
You can use the `@ModelAttribute` annotation:
@ -3142,7 +3142,7 @@ as the following example shows:
@@ -3142,7 +3142,7 @@ as the following example shows:
[[webflux-ann-initbinder]]
=== `DataBinder`
[.small]#<<web.adoc#mvc-ann-initbinder, Same as in Spring MVC>>#
[.small]#<<web.adoc#mvc-ann-initbinder, Web MVC>>#
`@Controller` or `@ControllerAdvice` classes can have `@InitBinder` methods, to
initialize instances of `WebDataBinder`. Those, in turn, are used to:
@ -3237,7 +3237,7 @@ controller-specific `Formatter` instances, as the following example shows:
@@ -3237,7 +3237,7 @@ controller-specific `Formatter` instances, as the following example shows:
[[webflux-ann-controller-exceptions]]
=== Managing Exceptions
[.small]#<<web.adoc#mvc-ann-exceptionhandler, Same as in Spring MVC>>#
[.small]#<<web.adoc#mvc-ann-exceptionhandler, Web MVC>>#
`@Controller` and <<mvc-ann-controller-advice, @ControllerAdvice>> classes can have
`@ExceptionHandler` methods to handle exceptions from controller methods. The following
@ -3298,7 +3298,7 @@ for more detail.
@@ -3298,7 +3298,7 @@ for more detail.
[[webflux-ann-rest-exceptions]]
==== REST API exceptions
[.small]#<<web.adoc#mvc-ann-rest-exceptions, Same as in Spring MVC>>#
[.small]#<<web.adoc#mvc-ann-rest-exceptions, Web MVC>>#
A common requirement for REST services is to include error details in the body of the
response. The Spring Framework does not automatically do so, because the representation
@ -3316,7 +3316,7 @@ an HTTP status code.
@@ -3316,7 +3316,7 @@ an HTTP status code.
[[webflux-ann-controller-advice]]
=== Controller Advice
[.small]#<<web.adoc#mvc-ann-controller-advice, Same as in Spring MVC>>#
[.small]#<<web.adoc#mvc-ann-controller-advice, Web MVC>>#
Typically, the `@ExceptionHandler`, `@InitBinder`, and `@ModelAttribute` methods apply
within the `@Controller` class (or class hierarchy) in which they are declared. If you
[.small]#<<web.adoc#mvc-caching, Same as in Spring MVC>>#
[.small]#<<web.adoc#mvc-caching, Web MVC>>#
HTTP caching can significantly improve the performance of a web application. HTTP caching
revolves around the `Cache-Control` response header and subsequent conditional request
@ -3431,7 +3431,7 @@ This section describes the HTTP caching related options available in Spring WebF
@@ -3431,7 +3431,7 @@ This section describes the HTTP caching related options available in Spring WebF
[[webflux-caching-cachecontrol]]
=== `CacheControl`
[.small]#<<web.adoc#mvc-caching-cachecontrol, Same as in Spring MVC>>#
[.small]#<<web.adoc#mvc-caching-cachecontrol, Web MVC>>#
{api-spring-framework}/http/CacheControl.html[`CacheControl`] provides support for
configuring settings related to the `Cache-Control` header and is accepted as an argument
@ -3479,7 +3479,7 @@ use case-oriented approach that focuses on the common scenarios, as the followin
@@ -3479,7 +3479,7 @@ use case-oriented approach that focuses on the common scenarios, as the followin
[[webflux-caching-etag-lastmodified]]
=== Controllers
[.small]#<<web.adoc#mvc-caching-etag-lastmodified, Same as in Spring MVC>>#
[.small]#<<web.adoc#mvc-caching-etag-lastmodified, Web MVC>>#
Controllers can add explicit support for HTTP caching. We recommend doing so, since the
`lastModified` or `ETag` value for a resource needs to be calculated before it can be compared
@ -3576,7 +3576,7 @@ to 409 (PRECONDITION_FAILED) to prevent concurrent modification.
@@ -3576,7 +3576,7 @@ to 409 (PRECONDITION_FAILED) to prevent concurrent modification.
[[webflux-caching-static-resources]]
=== Static Resources
[.small]#<<web.adoc#mvc-caching-static-resources, Same as in Spring MVC>>#
[.small]#<<web.adoc#mvc-caching-static-resources, Web MVC>>#
You should serve static resources with a `Cache-Control` and conditional response headers
for optimal performance. See the section on configuring <<webflux-config-static-resources>>.
@ -3586,7 +3586,7 @@ for optimal performance. See the section on configuring <<webflux-config-static-
@@ -3586,7 +3586,7 @@ for optimal performance. See the section on configuring <<webflux-config-static-
[[webflux-config]]
== WebFlux Config
[.small]#<<web.adoc#mvc-config, Same as in Spring MVC>>#
[.small]#<<web.adoc#mvc-config, Web MVC>>#
The WebFlux Java configuration declares the components that are required to process
requests with annotated controllers or functional endpoints, and it offers an API to
@ -3603,7 +3603,7 @@ gain full control over the configuration through the
@@ -3603,7 +3603,7 @@ gain full control over the configuration through the
[[webflux-config-enable]]
=== Enabling WebFlux Config
[.small]#<<web.adoc#mvc-config-enable, Same as in Spring MVC>>#
[.small]#<<web.adoc#mvc-config-enable, Web MVC>>#
You can use the `@EnableWebFlux` annotation in your Java config, as the following example shows:
@ -3632,7 +3632,7 @@ available on the classpath -- for JSON, XML, and others.
@@ -3632,7 +3632,7 @@ available on the classpath -- for JSON, XML, and others.
[[webflux-config-customize]]
=== WebFlux config API
[.small]#<<web.adoc#mvc-config-customize, Same as in Spring MVC>>#
[.small]#<<web.adoc#mvc-config-customize, Web MVC>>#
In your Java configuration, you can implement the `WebFluxConfigurer` interface,
as the following example shows:
@ -3663,7 +3663,7 @@ class WebConfig : WebFluxConfigurer {
@@ -3663,7 +3663,7 @@ class WebConfig : WebFluxConfigurer {
[[webflux-config-conversion]]
=== Conversion, formatting
[.small]#<<web.adoc#mvc-config-conversion, Same as in Spring MVC>>#
[.small]#<<web.adoc#mvc-config-conversion, Web MVC>>#
By default, formatters for `Number` and `Date` types are installed, including support for
the `@NumberFormat` and `@DateTimeFormat` annotations. Full support for the Joda-Time
@ -3706,7 +3706,7 @@ use `FormatterRegistrar` implementations.
@@ -3706,7 +3706,7 @@ use `FormatterRegistrar` implementations.
[[webflux-config-validation]]
=== Validation
[.small]#<<web.adoc#mvc-config-validation, Same as in Spring MVC>>#
[.small]#<<web.adoc#mvc-config-validation, Web MVC>>#
By default, if <<core.adoc#validation-beanvalidation-overview, Bean Validation>> is present
on the classpath (for example, the Hibernate Validator), the `LocalValidatorFactoryBean`
@ -3781,7 +3781,7 @@ mark it with `@Primary` in order to avoid conflict with the one declared in the
@@ -3781,7 +3781,7 @@ mark it with `@Primary` in order to avoid conflict with the one declared in the
[[webflux-config-content-negotiation]]
=== Content Type Resolvers
[.small]#<<web.adoc#mvc-config-content-negotiation, Same as in Spring MVC>>#
[.small]#<<web.adoc#mvc-config-content-negotiation, Web MVC>>#
You can configure how Spring WebFlux determines the requested media types for
`@Controller` instances from the request. By default, only the `Accept` header is checked,
@ -3819,7 +3819,7 @@ The following example shows how to customize the requested content type resoluti
@@ -3819,7 +3819,7 @@ The following example shows how to customize the requested content type resoluti
[[webflux-config-message-codecs]]
=== HTTP message codecs
[.small]#<<web.adoc#mvc-config-message-converters, Same as in Spring MVC>>#
[.small]#<<web.adoc#mvc-config-message-converters, Web MVC>>#
The following example shows how to customize how the request and response body are read and written:
@ -3870,7 +3870,7 @@ It also automatically registers the following well-known modules if they are det
@@ -3870,7 +3870,7 @@ It also automatically registers the following well-known modules if they are det
[[webflux-config-view-resolvers]]
=== View Resolvers
[.small]#<<web.adoc#mvc-config-view-resolvers, Same as in Spring MVC>>#
[.small]#<<web.adoc#mvc-config-view-resolvers, Web MVC>>#
The following example shows how to configure view resolution:
@ -4027,7 +4027,7 @@ See <<webflux-view>> for more on the view technologies that are integrated with
@@ -4027,7 +4027,7 @@ See <<webflux-view>> for more on the view technologies that are integrated with
[[webflux-config-static-resources]]
=== Static Resources
[.small]#<<web.adoc#mvc-config-static-resources, Same as in Spring MVC>>#
[.small]#<<web.adoc#mvc-config-static-resources, Web MVC>>#
This option provides a convenient way to serve static resources from a list of
@ -4142,7 +4142,7 @@ without versions -- for example, from `/jquery/jquery.min.js` to
@@ -4142,7 +4142,7 @@ without versions -- for example, from `/jquery/jquery.min.js` to
[[webflux-config-path-matching]]
=== Path Matching
[.small]#<<web.adoc#mvc-config-path-matching, Same as in Spring MVC>>#
[.small]#<<web.adoc#mvc-config-path-matching, Web MVC>>#
You can customize options related to path matching. For details on the individual options, see the
The CORS specification distinguishes between preflight, simple, and actual requests.
To learn how CORS works, you can read
@ -77,7 +77,7 @@ To learn more from the source or make advanced customizations, check the code be
@@ -77,7 +77,7 @@ To learn more from the source or make advanced customizations, check the code be
[[mvc-cors-controller]]
== `@CrossOrigin`
[.small]#<<web-reactive.adoc#webflux-cors-controller, Same as in Spring WebFlux>>#
In WebMvc.fn, an HTTP request is handled with a `HandlerFunction`: a function that takes
`ServerRequest` and returns a `ServerResponse`.
@ -110,7 +110,7 @@ If you register the `RouterFunction` as a bean, for instance by exposing it in a
@@ -110,7 +110,7 @@ If you register the `RouterFunction` as a bean, for instance by exposing it in a
[[webmvc-fn-handler-functions]]
== HandlerFunction
[.small]#<<web-reactive.adoc#webflux-fn-handler-functions, Same as in Spring WebFlux>>#
`ServerRequest` and `ServerResponse` are immutable interfaces that offer JDK 8-friendly
access to the HTTP request and response, including headers, body, method, and status code.
@ -375,7 +375,7 @@ See <<core.adoc#validation-beanvalidation, Spring Validation>>.
@@ -375,7 +375,7 @@ See <<core.adoc#validation-beanvalidation, Spring Validation>>.
[[webmvc-fn-router-functions]]
== `RouterFunction`
[.small]#<<web-reactive.adoc#webflux-fn-router-functions, Same as in Spring WebFlux>>#
Router functions are used to route the requests to the corresponding `HandlerFunction`.
Typically, you do not write router functions yourself, but rather use a method on the
@ -581,7 +581,7 @@ We can further improve by using the `nest` method together with `accept`:
@@ -581,7 +581,7 @@ We can further improve by using the `nest` method together with `accept`:
[[webmvc-fn-running]]
== Running a Server
[.small]#<<web-reactive.adoc#webflux-fn-running, Same as in Spring WebFlux>>#
You typically run router functions in a <<web.adoc#mvc-servlet, `DispatcherHandler`>>-based setup through the
<<web.adoc#mvc-config>>, which uses Spring configuration to declare the
@ -674,7 +674,7 @@ The following example shows a WebFlux Java configuration:
@@ -674,7 +674,7 @@ The following example shows a WebFlux Java configuration:
[[webmvc-fn-handler-filter-function]]
== Filtering Handler Functions
[.small]#<<web-reactive.adoc#webflux-fn-handler-filter-function, Same as in Spring WebFlux>>#
The use of view technologies in Spring MVC is pluggable, whether you decide to use
Thymeleaf, Groovy Markup Templates, JSPs, or other technologies, is primarily a matter
@ -12,7 +12,7 @@ Spring MVC. We assume you are already familiar with <<mvc-viewresolver>>.
@@ -12,7 +12,7 @@ Spring MVC. We assume you are already familiar with <<mvc-viewresolver>>.
[[mvc-view-thymeleaf]]
== Thymeleaf
[.small]#<<web-reactive.adoc#webflux-view-thymeleaf, Same as in Spring WebFlux>>#
Thymeleaf is a modern server-side Java template engine that emphasizes natural HTML
templates that can be previewed in a browser by double-clicking, which is very helpful
@ -32,7 +32,7 @@ See https://www.thymeleaf.org/documentation.html[Thymeleaf+Spring] for more deta
@@ -32,7 +32,7 @@ See https://www.thymeleaf.org/documentation.html[Thymeleaf+Spring] for more deta
[[mvc-view-freemarker]]
== FreeMarker
[.small]#<<web-reactive.adoc#webflux-view-freemarker, Same as in Spring WebFlux>>#
https://freemarker.apache.org/[Apache FreeMarker] is a template engine for generating any
kind of text output from HTML to email and others. The Spring Framework has built-in
@ -42,7 +42,7 @@ integration for using Spring MVC with FreeMarker templates.
@@ -42,7 +42,7 @@ integration for using Spring MVC with FreeMarker templates.
[[mvc-view-freemarker-contextconfig]]
=== View Configuration
[.small]#<<web-reactive.adoc#webflux-view-freemarker-contextconfig, Same as in Spring WebFlux>>#
The following example shows how to configure FreeMarker as a view technology:
@ -123,7 +123,7 @@ returns a view name of `welcome`, the resolver looks for the
@@ -123,7 +123,7 @@ returns a view name of `welcome`, the resolver looks for the
[[mvc-views-freemarker]]
=== FreeMarker Configuration
[.small]#<<web-reactive.adoc#webflux-views-freemarker, Same as in Spring WebFlux>>#
You can pass FreeMarker 'Settings' and 'SharedVariables' directly to the FreeMarker
`Configuration` object (which is managed by Spring) by setting the appropriate bean
@ -162,7 +162,7 @@ with additional convenience macros for generating form input elements themselves
@@ -162,7 +162,7 @@ with additional convenience macros for generating form input elements themselves
[[mvc-view-bind-macros]]
==== The Bind Macros
[.small]#<<web-reactive.adoc#webflux-view-bind-macros, Same as in Spring WebFlux>>#
A standard set of macros are maintained within the `spring-webmvc.jar` file for
FreeMarker, so they are always available to a suitably configured application.
@ -574,7 +574,7 @@ syntax. The following example shows a sample template for an HTML page:
@@ -574,7 +574,7 @@ syntax. The following example shows a sample template for an HTML page:
[[mvc-view-script]]
== Script Views
[.small]#<<web-reactive.adoc#webflux-view-script, Same as in Spring WebFlux>>#
The Spring Framework has a built-in integration for using Spring MVC with any
templating library that can run on top of the
@ -600,7 +600,7 @@ TIP: The basic rule for integrating any other script engine is that it must impl
@@ -600,7 +600,7 @@ TIP: The basic rule for integrating any other script engine is that it must impl
[[mvc-view-script-dependencies]]
=== Requirements
[.small]#<<web-reactive.adoc#webflux-view-script-dependencies, Same as in Spring WebFlux>>#
You can declare a `ScriptTemplateConfigurer` bean to specify the script engine to use,
the script files to load, what function to call to render templates, and so on.
@ -2020,7 +2020,7 @@ an external definition (by name) or as a `View` instance from the handler method
@@ -2020,7 +2020,7 @@ an external definition (by name) or as a `View` instance from the handler method
[[mvc-view-jackson]]
== Jackson
[.small]#<<web-reactive.adoc#webflux-view-httpmessagewriter, Same as in Spring WebFlux>>#
Spring MVC, as many other web frameworks, is designed around the front controller
pattern where a central `Servlet`, the `DispatcherServlet`, provides a shared algorithm
@ -237,7 +237,7 @@ TIP: If an application context hierarchy is not required, applications may confi
@@ -237,7 +237,7 @@ TIP: If an application context hierarchy is not required, applications may confi
[[mvc-servlet-special-bean-types]]
=== Special Bean Types
[.small]#<<web-reactive.adoc#webflux-special-bean-types, Same as in Spring WebFlux>>#
The `DispatcherServlet` delegates to special beans to process requests and render the
appropriate responses. By "`special beans`" we mean Spring-managed `Object` instances that
@ -297,7 +297,7 @@ The following table lists the special beans detected by the `DispatcherServlet`:
@@ -297,7 +297,7 @@ The following table lists the special beans detected by the `DispatcherServlet`:
[[mvc-servlet-config]]
=== Web MVC Config
[.small]#<<web-reactive.adoc#webflux-framework-config, Same as in Spring WebFlux>>#
The `DispatcherServlet` processes requests as follows:
@ -610,7 +610,7 @@ declare it as an <<mvc-ann-controller-advice>> bean or configure it directly on
@@ -610,7 +610,7 @@ declare it as an <<mvc-ann-controller-advice>> bean or configure it directly on
[[mvc-exceptionhandlers]]
=== Exceptions
[.small]#<<web-reactive.adoc#webflux-dispatcher-exceptions, Same as in Spring WebFlux>>#
If an exception occurs during request mapping or is thrown from a request handler (such as
a `@Controller`), the `DispatcherServlet` delegates to a chain of `HandlerExceptionResolver`
@ -721,7 +721,7 @@ however, use both a `WebApplicationInitializer` and a minimal `web.xml`.
@@ -721,7 +721,7 @@ however, use both a `WebApplicationInitializer` and a minimal `web.xml`.
[[mvc-viewresolver]]
=== View Resolution
[.small]#<<web-reactive.adoc#webflux-viewresolution, Same as in Spring WebFlux>>#
Spring MVC defines the `ViewResolver` and `View` interfaces that let you render
models in a browser without tying you to a specific view technology. `ViewResolver`
@ -779,7 +779,7 @@ The following table provides more details on the `ViewResolver` hierarchy:
@@ -779,7 +779,7 @@ The following table provides more details on the `ViewResolver` hierarchy:
[[mvc-viewresolver-handling]]
==== Handling
[.small]#<<web-reactive.adoc#webflux-viewresolution-handling, Same as in Spring WebFlux>>#
The special `redirect:` prefix in a view name lets you perform a redirect. The
`UrlBasedViewResolver` (and its subclasses) recognize this as an instruction that a
@ -830,7 +830,7 @@ Servlet/JSP engine. Note that you may also chain multiple view resolvers, instea
@@ -830,7 +830,7 @@ Servlet/JSP engine. Note that you may also chain multiple view resolvers, instea
[[mvc-multiple-representations]]
==== Content Negotiation
[.small]#<<web-reactive.adoc#webflux-multiple-representations, Same as in Spring WebFlux>>#
`MultipartResolver` from the `org.springframework.web.multipart` package is a strategy
for parsing multipart requests including file uploads. There is one implementation
@ -1162,7 +1162,7 @@ Once the Servlet 3.0 configuration is in place, you can add a bean of type
@@ -1162,7 +1162,7 @@ Once the Servlet 3.0 configuration is in place, you can add a bean of type
[[mvc-logging]]
=== Logging
[.small]#<<web-reactive.adoc#webflux-logging, Same as in Spring WebFlux>>#
The `spring-web` module provides some useful filters:
@ -1269,7 +1269,7 @@ available through the `ServletRequest.getParameter{asterisk}()` family of method
@@ -1269,7 +1269,7 @@ available through the `ServletRequest.getParameter{asterisk}()` family of method
[[filters-forwarded-headers]]
=== Forwarded Headers
[.small]#<<web-reactive.adoc#webflux-forwarded-headers, Same as in Spring WebFlux>>#
As a request goes through proxies (such as load balancers) the host, port, and
scheme may change, and that makes it a challenge to create links that point to the correct
@ -1330,7 +1330,7 @@ the filter via `web.xml` or in Spring Boot via a `FilterRegistrationBean` be sur
@@ -1330,7 +1330,7 @@ the filter via `web.xml` or in Spring Boot via a `FilterRegistrationBean` be sur
[[filters-cors]]
=== CORS
[.small]#<<web-reactive.adoc#webflux-filters-cors, Same as in Spring WebFlux>>#
Spring MVC provides fine-grained support for CORS configuration through annotations on
controllers. However, when used with Spring Security, we advise relying on the built-in
@ -1343,7 +1343,7 @@ See the sections on <<mvc-cors>> and the <<mvc-cors-filter>> for more details.
@@ -1343,7 +1343,7 @@ See the sections on <<mvc-cors>> and the <<mvc-cors-filter>> for more details.
[[mvc-controller]]
== Annotated Controllers
[.small]#<<web-reactive.adoc#webflux-controller, Same as in Spring WebFlux>>#
You can define controller beans by using a standard Spring bean definition in the
Servlet's `WebApplicationContext`. The `@Controller` stereotype allows for auto-detection,
@ -1468,7 +1468,7 @@ change to `<tx:annotation-driven proxy-target-class="true"/>`, and with
@@ -1468,7 +1468,7 @@ change to `<tx:annotation-driven proxy-target-class="true"/>`, and with
[[mvc-ann-requestmapping]]
=== Request Mapping
[.small]#<<web-reactive.adoc#webflux-ann-requestmapping, Same as in Spring WebFlux>>#
You can use the `@RequestMapping` annotation to map requests to controllers methods. It has
various attributes to match by URL, HTTP method, request parameters, headers, and media
@ -1533,7 +1533,7 @@ The following example has type and method level mappings:
@@ -1533,7 +1533,7 @@ The following example has type and method level mappings:
[[mvc-ann-requestmapping-uri-templates]]
==== URI patterns
[.small]#<<web-reactive.adoc#webflux-ann-requestmapping-uri-templates, Same as in Spring WebFlux>>#
You can map requests by using the following global patterns and wildcards:
@ -1633,7 +1633,7 @@ NOTE: Spring MVC uses the `PathMatcher` contract and the `AntPathMatcher` implem
@@ -1633,7 +1633,7 @@ NOTE: Spring MVC uses the `PathMatcher` contract and the `AntPathMatcher` implem
[[mvc-ann-requestmapping-pattern-comparison]]
==== Pattern Comparison
[.small]#<<web-reactive.adoc#webflux-ann-requestmapping-pattern-comparison, Same as in Spring WebFlux>>#
You can narrow the request mapping based on the `Content-Type` of the request,
as the following example shows:
@ -1752,7 +1752,7 @@ TIP: `MediaType` provides constants for commonly used media types, such as
@@ -1752,7 +1752,7 @@ TIP: `MediaType` provides constants for commonly used media types, such as
[[mvc-ann-requestmapping-produces]]
==== Producible Media Types
[.small]#<<web-reactive.adoc#webflux-ann-requestmapping-produces, Same as in Spring WebFlux>>#
You can narrow the request mapping based on the `Accept` request header and the list of
content types that a controller method produces, as the following example shows:
@ -1792,7 +1792,7 @@ TIP: `MediaType` provides constants for commonly used media types, such as
@@ -1792,7 +1792,7 @@ TIP: `MediaType` provides constants for commonly used media types, such as
[[mvc-ann-requestmapping-params-and-headers]]
==== Parameters, headers
[.small]#<<web-reactive.adoc#webflux-ann-requestmapping-params-and-headers, Same as in Spring WebFlux>>#
Spring MVC supports the use of <<core.adoc#beans-meta-annotations, composed annotations>>
for request mapping. Those are annotations that are themselves meta-annotated with
@ -1893,7 +1893,7 @@ you can check the custom attribute and return your own `RequestCondition`.
@@ -1893,7 +1893,7 @@ you can check the custom attribute and return your own `RequestCondition`.
[[mvc-ann-requestmapping-registration]]
==== Explicit Registrations
[.small]#<<web-reactive.adoc#webflux-ann-requestmapping-registration, Same as in Spring WebFlux>>#
You can programmatically register handler methods, which you can use for dynamic
registrations or for advanced cases, such as different instances of the same handler
@ -1946,7 +1946,7 @@ under different URLs. The following example registers a handler method:
@@ -1946,7 +1946,7 @@ under different URLs. The following example registers a handler method:
[[mvc-ann-methods]]
=== Handler Methods
[.small]#<<web-reactive.adoc#webflux-ann-methods, Same as in Spring WebFlux>>#
https://tools.ietf.org/html/rfc3986#section-3.3[RFC 3986] discusses name-value pairs in
path segments. In Spring MVC, we refer to those as "`matrix variables`" based on an
@ -2345,7 +2345,7 @@ you need to set a `UrlPathHelper` with `removeSemicolonContent=false` through
@@ -2345,7 +2345,7 @@ you need to set a `UrlPathHelper` with `removeSemicolonContent=false` through
[[mvc-ann-requestparam]]
==== `@RequestParam`
[.small]#<<web-reactive.adoc#webflux-ann-requestparam, Same as in Spring WebFlux>>#
You can use the `@RequestHeader` annotation to bind a request header to a method argument in a
controller.
@ -2482,7 +2482,7 @@ example, a method parameter annotated with `@RequestHeader("Accept")` can be of
@@ -2482,7 +2482,7 @@ example, a method parameter annotated with `@RequestHeader("Accept")` can be of
[[mvc-ann-cookievalue]]
==== `@CookieValue`
[.small]#<<web-reactive.adoc#webflux-ann-cookievalue, Same as in Spring WebFlux>>#
If you need access to pre-existing session attributes that are managed globally
(that is, outside the controller -- for example, by a filter) and may or may not be present,
@ -2823,7 +2823,7 @@ workflow, consider using `@SessionAttributes` as described in
@@ -2823,7 +2823,7 @@ workflow, consider using `@SessionAttributes` as described in
[[mvc-ann-requestattrib]]
==== `@RequestAttribute`
[.small]#<<web-reactive.adoc#webflux-ann-requestattrib, Same as in Spring WebFlux>>#
Similar to `@SessionAttribute`, you can use the `@RequestAttribute` annotations to
access pre-existing request attributes created earlier (for example, by a Servlet `Filter`
@ -2950,7 +2950,7 @@ Therefore, we recommend that you use flash attributes mainly for redirect scenar
@@ -2950,7 +2950,7 @@ Therefore, we recommend that you use flash attributes mainly for redirect scenar
[[mvc-multipart-forms]]
==== Multipart
[.small]#<<web-reactive.adoc#webflux-multipart-forms, Same as in Spring WebFlux>>#
`HttpEntity` is more or less identical to using <<mvc-ann-requestbody>> but is based on a
container object that exposes request headers and body. The following listing shows an example:
@ -3215,7 +3215,7 @@ container object that exposes request headers and body. The following listing sh
@@ -3215,7 +3215,7 @@ container object that exposes request headers and body. The following listing sh
[[mvc-ann-responsebody]]
==== `@ResponseBody`
[.small]#<<web-reactive.adoc#webflux-ann-responsebody, Same as in Spring WebFlux>>#
@ -3501,7 +3501,7 @@ unless the return value is a `String` that would otherwise be interpreted as a v
@@ -3501,7 +3501,7 @@ unless the return value is a `String` that would otherwise be interpreted as a v
[[mvc-ann-initbinder]]
=== `DataBinder`
[.small]#<<web-reactive.adoc#webflux-ann-initbinder, Same as in Spring WebFlux>>#
`@Controller` or `@ControllerAdvice` classes can have `@InitBinder` methods that
initialize instances of `WebDataBinder`, and those, in turn, can:
@ -3595,7 +3595,7 @@ controller-specific `Formatter` implementations, as the following example shows:
@@ -3595,7 +3595,7 @@ controller-specific `Formatter` implementations, as the following example shows:
[[mvc-ann-exceptionhandler]]
=== Exceptions
[.small]#<<web-reactive.adoc#webflux-ann-controller-exceptions, Same as in Spring WebFlux>>#
Spring MVC supports use of reactive client libraries in a controller (also read
<<web-reactive.adoc#webflux-reactive-libraries, Reactive Libraries>> in the WebFlux section).
@ -4588,7 +4588,7 @@ suitable under load. If you plan to stream with a reactive type, you should use
@@ -4588,7 +4588,7 @@ suitable under load. If you plan to stream with a reactive type, you should use
[[mvc-ann-async-disconnects]]
=== Disconnects
[.small]#<<web-reactive.adoc#webflux-codecs-streaming, Same as in Spring WebFlux>>#
The https://projects.spring.io/spring-security/[Spring Security] project provides support
for protecting web applications from malicious exploits. See the Spring Security
@ -4674,7 +4674,7 @@ https://hdiv.org/[HDIV] is another web security framework that integrates with S
@@ -4674,7 +4674,7 @@ https://hdiv.org/[HDIV] is another web security framework that integrates with S
[[mvc-caching]]
== HTTP Caching
[.small]#<<web-reactive.adoc#webflux-caching, Same as in Spring WebFlux>>#
HTTP caching can significantly improve the performance of a web application. HTTP caching
revolves around the `Cache-Control` response header and, subsequently, conditional request
@ -4690,7 +4690,7 @@ This section describes the HTTP caching-related options that are available in Sp
@@ -4690,7 +4690,7 @@ This section describes the HTTP caching-related options that are available in Sp
[[mvc-caching-cachecontrol]]
=== `CacheControl`
[.small]#<<web-reactive.adoc#webflux-caching-cachecontrol, Same as in Spring WebFlux>>#
In Java configuration, you can use the `@EnableWebMvc` annotation to enable MVC
configuration, as the following example shows:
@ -4928,7 +4928,7 @@ available on the classpath (for example, payload converters for JSON, XML, and o
@@ -4928,7 +4928,7 @@ available on the classpath (for example, payload converters for JSON, XML, and o
[[mvc-config-customize]]
=== MVC Config API
[.small]#<<web-reactive.adoc#webflux-config-customize, Same as in Spring WebFlux>>#
By default, if <<core.adoc#validation-beanvalidation-overview, Bean Validation>> is present
on the classpath (for example, Hibernate Validator), the `LocalValidatorFactoryBean` is
@ -5191,7 +5191,7 @@ The following example shows how to achieve the same configuration in XML:
@@ -5191,7 +5191,7 @@ The following example shows how to achieve the same configuration in XML:
[[mvc-config-content-negotiation]]
=== Content Types
[.small]#<<web-reactive.adoc#webflux-config-content-negotiation, Same as in Spring WebFlux>>#
You can configure how Spring MVC determines the requested media types from the request
(for example, `Accept` header, URL path extension, query parameter, and others).
@ -5257,7 +5257,7 @@ The following example shows how to achieve the same configuration in XML:
@@ -5257,7 +5257,7 @@ The following example shows how to achieve the same configuration in XML:
[[mvc-config-message-converters]]
=== Message Converters
[.small]#<<web-reactive.adoc#webflux-config-message-codecs, Same as in Spring WebFlux>>#
@ -5754,7 +5754,7 @@ The following example shows how to achieve the same configuration in XML:
@@ -5754,7 +5754,7 @@ The following example shows how to achieve the same configuration in XML:
[[mvc-config-path-matching]]
=== Path Matching
[.small]#<<web-reactive.adoc#webflux-config-path-matching, Same as in Spring WebFlux>>#
You can customize options related to path matching and treatment of the URL.
For details on the individual options, see the
@ -5843,7 +5843,7 @@ The following example shows how to achieve the same configuration in XML:
@@ -5843,7 +5843,7 @@ The following example shows how to achieve the same configuration in XML:
[[mvc-config-advanced-java]]
=== Advanced Java Config
[.small]#<<web-reactive.adoc#webflux-config-advanced-java, Same as in Spring WebFlux>>#
@ -5918,7 +5918,7 @@ by letting it be detected through a `<component-scan/>` declaration.
@@ -5918,7 +5918,7 @@ by letting it be detected through a `<component-scan/>` declaration.
[[mvc-http2]]
== HTTP/2
[.small]#<<web-reactive.adoc#webflux-http2, Same as in Spring WebFlux>>#
Creating a WebSocket server is as simple as implementing `WebSocketHandler` or, more
likely, extending either `TextWebSocketHandler` or `BinaryWebSocketHandler`. The following
@ -112,7 +112,7 @@ sending. One option is to wrap the `WebSocketSession` with
@@ -112,7 +112,7 @@ sending. One option is to wrap the `WebSocketSession` with
[[websocket-server-handshake]]
=== WebSocket Handshake
[.small]#<<web-reactive.adoc#webflux-websocket-server-handshake, Same as in Spring WebFlux>>#
The easiest way to customize the initial HTTP WebSocket handshake request is through
a `HandshakeInterceptor`, which exposes methods for "`before`" and "`after`" the handshake.
@ -258,7 +258,7 @@ Java initialization API. The following example shows how to do so:
@@ -258,7 +258,7 @@ Java initialization API. The following example shows how to do so:
[[websocket-server-runtime-configuration]]
=== Server Configuration
[.small]#<<web-reactive.adoc#webflux-websocket-server-config, Same as in Spring WebFlux>>#
Each underlying WebSocket engine exposes configuration properties that control
runtime characteristics, such as the size of message buffer sizes, idle timeout,
@ -385,7 +385,7 @@ The following example shows the XML configuration equivalent of the preceding ex
@@ -385,7 +385,7 @@ The following example shows the XML configuration equivalent of the preceding ex
[[websocket-server-allowed-origins]]
=== Allowed Origins
[.small]#<<web-reactive.adoc#webflux-websocket-server-cors, Same as in Spring WebFlux>>#