|
|
@ -2187,6 +2187,8 @@ method:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
----
|
|
|
|
----
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[[boot-features-spring-webflux]]
|
|
|
|
[[boot-features-spring-webflux]]
|
|
|
|
=== The '`Spring WebFlux framework`'
|
|
|
|
=== The '`Spring WebFlux framework`'
|
|
|
|
|
|
|
|
|
|
|
@ -2195,8 +2197,8 @@ Unlike Spring MVC, it does not require the Servlet API, is fully asynchronous an
|
|
|
|
non-blocking, and implements the http://www.reactive-streams.org/[Reactive Streams]
|
|
|
|
non-blocking, and implements the http://www.reactive-streams.org/[Reactive Streams]
|
|
|
|
specification through http://projectreactor.io/[the Reactor project].
|
|
|
|
specification through http://projectreactor.io/[the Reactor project].
|
|
|
|
|
|
|
|
|
|
|
|
Spring WebFlux comes in two flavours — the annotation-based one is quite close to
|
|
|
|
Spring WebFlux comes in two flavors — the annotation-based one is quite close to the
|
|
|
|
the Spring MVC model we know:
|
|
|
|
Spring MVC model we know:
|
|
|
|
|
|
|
|
|
|
|
|
[source,java,indent=0]
|
|
|
|
[source,java,indent=0]
|
|
|
|
----
|
|
|
|
----
|
|
|
@ -2222,8 +2224,8 @@ the Spring MVC model we know:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
----
|
|
|
|
----
|
|
|
|
|
|
|
|
|
|
|
|
'`WebFlux.fn`', the functional variant, separates the routing configuration from the actual handling
|
|
|
|
'`WebFlux.fn`', the functional variant, separates the routing configuration from the
|
|
|
|
of the requests:
|
|
|
|
actual handling of the requests:
|
|
|
|
|
|
|
|
|
|
|
|
[source,java,indent=0]
|
|
|
|
[source,java,indent=0]
|
|
|
|
----
|
|
|
|
----
|
|
|
@ -2236,17 +2238,17 @@ of the requests:
|
|
|
|
.andRoute(GET("/{user}/customers").and(accept(APPLICATION_JSON)), userHandler::getUserCustomers)
|
|
|
|
.andRoute(GET("/{user}/customers").and(accept(APPLICATION_JSON)), userHandler::getUserCustomers)
|
|
|
|
.andRoute(DELETE("/{user}").and(accept(APPLICATION_JSON)), userHandler::deleteUser);
|
|
|
|
.andRoute(DELETE("/{user}").and(accept(APPLICATION_JSON)), userHandler::deleteUser);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Component
|
|
|
|
@Component
|
|
|
|
public class UserHandler {
|
|
|
|
public class UserHandler {
|
|
|
|
|
|
|
|
|
|
|
|
public Mono<ServerResponse> getUser(ServerRequest request) {
|
|
|
|
public Mono<ServerResponse> getUser(ServerRequest request) {
|
|
|
|
// ...
|
|
|
|
// ...
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public Mono<ServerResponse> getUserCustomers(ServerRequest request) {
|
|
|
|
public Mono<ServerResponse> getUserCustomers(ServerRequest request) {
|
|
|
|
// ...
|
|
|
|
// ...
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -2256,19 +2258,20 @@ of the requests:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
----
|
|
|
|
----
|
|
|
|
|
|
|
|
|
|
|
|
WebFlux is part of the Spring Framework and detailed information is available in
|
|
|
|
WebFlux is part of the Spring Framework and detailed information is available in the
|
|
|
|
the {spring-reference}web.html#web-reactive[reference documentation].
|
|
|
|
{spring-reference}web.html#web-reactive[reference documentation].
|
|
|
|
|
|
|
|
|
|
|
|
To get started, add the `spring-boot-starter-webflux` module to your application.
|
|
|
|
To get started, add the `spring-boot-starter-webflux` module to your application.
|
|
|
|
|
|
|
|
|
|
|
|
NOTE: Adding both `spring-boot-starter-web` and `spring-boot-starter-webflux`
|
|
|
|
NOTE: Adding both `spring-boot-starter-web` and `spring-boot-starter-webflux` modules in
|
|
|
|
modules in your application will result in Spring Boot auto-configuring Spring
|
|
|
|
your application will result in Spring Boot auto-configuring Spring MVC, not WebFlux. This
|
|
|
|
MVC, not WebFlux. This behavior has been chosen because many Spring developers
|
|
|
|
behavior has been chosen because many Spring developers will add
|
|
|
|
will add `spring-boot-starter-webflux` to their Spring MVC application to use
|
|
|
|
`spring-boot-starter-webflux` to their Spring MVC application to use the reactive
|
|
|
|
the reactive `WebCLient`. You can still enforce your choice by setting the
|
|
|
|
`WebCLient`. You can still enforce your choice by setting the chosen application type like
|
|
|
|
chosen application type like
|
|
|
|
|
|
|
|
`SpringApplication.setWebApplicationType(WebApplicationType.REACTIVE)`.
|
|
|
|
`SpringApplication.setWebApplicationType(WebApplicationType.REACTIVE)`.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[[boot-features-spring-webflux-auto-configuration]]
|
|
|
|
[[boot-features-spring-webflux-auto-configuration]]
|
|
|
|
==== Spring WebFlux auto-configuration
|
|
|
|
==== Spring WebFlux auto-configuration
|
|
|
|
Spring Boot provides auto-configuration for Spring WebFlux that works well with most
|
|
|
|
Spring Boot provides auto-configuration for Spring WebFlux that works well with most
|
|
|
@ -2276,29 +2279,30 @@ applications.
|
|
|
|
|
|
|
|
|
|
|
|
The auto-configuration adds the following features on top of Spring's defaults:
|
|
|
|
The auto-configuration adds the following features on top of Spring's defaults:
|
|
|
|
|
|
|
|
|
|
|
|
* Configuring codecs for `HttpMessageReader` and `HttpMessageWriter` instances (see below).
|
|
|
|
* Configuring codecs for `HttpMessageReader` and `HttpMessageWriter` instances (see
|
|
|
|
|
|
|
|
below).
|
|
|
|
* Support for serving static resources, including support for WebJars (see below).
|
|
|
|
* Support for serving static resources, including support for WebJars (see below).
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
If you want to keep Spring Boot WebFlux features, and you just want to add additional
|
|
|
|
|
|
|
|
{spring-reference}web.html#web-reactive[WebFlux configuration] you can add your own
|
|
|
|
|
|
|
|
`@Configuration` class of type `WebFluxConfigurer`, but *without* `@EnableWebFlux`.
|
|
|
|
|
|
|
|
|
|
|
|
If you want to keep Spring Boot WebFlux features, and
|
|
|
|
If you want to take complete control of Spring WebFlux, you can add your own
|
|
|
|
you just want to add additional {spring-reference}web.html#web-reactive[WebFlux configuration]
|
|
|
|
`@Configuration` annotated with `@EnableWebFlux`.
|
|
|
|
you can add your own `@Configuration` class of type `WebFluxConfigurer`,
|
|
|
|
|
|
|
|
but *without* `@EnableWebFlux`.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
If you want to take complete control of Spring WebFlux, you can add your own `@Configuration`
|
|
|
|
|
|
|
|
annotated with `@EnableWebFlux`.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[[boot-features-spring-webflux-httpcodecs]]
|
|
|
|
[[boot-features-spring-webflux-httpcodecs]]
|
|
|
|
==== HTTP codecs with HttpMessageReaders and HttpMessageWriters
|
|
|
|
==== HTTP codecs with HttpMessageReaders and HttpMessageWriters
|
|
|
|
Spring WebFlux uses the `HttpMessageReader` and `HttpMessageWriter` interface to convert
|
|
|
|
Spring WebFlux uses the `HttpMessageReader` and `HttpMessageWriter` interface to convert
|
|
|
|
HTTP requests and responses. They are configured with `CodecConfigurer` with sensible defaults,
|
|
|
|
HTTP requests and responses. They are configured with `CodecConfigurer` with sensible
|
|
|
|
by looking at the libraries available in your classpath.
|
|
|
|
defaults, by looking at the libraries available in your classpath.
|
|
|
|
|
|
|
|
|
|
|
|
Spring Boot will apply further customization using `CodecCustomizer` instances.
|
|
|
|
Spring Boot will apply further customization using `CodecCustomizer` instances.
|
|
|
|
For example, `spring.jackson.*` configuration keys will be applied to the Jackson codec.
|
|
|
|
For example, `spring.jackson.*` configuration keys will be applied to the Jackson codec.
|
|
|
|
|
|
|
|
|
|
|
|
If you need to add or customize codecs you can create a custom `CodecCustomizer` component:
|
|
|
|
If you need to add or customize codecs you can create a custom `CodecCustomizer`
|
|
|
|
|
|
|
|
component:
|
|
|
|
|
|
|
|
|
|
|
|
[source,java,indent=0]
|
|
|
|
[source,java,indent=0]
|
|
|
|
----
|
|
|
|
----
|
|
|
@ -2319,6 +2323,8 @@ If you need to add or customize codecs you can create a custom `CodecCustomizer`
|
|
|
|
|
|
|
|
|
|
|
|
You can also leverage <<boot-features-json-components,Boot's custom JSON serializers and deserializers>>.
|
|
|
|
You can also leverage <<boot-features-json-components,Boot's custom JSON serializers and deserializers>>.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[[boot-features-spring-webflux-static-content]]
|
|
|
|
[[boot-features-spring-webflux-static-content]]
|
|
|
|
==== Static Content
|
|
|
|
==== Static Content
|
|
|
|
By default Spring Boot will serve static content from a directory called `/static` (or
|
|
|
|
By default Spring Boot will serve static content from a directory called `/static` (or
|
|
|
@ -2353,9 +2359,9 @@ be deployed as war and have no use of the `src/main/webapp` directory.
|
|
|
|
|
|
|
|
|
|
|
|
[[boot-features-spring-webflux-template-engines]]
|
|
|
|
[[boot-features-spring-webflux-template-engines]]
|
|
|
|
==== Template engines
|
|
|
|
==== Template engines
|
|
|
|
As well as REST web services, you can also use Spring WebFlux to serve dynamic HTML content.
|
|
|
|
As well as REST web services, you can also use Spring WebFlux to serve dynamic HTML
|
|
|
|
Spring WebFlux supports a variety of templating technologies including Thymeleaf, FreeMarker
|
|
|
|
content. Spring WebFlux supports a variety of templating technologies including Thymeleaf,
|
|
|
|
and Mustache.
|
|
|
|
FreeMarker and Mustache.
|
|
|
|
|
|
|
|
|
|
|
|
Spring Boot includes auto-configuration support for the following templating engines:
|
|
|
|
Spring Boot includes auto-configuration support for the following templating engines:
|
|
|
|
|
|
|
|
|
|
|
@ -2367,6 +2373,7 @@ When you're using one of these templating engines with the default configuration
|
|
|
|
templates will be picked up automatically from `src/main/resources/templates`.
|
|
|
|
templates will be picked up automatically from `src/main/resources/templates`.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[[boot-features-jersey]]
|
|
|
|
[[boot-features-jersey]]
|
|
|
|
=== JAX-RS and Jersey
|
|
|
|
=== JAX-RS and Jersey
|
|
|
|
If you prefer the JAX-RS programming model for REST endpoints you can use one of the
|
|
|
|
If you prefer the JAX-RS programming model for REST endpoints you can use one of the
|
|
|
@ -5094,6 +5101,8 @@ TIP: `RestTemplateBuilder` includes a number of useful methods that can be used
|
|
|
|
configure a `RestTemplate`. For example, to add BASIC auth support you can use
|
|
|
|
configure a `RestTemplate`. For example, to add BASIC auth support you can use
|
|
|
|
`builder.basicAuthorization("user", "password").build()`.
|
|
|
|
`builder.basicAuthorization("user", "password").build()`.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[[boot-features-resttemplate-customization]]
|
|
|
|
[[boot-features-resttemplate-customization]]
|
|
|
|
=== RestTemplate customization
|
|
|
|
=== RestTemplate customization
|
|
|
|
There are three main approaches to `RestTemplate` customization, depending on how broadly
|
|
|
|
There are three main approaches to `RestTemplate` customization, depending on how broadly
|
|
|
@ -5121,9 +5130,9 @@ Lastly, the most extreme (and rarely used) option is to create your own
|
|
|
|
`RestTemplateBuilder` and will prevent any `RestTemplateCustomizer` beans from being used.
|
|
|
|
`RestTemplateBuilder` and will prevent any `RestTemplateCustomizer` beans from being used.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[[boot-features-webclient]]
|
|
|
|
[[boot-features-webclient]]
|
|
|
|
== Calling REST services with '`WebClient`'
|
|
|
|
== Calling REST services with '`WebClient`'
|
|
|
|
|
|
|
|
|
|
|
|
If you have Spring WebFlux on your classpath, you can also choose to use `WebClient`
|
|
|
|
If you have Spring WebFlux on your classpath, you can also choose to use `WebClient`
|
|
|
|
to call remote REST services; compared to `RestTemplate`, this client has more a
|
|
|
|
to call remote REST services; compared to `RestTemplate`, this client has more a
|
|
|
|
functional feel to it and is fully reactive. You can create your own client
|
|
|
|
functional feel to it and is fully reactive. You can create your own client
|
|
|
@ -5176,6 +5185,8 @@ would do locally at the point of injection.
|
|
|
|
Lastly, you can fall back to the original API and just use `WebClient.create()`. In that
|
|
|
|
Lastly, you can fall back to the original API and just use `WebClient.create()`. In that
|
|
|
|
case, no auto-configuration nor `WebClientCustomizer` will be applied.
|
|
|
|
case, no auto-configuration nor `WebClientCustomizer` will be applied.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[[boot-features-validation]]
|
|
|
|
[[boot-features-validation]]
|
|
|
|
== Validation
|
|
|
|
== Validation
|
|
|
|
The method validation feature supported by Bean Validation 1.1 is automatically enabled
|
|
|
|
The method validation feature supported by Bean Validation 1.1 is automatically enabled
|
|
|
@ -5992,12 +6003,15 @@ definition.
|
|
|
|
A list of the auto-configuration that is enabled by `@WebMvcTest` can be
|
|
|
|
A list of the auto-configuration that is enabled by `@WebMvcTest` can be
|
|
|
|
<<appendix-test-auto-configuration#test-auto-configuration,found in the appendix>>.
|
|
|
|
<<appendix-test-auto-configuration#test-auto-configuration,found in the appendix>>.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[[boot-features-testing-spring-boot-applications-testing-autoconfigured-webflux-tests]]
|
|
|
|
[[boot-features-testing-spring-boot-applications-testing-autoconfigured-webflux-tests]]
|
|
|
|
==== Auto-configured Spring WebFlux tests
|
|
|
|
==== Auto-configured Spring WebFlux tests
|
|
|
|
To test Spring WebFlux controllers are working as expected you can use the `@WebFluxTest`
|
|
|
|
To test Spring WebFlux controllers are working as expected you can use the `@WebFluxTest`
|
|
|
|
annotation. `@WebFluxTest` will auto-configure the Spring WebFlux infrastructure and limit
|
|
|
|
annotation. `@WebFluxTest` will auto-configure the Spring WebFlux infrastructure and limit
|
|
|
|
scanned beans to `@Controller`, `@ControllerAdvice`, `@JsonComponent`,and `WebFluxConfigurer`.
|
|
|
|
scanned beans to `@Controller`, `@ControllerAdvice`, `@JsonComponent`,and
|
|
|
|
Regular `@Component` beans will not be scanned when using this annotation.
|
|
|
|
`WebFluxConfigurer`. Regular `@Component` beans will not be scanned when using this
|
|
|
|
|
|
|
|
annotation.
|
|
|
|
|
|
|
|
|
|
|
|
Often `@WebFluxTest` will be limited to a single controller and used in combination with
|
|
|
|
Often `@WebFluxTest` will be limited to a single controller and used in combination with
|
|
|
|
`@MockBean` to provide mock implementations for required collaborators.
|
|
|
|
`@MockBean` to provide mock implementations for required collaborators.
|
|
|
@ -6010,14 +6024,14 @@ TIP: You can also auto-configure `WebTestClient` in a non-`@WebFluxTest`
|
|
|
|
|
|
|
|
|
|
|
|
[source,java,indent=0]
|
|
|
|
[source,java,indent=0]
|
|
|
|
----
|
|
|
|
----
|
|
|
|
import org.junit.Test;
|
|
|
|
import org.junit.Test;
|
|
|
|
import org.junit.runner.RunWith;
|
|
|
|
import org.junit.runner.RunWith;
|
|
|
|
|
|
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest;
|
|
|
|
import org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTest;
|
|
|
|
import org.springframework.http.MediaType;
|
|
|
|
import org.springframework.http.MediaType;
|
|
|
|
import org.springframework.test.context.junit4.SpringRunner;
|
|
|
|
import org.springframework.test.context.junit4.SpringRunner;
|
|
|
|
import org.springframework.test.web.reactive.server.WebTestClient;
|
|
|
|
import org.springframework.test.web.reactive.server.WebTestClient;
|
|
|
|
|
|
|
|
|
|
|
|
@RunWith(SpringRunner.class)
|
|
|
|
@RunWith(SpringRunner.class)
|
|
|
|
@WebFluxTest(UserVehicleController.class)
|
|
|
|
@WebFluxTest(UserVehicleController.class)
|
|
|
@ -6046,6 +6060,7 @@ A list of the auto-configuration that is enabled by `@WebFluxTest` can be
|
|
|
|
<<appendix-test-auto-configuration#test-auto-configuration,found in the appendix>>.
|
|
|
|
<<appendix-test-auto-configuration#test-auto-configuration,found in the appendix>>.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[[boot-features-testing-spring-boot-applications-testing-autoconfigured-jpa-test]]
|
|
|
|
[[boot-features-testing-spring-boot-applications-testing-autoconfigured-jpa-test]]
|
|
|
|
==== Auto-configured Data JPA tests
|
|
|
|
==== Auto-configured Data JPA tests
|
|
|
|
`@DataJpaTest` can be used if you want to test JPA applications. By default it will
|
|
|
|
`@DataJpaTest` can be used if you want to test JPA applications. By default it will
|
|
|
|