Use convention based code imports

Closes gh-29647
This commit is contained in:
Phillip Webb
2022-02-03 15:01:30 -08:00
parent 71695d2162
commit 0e906dc6e2
57 changed files with 250 additions and 967 deletions

View File

@@ -10,22 +10,13 @@ Unlike Spring MVC, it does not require the servlet API, is fully asynchronous an
Spring WebFlux comes in two flavors: functional and annotation-based.
The annotation-based one is quite close to the Spring MVC model, as shown in the following example:
[source,java,indent=0,subs="verbatim"]
----
include::{docs-java}/web/reactive/webflux/MyRestController.java[]
----
include::code:MyRestController[]
"`WebFlux.fn`", the functional variant, separates the routing configuration from the actual handling of the requests, as shown in the following example:
[source,java,indent=0,subs="verbatim"]
----
include::{docs-java}/web/reactive/webflux/MyRoutingConfiguration.java[]
----
include::code:MyRoutingConfiguration[]
[source,java,indent=0,subs="verbatim"]
----
include::{docs-java}/web/reactive/webflux/MyUserHandler.java[]
----
include::code:MyUserHandler[]
WebFlux is part of the Spring Framework and detailed information is available in its {spring-framework-docs}/web-reactive.html#webflux-fn[reference documentation].
@@ -41,15 +32,9 @@ You can still enforce your choice by setting the chosen application type to `Spr
"`WebFlux.fn`", the functional variant, separates the routing configuration from the actual handling of the requests, as shown in the following example:
[source,java,indent=0,subs="verbatim"]
----
include::{docs-java}/web/reactive/webflux/MyRoutingConfiguration.java[]
----
include::code:MyRoutingConfiguration[]
[source,java,indent=0,subs="verbatim"]
----
include::{docs-java}/web/reactive/webflux/MyUserHandler.java[]
----
include::code:MyUserHandler[]
WebFlux is part of the Spring Framework and detailed information is available in its {spring-framework-docs}/web-reactive.html#webflux-fn[reference documentation].
@@ -88,10 +73,7 @@ For example, `+spring.jackson.*+` configuration keys are applied to the Jackson
If you need to add or customize codecs, you can create a custom `CodecCustomizer` component, as shown in the following example:
[source,java,indent=0,subs="verbatim"]
----
include::{docs-java}/web/reactive/webflux/httpcodecs/MyCodecsConfiguration.java[]
----
include::code:MyCodecsConfiguration[]
You can also leverage <<web#web.servlet.spring-mvc.json,Boot's custom JSON serializers and deserializers>>.
@@ -162,20 +144,14 @@ For that, you can add a bean of type `ErrorAttributes`.
To change the error handling behavior, you can implement `ErrorWebExceptionHandler` and register a bean definition of that type.
Because a `ErrorWebExceptionHandler` is quite low-level, Spring Boot also provides a convenient `AbstractErrorWebExceptionHandler` to let you handle errors in a WebFlux functional way, as shown in the following example:
[source,java,indent=0,subs="verbatim"]
----
include::{docs-java}/web/reactive/webflux/errorhandling/MyErrorWebExceptionHandler.java[]
----
include::code:MyErrorWebExceptionHandler[]
For a more complete picture, you can also subclass `DefaultErrorWebExceptionHandler` directly and override specific methods.
In some cases, errors handled at the controller or handler function level are not recorded by the <<actuator#actuator.metrics.supported.spring-webflux, metrics infrastructure>>.
Applications can ensure that such exceptions are recorded with the request metrics by setting the handled exception as a request attribute:
[source,java,indent=0,subs="verbatim"]
----
include::{docs-java}/web/reactive/webflux/errorhandling/MyExceptionHandlingController.java[]
----
include::code:MyExceptionHandlingController[]

View File

@@ -10,10 +10,7 @@ Methods in your controller are mapped to HTTP by using `@RequestMapping` annotat
The following code shows a typical `@RestController` that serves JSON data:
[source,java,indent=0,subs="verbatim"]
----
include::{docs-java}/web/servlet/springmvc/MyRestController.java[]
----
include::code:MyRestController[]
Spring MVC is part of the core Spring Framework, and detailed information is available in the {spring-framework-docs}/web.html#mvc[reference documentation].
There are also several guides that cover Spring MVC available at https://spring.io/guides.
@@ -60,10 +57,7 @@ By default, strings are encoded in `UTF-8`.
If you need to add or customize converters, you can use Spring Boot's `HttpMessageConverters` class, as shown in the following listing:
[source,java,indent=0,subs="verbatim"]
----
include::{docs-java}/web/servlet/springmvc/messageconverters/MyHttpMessageConvertersConfiguration.java[]
----
include::code:MyHttpMessageConvertersConfiguration[]
Any `HttpMessageConverter` bean that is present in the context is added to the list of converters.
You can also override default converters in the same way.
@@ -78,10 +72,7 @@ Custom serializers are usually https://github.com/FasterXML/jackson-docs/wiki/Ja
You can use the `@JsonComponent` annotation directly on `JsonSerializer`, `JsonDeserializer` or `KeyDeserializer` implementations.
You can also use it on classes that contain serializers/deserializers as inner classes, as shown in the following example:
[source,java,indent=0,subs="verbatim"]
----
include::{docs-java}/web/servlet/springmvc/json/MyJsonComponent.java[]
----
include::code:MyJsonComponent[]
All `@JsonComponent` beans in the `ApplicationContext` are automatically registered with Jackson.
Because `@JsonComponent` is meta-annotated with `@Component`, the usual component-scanning rules apply.
@@ -91,10 +82,7 @@ See {spring-boot-module-api}/jackson/JsonObjectSerializer.html[`JsonObjectSerial
The example above can be rewritten to use `JsonObjectSerializer`/`JsonObjectDeserializer` as follows:
[source,java,indent=0,subs="verbatim"]
----
include::{docs-java}/web/servlet/springmvc/json/object/MyJsonComponent.java[]
----
include::code:object/MyJsonComponent[]
@@ -340,20 +328,14 @@ To do so, extend `BasicErrorController`, add a public method with a `@RequestMap
You can also define a class annotated with `@ControllerAdvice` to customize the JSON document to return for a particular controller and/or exception type, as shown in the following example:
[source,java,indent=0,subs="verbatim"]
----
include::{docs-java}/web/servlet/springmvc/errorhandling/MyControllerAdvice.java[]
----
include::code:MyControllerAdvice[]
In the preceding example, if `YourException` is thrown by a controller defined in the same package as `SomeController`, a JSON representation of the `CustomErrorType` POJO is used instead of the `ErrorAttributes` representation.
In some cases, errors handled at the controller level are not recorded by the <<actuator#actuator.metrics.supported.spring-mvc, metrics infrastructure>>.
Applications can ensure that such exceptions are recorded with the request metrics by setting the handled exception as a request attribute:
[source,java,indent=0,subs="verbatim"]
----
include::{docs-java}/web/servlet/springmvc/errorhandling/MyController.java[]
----
include::code:MyController[]
@@ -395,10 +377,7 @@ To map all `5xx` errors by using a FreeMarker template, your directory structure
For more complex mappings, you can also add beans that implement the `ErrorViewResolver` interface, as shown in the following example:
[source,java,indent=0,subs="verbatim"]
----
include::{docs-java}/web/servlet/springmvc/errorhandling/errorpages/MyErrorViewResolver.java[]
----
include::code:MyErrorViewResolver[]
You can also use regular Spring MVC features such as {spring-framework-docs}/web.html#mvc-exceptionhandlers[`@ExceptionHandler` methods] and {spring-framework-docs}/web.html#mvc-ann-controller-advice[`@ControllerAdvice`].
The `ErrorController` then picks up any unhandled exceptions.
@@ -410,17 +389,11 @@ The `ErrorController` then picks up any unhandled exceptions.
For applications that do not use Spring MVC, you can use the `ErrorPageRegistrar` interface to directly register `ErrorPages`.
This abstraction works directly with the underlying embedded servlet container and works even if you do not have a Spring MVC `DispatcherServlet`.
[source,java,indent=0,subs="verbatim"]
----
include::{docs-java}/web/servlet/springmvc/errorhandling/errorpageswithoutspringmvc/MyErrorPagesConfiguration.java[]
----
include::code:MyErrorPagesConfiguration[]
NOTE: If you register an `ErrorPage` with a path that ends up being handled by a `Filter` (as is common with some non-Spring web frameworks, like Jersey and Wicket), then the `Filter` has to be explicitly registered as an `ERROR` dispatcher, as shown in the following example:
[source,java,indent=0,subs="verbatim"]
----
include::{docs-java}/web/servlet/springmvc/errorhandling/errorpageswithoutspringmvc/MyFilterConfiguration.java[]
----
include::code:MyFilterConfiguration[]
Note that the default `FilterRegistrationBean` does not include the `ERROR` dispatcher type.
@@ -449,10 +422,7 @@ As of version 4.2, Spring MVC {spring-framework-docs}/web.html#mvc-cors[supports
Using {spring-framework-docs}/web.html#mvc-cors-controller[controller method CORS configuration] with {spring-framework-api}/web/bind/annotation/CrossOrigin.html[`@CrossOrigin`] annotations in your Spring Boot application does not require any specific configuration.
{spring-framework-docs}/web.html#mvc-cors-global[Global CORS configuration] can be defined by registering a `WebMvcConfigurer` bean with a customized `addCorsMappings(CorsRegistry)` method, as shown in the following example:
[source,java,indent=0,subs="verbatim"]
----
include::{docs-java}/web/servlet/springmvc/cors/MyCorsConfiguration.java[]
----
include::code:MyCorsConfiguration[]
@@ -465,10 +435,7 @@ Jersey has some native Spring support, so we also provide auto-configuration sup
To get started with Jersey, include the `spring-boot-starter-jersey` as a dependency and then you need one `@Bean` of type `ResourceConfig` in which you register all the endpoints, as shown in the following example:
[source,java,indent=0,subs="verbatim"]
----
include::{docs-java}/web/servlet/jersey/MyJerseyConfig.java[]
----
include::code:MyJerseyConfig[]
WARNING: Jersey's support for scanning executable archives is rather limited.
For example, it cannot scan for endpoints in a package found in a <<deployment#deployment.installing, fully executable jar file>> or in `WEB-INF/classes` when running an executable war file.
@@ -478,10 +445,7 @@ For more advanced customizations, you can also register an arbitrary number of b
All the registered endpoints should be `@Components` with HTTP resource annotations (`@GET` and others), as shown in the following example:
[source,java,indent=0,subs="verbatim"]
----
include::{docs-java}/web/servlet/jersey/MyEndpoint.java[]
----
include::code:MyEndpoint[]
Since the `Endpoint` is a Spring `@Component`, its lifecycle is managed by Spring and you can use the `@Autowired` annotation to inject dependencies and use the `@Value` annotation to inject external configuration.
By default, the Jersey servlet is registered and mapped to `/*`.
@@ -614,10 +578,7 @@ The `CookieSameSiteSupplier` is passed a `Cookie` and may return a `SameSite` va
There are a number of convenience factory and filter methods that you can use to quickly match specific cookies.
For example, adding the following bean will automatically apply a `SameSite` of `Lax` for all cookies with a name that matches the regular expression `myapp.*`.
[source,java,indent=0,subs="verbatim"]
----
include::{docs-java}/web/servlet/embeddedcontainer/customizing/samesite/MySameSiteConfiguration.java[]
----
include::code:MySameSiteConfiguration[]
@@ -627,18 +588,12 @@ If you need to programmatically configure your embedded servlet container, you c
`WebServerFactoryCustomizer` provides access to the `ConfigurableServletWebServerFactory`, which includes numerous customization setter methods.
The following example shows programmatically setting the port:
[source,java,indent=0,subs="verbatim"]
----
include::{docs-java}/web/servlet/embeddedcontainer/customizing/programmatic/MyWebServerFactoryCustomizer.java[]
----
include::code:MyWebServerFactoryCustomizer[]
`TomcatServletWebServerFactory`, `JettyServletWebServerFactory` and `UndertowServletWebServerFactory` are dedicated variants of `ConfigurableServletWebServerFactory` that have additional customization setter methods for Tomcat, Jetty and Undertow respectively.
The following example shows how to customize `TomcatServletWebServerFactory` that provides access to Tomcat-specific configuration options:
[source,java,indent=0,subs="verbatim"]
----
include::{docs-java}/web/servlet/embeddedcontainer/customizing/programmatic/MyTomcatWebServerFactoryCustomizer.java[]
----
include::code:MyTomcatWebServerFactoryCustomizer[]

View File

@@ -54,10 +54,7 @@ Typically, however, applications will not implement `DataFetcher` directly and w
Spring Boot will automatically detect `@Controller` classes with annotated handler methods and register those as `DataFetcher`s.
Here's a sample implementation for our greeting query with a `@Controller` class:
[source,java,indent=0,subs="verbatim"]
----
include::{docs-java}/web/graphql/runtimewiring/GreetingController.java[]
----
include::code:GreetingController[]

View File

@@ -60,10 +60,7 @@ Spring Boot provides convenience methods that can be used to override access rul
For example, you can customize your security configuration by adding something like:
[source,java,indent=0,subs="verbatim"]
----
include::{docs-java}/web/security/springwebflux/MyWebFluxSecurityConfiguration.java[]
----
include::code:MyWebFluxSecurityConfiguration[]
@@ -139,10 +136,7 @@ By default, Spring Security's `OAuth2LoginAuthenticationFilter` only processes U
If you want to customize the `redirect-uri` to use a different pattern, you need to provide configuration to process that custom pattern.
For example, for servlet applications, you can add your own `SecurityFilterChain` that resembles the following:
[source,java,indent=0,subs="verbatim"]
----
include::{docs-java}/web/security/oauth2/client/MyOAuthClientConfiguration.java[]
----
include::code:MyOAuthClientConfiguration[]
TIP: Spring Boot auto-configures an `InMemoryOAuth2AuthorizedClientService` which is used by Spring Security for the management of client registrations.
The `InMemoryOAuth2AuthorizedClientService` has limited capabilities and we recommend using it only for development environments.