Compare commits

...

61 Commits

Author SHA1 Message Date
Spring Builds 38206d0856 Release v6.0.4 2023-01-11 12:10:28 +00:00
Brian Clozel 3d571e8ab5 Upgrade to Micrometer 1.10.3
Closes gh-29802
2023-01-11 11:55:43 +01:00
Juergen Hoeller c7cfdfc26c Downgrade to Mockito 4.9 for spring-beans tests
Includes upgrade to HtmlUnit 2.69
2023-01-11 00:26:30 +01:00
Juergen Hoeller 4d27aee7cf Upgrade to Checkstyle 10.6 2023-01-11 00:03:23 +01:00
Juergen Hoeller 63f0beb209 Upgrade to Reactor 2022.0.2
Includes Mockito 4.11 and AssertJ 3.24.1

Closes gh-29797
2023-01-11 00:01:34 +01:00
Sam Brannen 8dbdfb0449 Polishing 2023-01-10 16:08:02 +01:00
Sam Brannen d5fb5d029b Polish contribution 2023-01-10 16:07:48 +01:00
Vasiliy Kudriavtsev 3738a45658 Avoid wasted memory on empty maps and sets
This commit avoids wasted memory on empty hash maps in
MergedAnnotationReadingVisitor and empty sets in InjectionMetadata.

Closes gh-29742
2023-01-10 15:52:17 +01:00
Sam Brannen fd3e99c7ee Upgrade to JUnit 5.9.2 2023-01-10 15:23:20 +01:00
Sébastien Deleuze 4396801933 Add reflection hints for Kotlin reflection on functions
Kotlin reflection API invocation on a specific function
may require iterating on all Java methods to find the right
Kotlin function. As a consequence, this commit adds introspection
hints on the class declared methods for all Kotlin beans since
the impact on the footprint is low.

Closes gh-29663
2023-01-10 14:49:26 +01:00
Brian Clozel a516ed8739 Upgrade Gradle Enterprise & Conventions plugins 2023-01-10 09:10:59 +01:00
Juergen Hoeller 88cac6a4a3 Reduce creation of composite interfaces to actual method references in current pointcut
Closes gh-29519
2023-01-09 20:34:20 +01:00
Sam Brannen 11df955d88 Update copyright headers 2023-01-09 18:33:06 +01:00
Sam Brannen 77832a6da9 Apply "instanceof pattern matching" in HttpHeaders 2023-01-09 18:27:13 +01:00
Sam Brannen ce1f6cf0bf Polishing 2023-01-09 18:27:13 +01:00
rstoyanchev c702cd418e Polishing
See gh-29721
2023-01-06 16:50:19 +00:00
Simon Baslé 4bcc24372a Add ExecutingResponseCreator
This commit adds a new `ResponseCreator` implementation that uses a
`ClientHttpRequestFactory` to perform an actual request.

Closes gh-29721
2022-12-20 16:36:06 +01:00
rstoyanchev ae7cff35dc Polishing 2022-12-20 09:39:15 +00:00
IDJack 8282be682c Fix typo in core-aot.adoc
Closes gh-29788
2023-01-09 13:19:16 +01:00
Sébastien Deleuze b81a291c85 Add reflection hints for @RequestPart
Closes gh-29749
2023-01-06 17:26:33 +01:00
Sébastien Deleuze 3348e74ab8 Add native support for @Convert on JPA entities
This commit infers the reflection hints required for converters
when they are specified with the @Convert annotation at class or
field level.

It also refines the hints generated for @Converter in order
to just generate the construct hint, not the public method one
which should be not needed.

Closes gh-29771
2023-01-06 12:56:26 +01:00
Simon Baslé e2832ea596 Polish #29727: Mention Kotlin andExpectAll in reference manual (#29766)
This just adds a Kotlin snippet alongside the Java snippet in the
reference manual.

Relates to gh-29727
Closes gh-27317
2023-01-04 19:11:54 +01:00
Simon Baslé 74f58198fd Add Kotlin DSL support for MockMVC andExpectAll (#29727)
As the DSL internally calls `ResultActions.andExpect`, this is done with
a trick where a synthetic `ResultActions` is provided at top level which
stores each `ResultMatcher` in a mutable list.

Once the DSL usage is done, the top level DSL `andExpectAll` turns that
list into a `vararg` passed down to the actual `actions.andExpectAll`.

Closes gh-27317
2023-01-03 18:26:26 +01:00
Juergen Hoeller 42b16591ec SpringPersistenceUnitInfo leniently ignores transformer if no LoadTimeWeaver is present
Closes gh-29736
2023-01-03 11:55:33 +01:00
Johnny Lim 62cf2f0a4f Fix wrong asserted code in SQLExceptionSubclassTranslatorTests (#29748)
There was a typo in the test, covering the wrong SQLState code.

Polishes a644245.
Relates to gh-29699.
2023-01-03 10:57:02 +01:00
Juergen Hoeller 32e1a6452a Upgrade to Undertow 2.3.2, Apache HttpClient 5.2.1, Selenium HtmlUnit Driver 2.67 2022-12-23 16:11:16 +01:00
Juergen Hoeller 55295faff0 Upgrade to Tomcat 10.1.4, Jetty 11.0.13, Netty 4.1.86, Protobuf 3.21.12, SLF4J 2.0.6 2022-12-23 15:14:52 +01:00
Juergen Hoeller 254c3725e2 Polishing 2022-12-23 15:14:11 +01:00
Juergen Hoeller d5ff232246 Defensive check for null returned from createConnection()
Closes gh-29706
2022-12-23 15:13:41 +01:00
Juergen Hoeller a644245e0e Check well-known database error codes in case of generic SQL state 23000
Closes gh-29699
2022-12-23 15:13:29 +01:00
Sam Brannen 697292ba0c Apply update_copyright_headers.sh 2022-12-20 15:21:54 +01:00
Brian Clozel 5366ac84e6 Fix path within mapping when pattern contains ".*"
Prior to this commit, extracting the path within handler mapping would
result in "" if the matching path element would be a Regex and contain
".*". This could cause issues with resource handling if the handler
mapping pattern was similar to `"/folder/file.*.extension"`.

This commit introduces a new `isLiteral()` method in the `PathElement`
abstract class that expresses whether the path element can be compared
as a String for path matching or if it requires a more elaborate
matching process.

Using this method for extracting the path within handler mapping avoids
relying on wildcard count or other properties.

Fixes gh-29712
2022-12-19 10:15:39 +01:00
Sam Brannen b71db12c43 Apply "instanceof pattern matching" in spring-aop 2022-12-18 14:14:40 +01:00
Sam Brannen ab571e3562 Revert incorrect change to ParamAware Javadoc 2022-12-18 14:14:40 +01:00
Sam Brannen 5e42a6eb0a Update Trigger & TriggerContext reference documentation
Closes gh-29702
2022-12-18 13:21:27 +01:00
Sam Brannen 0bfddbc9cb Polishing 2022-12-18 13:20:46 +01:00
diguage 511dab1ade Apply "instanceof pattern matching" (#29710) 2022-12-18 12:46:15 +01:00
Sam Brannen d4623a393b Update copyright headers 2022-12-18 12:16:19 +01:00
Sam Brannen c2c3be4ee3 Polishing 2022-12-18 12:05:24 +01:00
Sam Brannen 270f8526d1 Fix typos in anchors 2022-12-18 12:05:24 +01:00
Sam Brannen 4171e35546 Fix formatting in examples 2022-12-18 12:05:18 +01:00
Sam Brannen bea505d32e Fix typos in reference manual 2022-12-18 12:05:18 +01:00
Sam Brannen 5c28b56823 Remove duplicated words in Javadoc 2022-12-18 12:05:18 +01:00
Sam Brannen d2b54e2611 Remove duplicate words in reference manual
Found using regular expression: \b(\w+)\s+\1\b
2022-12-18 10:46:30 +01:00
Sam Brannen f8a58f2bc4 Update Jakarta Mail info in ref docs
Closes gh-29707
2022-12-17 14:24:38 +01:00
Sam Brannen f6e1fb3150 Cross reference WebTestClient section 2022-12-17 14:01:21 +01:00
Simon Baslé 14c7b9bd08 Change plain 'WebMVC' links to 'See equivalent in the Servlet stack'
Closes gh-29694
2022-12-17 13:51:29 +01:00
Sam Brannen 11f3edc352 Change plain 'WebFlux' links to 'See equivalent in the Reactive stack'
Closes gh-29694
2022-12-17 13:51:29 +01:00
Simon Baslé fbd4463f20 Fix subsection style in WebFlux Concurrency Model
The block title style previously used was not rendered in HTML and the
title couldn't be differentiated from the text. Though, it was in the
PDF, as italics.

Introducing delimited blocks in the open (`--`) style did introduce
styling, but the vertical alignment isn't great.

This commit turns these block titles to actual (deep) section titles.
In the final HTML, at this depth there is no numbering but bold styling
is there. The PDF rendering has also been verified to have relevant
style.

Closes gh-29694
2022-12-17 13:43:08 +01:00
Simon Baslé a3a94950f6 Rework linking to Spring MVC Async support vs WebFlux section
The link was previously named "Compared to WebFlux", which is easy to
mix up with the various links to equivalent sections in the WebFlux
chapter. Here the links point to a small section comparing the Servlet
Async API to the WebFlux stack from a high perspective.

In this commit we eliminate most of these links, except at the
beginning of the Asynchronous section. We also add a small mention of
the Servlet configuration in the comparison paragraphs, since the
Configuring section is the one furthest from the comparison paragraphs
that used to have a link to it.

Closes gh-29694
2022-12-17 13:43:04 +01:00
Simon Baslé b51a14ff92 Remove statement that users could be on Java < 5
Closes gh-29694
2022-12-17 13:42:59 +01:00
Simon Baslé 929d5567b8 Remove ref to JOTM, inactive since 2009
Closes gh-29694
2022-12-17 13:42:55 +01:00
Simon Baslé beaa7ba0e3 Fix link to Jakarta Mail
Closes gh-29694
2022-12-17 13:42:50 +01:00
Simon Baslé 1a839f5103 Fix typos in reference manual
Closes gh-29694
2022-12-17 13:42:46 +01:00
Sam Brannen 8df4f927fd Polish contribution
See gh-29697
2022-12-17 11:10:44 +01:00
Simon Baslé 5965917d16 Extract ResourceEntityResolver HTTPS schema resolution fallback
This commit extracts the DTD/XSD remote lookup fallback from the
resolveEntity() method into a protected method.

A WARN-level logging statement is added to the extracted fallback in
order to make it clear that remote lookup happened.

Overriding the protected method would allow users to avoid this
fallback entirely if it isn't desirable, without the need to duplicate
the local resolution code.

Closes gh-29697
2022-12-17 10:34:15 +01:00
Sam Brannen 57cfb94f1f Improve documentation for literals in SpEL expressions
Closes gh-29700
2022-12-16 14:50:07 +01:00
Sam Brannen b5b80d0a07 Polishing 2022-12-16 14:50:02 +01:00
Sam Brannen e5cfbae3fd Polishing 2022-12-16 13:48:45 +01:00
Carlos Belizón 5d2ca11315 Fix manipulating property sources example in Javadoc for ConfigurableEnvironment
The "manipulating property sources" example in the Javadoc for
`ConfigurableEnvironment` states that `MutablePropertySources`
expect a `Map<String,String>`; whereas it expects a
`Map<String,Object>`.

Closes gh-29693
2022-12-16 13:47:40 +01:00
Spring Builds 858394b720 Next development version (v6.0.4-SNAPSHOT) 2022-12-15 09:12:22 +00:00
124 changed files with 1691 additions and 706 deletions
+3 -3
View File
@@ -78,7 +78,7 @@ configure([rootProject] + javaProjects) { project ->
}
checkstyle {
toolVersion = "10.5.0"
toolVersion = "10.6.0"
configDirectory.set(rootProject.file("src/checkstyle"))
}
@@ -130,9 +130,9 @@ configure([rootProject] + javaProjects) { project ->
// TODO Uncomment link to JUnit 5 docs once we have sorted out
// the following warning in the build.
//
// warning: The code being documented uses packages in the unnamed module, but the packages defined in https://junit.org/junit5/docs/5.9.1/api/ are in named modules.
// warning: The code being documented uses packages in the unnamed module, but the packages defined in https://junit.org/junit5/docs/5.9.2/api/ are in named modules.
//
// "https://junit.org/junit5/docs/5.9.1/api/",
// "https://junit.org/junit5/docs/5.9.2/api/",
"https://www.reactive-streams.org/reactive-streams-1.0.3-javadoc/",
"https://javadoc.io/static/io.rsocket/rsocket-core/1.1.1/",
"https://r2dbc.io/spec/1.0.0.RELEASE/api/",
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -40,7 +40,6 @@ class TestConventions {
}
private void configureTestConventions(Project project) {
project.getPlugins().apply(TestRetryPlugin.class);
project.getTasks().withType(Test.class,
(test) -> project.getPlugins().withType(TestRetryPlugin.class, (testRetryPlugin) -> {
TestRetryTaskExtension testRetry = test.getExtensions().getByType(TestRetryTaskExtension.class);
@@ -23,7 +23,7 @@ Spring's pointcut model enables pointcut reuse independent of advice types. You
target different advice with the same pointcut.
The `org.springframework.aop.Pointcut` interface is the central interface, used to
target advices to particular classes and methods. The complete interface follows:
target advice to particular classes and methods. The complete interface follows:
[source,java,indent=0,subs="verbatim,quotes"]
----
@@ -843,7 +843,7 @@ created by the implementation of the `getObject()` method in the `ProxyFactoryBe
method creates an AOP proxy that wraps a target object.
One of the most important benefits of using a `ProxyFactoryBean` or another IoC-aware
class to create AOP proxies is that advices and pointcuts can also be
class to create AOP proxies is that advice and pointcuts can also be
managed by IoC. This is a powerful feature, enabling certain approaches that are hard to
achieve with other AOP frameworks. For example, an advice may itself reference
application objects (besides the target, which should be available in any AOP
@@ -901,7 +901,7 @@ to be applied. You can find an example of using this feature in <<aop-global-adv
* singleton: Whether or not the factory should return a single object, no matter how
often the `getObject()` method is called. Several `FactoryBean` implementations offer
such a method. The default value is `true`. If you want to use stateful advice - for
example, for stateful mixins - use prototype advices along with a singleton value of
example, for stateful mixins - use prototype advice along with a singleton value of
`false`.
@@ -965,7 +965,7 @@ Consider a simple example of `ProxyFactoryBean` in action. This example involves
the example.
* An `Advisor` and an `Interceptor` used to provide advice.
* An AOP proxy bean definition to specify the target object (the `personTarget` bean),
the interfaces to proxy, and the advices to apply.
the interfaces to proxy, and the advice to apply.
The following listing shows the example:
@@ -1239,7 +1239,7 @@ The first step is to construct an object of type
object, as in the preceding example, or specify the interfaces to be proxied in an alternate
constructor.
You can add advices (with interceptors as a specialized kind of advice), advisors, or both
You can add advice (with interceptors as a specialized kind of advice), advisors, or both
and manipulate them for the life of the `ProxyFactory`. If you add an
`IntroductionInterceptionAroundAdvisor`, you can cause the proxy to implement additional
interfaces.
@@ -1468,7 +1468,7 @@ Using this mechanism involves:
* Specifying a `DefaultAdvisorAutoProxyCreator` bean definition.
* Specifying any number of advisors in the same or related contexts. Note that these
must be advisors, not interceptors or other advices. This is necessary,
must be advisors, not interceptors or other advice. This is necessary,
because there must be a pointcut to evaluate, to check the eligibility of each advice
to candidate bean definitions.
@@ -1731,7 +1731,7 @@ of target source, as the following example shows:
NOTE: `ThreadLocal` instances come with serious issues (potentially resulting in memory leaks) when
incorrectly using them in multi-threaded and multi-classloader environments. You
should always consider wrapping a threadlocal in some other class and never directly use
should always consider wrapping a `ThreadLocal` in some other class and never directly use
the `ThreadLocal` itself (except in the wrapper class). Also, you should
always remember to correctly set and unset (where the latter simply involves a call to
`ThreadLocal.set(null)`) the resource local to the thread. Unsetting should be done in
@@ -2680,7 +2680,7 @@ following:
[[aop-schema-instatiation-models]]
[[aop-schema-instantiation-models]]
=== Aspect Instantiation Models
The only supported instantiation model for schema-defined aspects is the singleton
@@ -2920,8 +2920,7 @@ AspectJ. You also need to use AspectJ if you wish to advise join points other th
simple method executions (for example, field get or set join points and so on).
When you use AspectJ, you have the choice of the AspectJ language syntax (also known as
the "`code style`") or the @AspectJ annotation style. Clearly, if you do not use Java
5+, the choice has been made for you: Use the code style. If aspects play a large
the "`code style`") or the @AspectJ annotation style. If aspects play a large
role in your design, and you are able to use the https://www.eclipse.org/ajdt/[AspectJ
Development Tools (AJDT)] plugin for Eclipse, the AspectJ language syntax is the
preferred option. It is cleaner and simpler because the language was purposefully
@@ -4150,7 +4149,7 @@ The following example specifies a `ReflectiveLoadTimeWeaver`:
}
----
If you use XML-based configuration, you can specify the fully qualified classname
If you use XML-based configuration, you can specify the fully qualified class name
as the value of the `weaver-class` attribute on the `<context:load-time-weaver/>`
element. Again, the following example specifies a `ReflectiveLoadTimeWeaver`:
@@ -72,7 +72,7 @@ These are:
* `SmartInstantiationAwareBeanPostProcessor` implementations determine a more precise bean type if necessary.
This makes sure to create any proxy that will be required at runtime.
One this part completes, the `BeanFactory` contains the bean definitions that are necessary for the application to run. It does not trigger bean instantiation but allows the AOT engine to inspect the beans that will be created at runtime.
Once this part completes, the `BeanFactory` contains the bean definitions that are necessary for the application to run. It does not trigger bean instantiation but allows the AOT engine to inspect the beans that will be created at runtime.
[[core.aot.bean-factory-initialization-contributions]]
== Bean Factory Initialization AOT Contributions
@@ -1069,7 +1069,7 @@ The following listing shows the `Component` class:
private String name;
private List<Component> components = new ArrayList<Component> ();
// mmm, there is no setter method for the 'components'
// there is no setter method for the 'components'
public void addComponent(Component component) {
this.components.add(component);
}
@@ -1099,7 +1099,7 @@ The following listing shows the `Component` class:
var name: String? = null
private val components = ArrayList<Component>()
// mmm, there is no setter method for the 'components'
// there is no setter method for the 'components'
fun addComponent(component: Component) {
this.components.add(component)
}
@@ -605,7 +605,7 @@ creating a namespace), yet they refer to the same bean.
.Java-configuration
****
If you use Javaconfiguration, the `@Bean` annotation can be used to provide aliases.
If you use Java Configuration, the `@Bean` annotation can be used to provide aliases.
See <<beans-java-bean-annotation>> for details.
****
@@ -3981,7 +3981,7 @@ dependency type. The following table summarizes the most important `Aware` inter
| <<aop-aj-ltw>>
| `MessageSourceAware`
| Configured strategy for resolving messages (with support for parametrization and
| Configured strategy for resolving messages (with support for parameterization and
internationalization).
| <<context-introduction>>
@@ -5020,7 +5020,7 @@ through Java 8's `java.util.Optional`, as the following example shows:
As of Spring Framework 5.0, you can also use a `@Nullable` annotation (of any kind
in any package -- for example, `javax.annotation.Nullable` from JSR-305) or just leverage
Kotlin builtin null-safety support:
Kotlin built-in null-safety support:
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
@@ -6324,7 +6324,7 @@ the `@RestController` annotation from Spring MVC is composed of `@Controller` an
In addition, composed annotations can optionally redeclare attributes from
meta-annotations to allow customization. This can be particularly useful when you
want to only expose a subset of the meta-annotation's attributes. For example, Spring's
`@SessionScope` annotation hardcodes the scope name to `session` but still allows
`@SessionScope` annotation hard codes the scope name to `session` but still allows
customization of the `proxyMode`. The following listing shows the definition of the
`SessionScope` annotation:
@@ -744,25 +744,42 @@ topics:
[[expressions-ref-literal]]
=== Literal Expressions
The types of literal expressions supported are strings, numeric values (int, real, hex),
boolean, and null. Strings are delimited by single quotation marks. To put a single quotation mark itself
in a string, use two single quotation mark characters.
SpEL supports the following types of literal expressions.
The following listing shows simple usage of literals. Typically, they are not used
in isolation like this but, rather, as part of a more complex expression -- for example,
using a literal on one side of a logical comparison operator.
- strings
- numeric values: integer (`int` or `long`), hexadecimal (`int` or `long`), real (`float`
or `double`)
- boolean values: `true` or `false`
- null
Strings can delimited by single quotation marks (`'`) or double quotation marks (`"`). To
include a single quotation mark within a string literal enclosed in single quotation
marks, use two adjacent single quotation mark characters. Similarly, to include a double
quotation mark within a string literal enclosed in double quotation marks, use two
adjacent double quotation mark characters.
Numbers support the use of the negative sign, exponential notation, and decimal points.
By default, real numbers are parsed by using `Double.parseDouble()`.
The following listing shows simple usage of literals. Typically, they are not used in
isolation like this but, rather, as part of a more complex expression -- for example,
using a literal on one side of a logical comparison operator or as an argument to a
method.
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
----
ExpressionParser parser = new SpelExpressionParser();
// evals to "Hello World"
// evaluates to "Hello World"
String helloWorld = (String) parser.parseExpression("'Hello World'").getValue();
// evaluates to "Tony's Pizza"
String pizzaParlor = (String) parser.parseExpression("'Tony''s Pizza'").getValue();
double avogadrosNumber = (Double) parser.parseExpression("6.0221415E+23").getValue();
// evals to 2147483647
// evaluates to 2147483647
int maxValue = (Integer) parser.parseExpression("0x7FFFFFFF").getValue();
boolean trueValue = (Boolean) parser.parseExpression("true").getValue();
@@ -774,12 +791,15 @@ using a literal on one side of a logical comparison operator.
----
val parser = SpelExpressionParser()
// evals to "Hello World"
// evaluates to "Hello World"
val helloWorld = parser.parseExpression("'Hello World'").value as String
// evaluates to "Tony's Pizza"
val pizzaParlor = parser.parseExpression("'Tony''s Pizza'").value as String
val avogadrosNumber = parser.parseExpression("6.0221415E+23").value as Double
// evals to 2147483647
// evaluates to 2147483647
val maxValue = parser.parseExpression("0x7FFFFFFF").value as Int
val trueValue = parser.parseExpression("true").value as Boolean
@@ -787,9 +807,6 @@ using a literal on one side of a logical comparison operator.
val nullValue = parser.parseExpression("null").value
----
Numbers support the use of the negative sign, exponential notation, and decimal points.
By default, real numbers are parsed by using `Double.parseDouble()`.
[[expressions-properties-arrays]]
@@ -804,7 +821,7 @@ Pupin's city of birth, we use the following expressions:
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
----
// evals to 1856
// evaluates to 1856
int year = (Integer) parser.parseExpression("birthdate.year + 1900").getValue(context);
String city = (String) parser.parseExpression("placeOfBirth.city").getValue(context);
@@ -812,7 +829,7 @@ Pupin's city of birth, we use the following expressions:
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
// evals to 1856
// evaluates to 1856
val year = parser.parseExpression("birthdate.year + 1900").getValue(context) as Int
val city = parser.parseExpression("placeOfBirth.city").getValue(context) as String
@@ -129,7 +129,7 @@ Typically, you need an application server's JTA capability only if your applicat
to handle transactions across multiple resources, which is not a requirement for many
applications. Many high-end applications use a single, highly scalable database (such as
Oracle RAC) instead. Stand-alone transaction managers (such as
https://www.atomikos.com/[Atomikos Transactions] and https://jotm.ow2.org/[JOTM])
https://www.atomikos.com/[Atomikos Transactions])
are other options. Of course, you may need other application server capabilities, such as
Java Message Service (JMS) and Jakarta EE Connector Architecture (JCA).
@@ -1090,8 +1090,8 @@ application-specific `Exception` type by supplying an _exception pattern_ via th
----
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="get*" read-only="true" rollback-for="NoProductInStockException"/>
<tx:method name="*"/>
<tx:method name="get*" read-only="true" rollback-for="NoProductInStockException"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
----
@@ -1105,8 +1105,8 @@ unhandled `InstrumentNotFoundException`:
----
<tx:advice id="txAdvice">
<tx:attributes>
<tx:method name="updateStock" no-rollback-for="InstrumentNotFoundException"/>
<tx:method name="*"/>
<tx:method name="updateStock" no-rollback-for="InstrumentNotFoundException"/>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
----
@@ -1121,7 +1121,7 @@ attendant transaction:
----
<tx:advice id="txAdvice">
<tx:attributes>
<tx:method name="*" rollback-for="Throwable" no-rollback-for="InstrumentNotFoundException"/>
<tx:method name="*" rollback-for="Throwable" no-rollback-for="InstrumentNotFoundException"/>
</tx:attributes>
</tx:advice>
----
@@ -4188,7 +4188,7 @@ To configure a `DriverManagerDataSource`:
. Obtain a connection with `DriverManagerDataSource` as you typically obtain a JDBC
connection.
. Specify the fully qualified classname of the JDBC driver so that the `DriverManager`
. Specify the fully qualified class name of the JDBC driver so that the `DriverManager`
can load the driver class.
. Provide a URL that varies between JDBC drivers. (See the documentation for your driver
for the correct value.)
@@ -4346,7 +4346,7 @@ javadoc for more details.
==== Using `DataSourceTransactionManager`
The `DataSourceTransactionManager` class is a `PlatformTransactionManager`
implementation for single JDBC datasources. It binds a JDBC connection from the
implementation for single JDBC data sources. It binds a JDBC connection from the
specified data source to the currently executing thread, potentially allowing for one
thread connection per data source.
@@ -6825,7 +6825,7 @@ The following query uses a bind variable:
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
val first = client.sql("SELECT id, name FROM person WHERE WHERE first_name = :fn")
val first = client.sql("SELECT id, name FROM person WHERE first_name = :fn")
.bind("fn", "Joe")
.fetch().awaitSingle()
----
@@ -6850,7 +6850,7 @@ Without specifying further mapping details, queries return tabular results
as `Map` whose keys are case-insensitive column names that map to their column value.
You can take control over result mapping by supplying a `Function<Row, T>` that gets
called for each `Row` so it can can return arbitrary values (singular values,
called for each `Row` so it can return arbitrary values (singular values,
collections and maps, and objects).
The following example extracts the `name` column and emits its value:
@@ -6966,7 +6966,7 @@ Consider the following query:
SELECT id, name, state FROM table WHERE (name, age) IN (('John', 35), ('Ann', 50))
----
The preceding query can be parametrized and run as follows:
The preceding query can be parameterized and run as follows:
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
@@ -7212,7 +7212,7 @@ responsibility of the administrator who sets up the `ConnectionFactory`. You
most likely fill both roles as you develop and test code, but you do not
necessarily have to know how the production data source is configured.
When you use Spring's R2DBC layer, you can can configure your own with a
When you use Spring's R2DBC layer, you can configure your own with a
connection pool implementation provided by a third party. A popular
implementation is R2DBC Pool (`r2dbc-pool`). Implementations in the Spring
distribution are meant only for testing purposes and do not provide pooling.
@@ -7286,7 +7286,7 @@ javadoc for more details.
==== Using `R2dbcTransactionManager`
The `R2dbcTransactionManager` class is a `ReactiveTransactionManager` implementation for
single R2DBC datasources. It binds an R2DBC connection from the specified connection factory
single R2DBC data sources. It binds an R2DBC connection from the specified connection factory
to the subscriber `Context`, potentially allowing for one subscriber connection for each
connection factory.
@@ -8636,7 +8636,7 @@ given `javax.xml.transform.Result`. The result is a tagging interface that basic
represents an XML output abstraction. Concrete implementations wrap various XML
representations, as the following table indicates:
[[oxm-marshller-tbl]]
[[oxm-marshaller-tbl]]
|===
| Result implementation| Wraps XML representation
@@ -8679,7 +8679,7 @@ This interface also has one method, which reads from the given
with `Result`, `Source` is a tagging interface that has three concrete implementations. Each
wraps a different XML representation, as the following table indicates:
[[oxm-unmarshller-tbl]]
[[oxm-unmarshaller-tbl]]
|===
| Source implementation| Wraps XML representation
@@ -140,9 +140,9 @@ Since caches are essentially key-value stores, each invocation of a cached metho
needs to be translated into a suitable key for cache access. The caching abstraction
uses a simple `KeyGenerator` based on the following algorithm:
* If no params are given, return `SimpleKey.EMPTY`.
* If only one param is given, return that instance.
* If more than one param is given, return a `SimpleKey` that contains all parameters.
* If no parameters are given, return `SimpleKey.EMPTY`.
* If only one parameter is given, return that instance.
* If more than one parameter is given, return a `SimpleKey` that contains all parameters.
This approach works well for most use-cases, as long as parameters have natural keys
and implement valid `hashCode()` and `equals()` methods. If that is not the case,
@@ -5,14 +5,15 @@ This section describes how to send email with the Spring Framework.
.Library dependencies
****
The following JAR needs to be on the classpath of your application in order to use
the Spring Framework's email library:
The following JAR needs to be on the classpath of your application in order to use the
Spring Framework's email support:
* The https://eclipse-ee4j.github.io/mail/[JavaMail / Jakarta Mail 1.6] library
* The https://jakartaee.github.io/mail-api/[Jakarta Mail] library
This library is freely available on the web -- for example, in Maven Central as
`com.sun.mail:jakarta.mail`. Please make sure to use the latest 1.6.x version
rather than Jakarta Mail 2.0 (which comes with a different package namespace).
`com.sun.mail:jakarta.mail`. Please make sure to use the latest 2.x version (which uses
the `jakarta.mail` package namespace) rather than Jakarta Mail 1.6.x (which uses the
`javax.mail` package namespace).
****
The Spring Framework provides a helpful utility library for sending email that shields
@@ -215,7 +215,7 @@ converters to use explicitly.
[[rest-message-conversion]]
==== Message Conversion
[.small]#<<web-reactive.adoc#webflux-codecs, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-codecs, See equivalent in the Reactive stack>>#
The `spring-web` module contains the `HttpMessageConverter` contract for reading and
writing the body of HTTP requests and responses through `InputStream` and `OutputStream`.
@@ -417,7 +417,7 @@ method parameters:
| Dynamically set the HTTP method for the request, overriding the annotation's `method` attribute
| `@RequestHeader`
| Add a request header or mutliple headers. The argument may be a `Map<String, ?>` or
| Add a request header or multiple headers. The argument may be a `Map<String, ?>` or
`MultiValueMap<String, ?>` with multiple headers, a `Collection<?>` of values, or an
individual value. Type conversion is supported for non-String values.
@@ -432,7 +432,7 @@ method parameters:
through the configured `ReactiveAdapterRegistry`.
| `@RequestParam`
| Add a request parameter or mutliple parameters. The argument may be a `Map<String, ?>`
| Add a request parameter or multiple parameters. The argument may be a `Map<String, ?>`
or `MultiValueMap<String, ?>` with multiple parameters, a `Collection<?>` of values, or
an individual value. Type conversion is supported for non-String values.
@@ -446,7 +446,7 @@ method parameters:
a Spring `Part`, or Reactive Streams `Publisher` of any of the above.
| `@CookieValue`
| Add a cookie or mutliple cookies. The argument may be a `Map<String, ?>` or
| Add a cookie or multiple cookies. The argument may be a `Map<String, ?>` or
`MultiValueMap<String, ?>` with multiple cookies, a `Collection<?>` of values, or an
individual value. Type conversion is supported for non-String values.
@@ -9,7 +9,7 @@ implementations behind the common interfaces abstracts away the differences betw
SE 5, Java SE 6, and Jakarta EE environments.
Spring also features integration classes to support scheduling with the `Timer`
(part of the JDK since 1.3) and the Quartz Scheduler ( https://www.quartz-scheduler.org/[]).
(part of the JDK since 1.3) and the https://www.quartz-scheduler.org/[Quartz Scheduler].
You can set up both of those schedulers by using a `FactoryBean` with optional references to
`Timer` or `Trigger` instances, respectively. Furthermore, a convenience class for both
the Quartz Scheduler and the `Timer` is available that lets you invoke a method of
@@ -171,7 +171,7 @@ much more flexible.
The `Trigger` interface is essentially inspired by JSR-236 which, as of Spring 3.0,
was not yet officially implemented. The basic idea of the `Trigger` is that execution
times may be determined based on past execution outcomes or even arbitrary conditions.
If these determinations do take into account the outcome of the preceding execution,
If these determinations take into account the outcome of the preceding execution,
that information is available within a `TriggerContext`. The `Trigger` interface itself
is quite simple, as the following listing shows:
@@ -179,7 +179,7 @@ is quite simple, as the following listing shows:
----
public interface Trigger {
Date nextExecutionTime(TriggerContext triggerContext);
Instant nextExecution(TriggerContext triggerContext);
}
----
@@ -192,11 +192,11 @@ default). The following listing shows the available methods for `Trigger` implem
----
public interface TriggerContext {
Date lastScheduledExecutionTime();
Instant lastScheduledExecution();
Date lastActualExecutionTime();
Instant lastActualExecution();
Date lastCompletionTime();
Instant lastCompletion();
}
----
@@ -208,7 +208,7 @@ Spring provides two implementations of the `Trigger` interface. The most interes
is the `CronTrigger`. It enables the scheduling of tasks based on
<<scheduling-cron-expression,cron expressions>>.
For example, the following task is scheduled to run 15 minutes past each hour but only
during the 9-to-5 "`business hours`" on weekdays:
during the 9-to-5 "business hours" on weekdays:
[source,java,indent=0]
[subs="verbatim"]
@@ -782,9 +782,9 @@ You can use these macros instead of the six-digit value, thus: `@Scheduled(cron
== Using the Quartz Scheduler
Quartz uses `Trigger`, `Job`, and `JobDetail` objects to realize scheduling of all kinds
of jobs. For the basic concepts behind Quartz, see
https://www.quartz-scheduler.org/[]. For convenience purposes, Spring offers a couple of
classes that simplify using Quartz within Spring-based applications.
of jobs. For the basic concepts behind Quartz, see the
https://www.quartz-scheduler.org/[Quartz Web site]. For convenience purposes, Spring
offers a couple of classes that simplify using Quartz within Spring-based applications.
[[scheduling-quartz-jobdetail]]
@@ -882,12 +882,13 @@ that merely invoke a method. You need only create the actual business object and
wire up the detail object.
By default, Quartz Jobs are stateless, resulting in the possibility of jobs interfering
with each other. If you specify two triggers for the same `JobDetail`, it is
possible that, before the first job has finished, the second one starts. If
`JobDetail` classes implement the `Stateful` interface, this does not happen. The second
job does not start before the first one has finished. To make jobs resulting from the
`MethodInvokingJobDetailFactoryBean` be non-concurrent, set the `concurrent` flag to
`false`, as the following example shows:
with each other. If you specify two triggers for the same `JobDetail`, it is possible
that the second one starts before the first job has finished. If `JobDetail` classes
implement the `Stateful` interface, this does not happen: the second job does not start
before the first one has finished.
To make jobs resulting from the `MethodInvokingJobDetailFactoryBean` be non-concurrent,
set the `concurrent` flag to `false`, as the following example shows:
[source,xml,indent=0,subs="verbatim,quotes"]
----
@@ -117,6 +117,55 @@ logic but without running a server. The following example shows how to do so:
// Test code that uses the above RestTemplate ...
----
In some cases it may be necessary to perform an actual call to a remote service instead
of mocking the response. The following example shows how to do that through
`ExecutingResponseCreator`:
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
.Java
----
RestTemplate restTemplate = new RestTemplate();
// Create ExecutingResponseCreator with the original request factory
ExecutingResponseCreator withActualResponse = new ExecutingResponseCreator(restTemplate.getRequestFactory());
MockRestServiceServer mockServer = MockRestServiceServer.bindTo(restTemplate).build();
mockServer.expect(requestTo("/profile")).andRespond(withSuccess());
mockServer.expect(requestTo("/quoteOfTheDay")).andRespond(withActualResponse);
// Test code that uses the above RestTemplate ...
mockServer.verify();
----
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
val restTemplate = RestTemplate()
// Create ExecutingResponseCreator with the original request factory
val withActualResponse = new ExecutingResponseCreator(restTemplate.getRequestFactory())
val mockServer = MockRestServiceServer.bindTo(restTemplate).build()
mockServer.expect(requestTo("/profile")).andRespond(withSuccess())
mockServer.expect(requestTo("/quoteOfTheDay")).andRespond(withActualResponse)
// Test code that uses the above RestTemplate ...
mockServer.verify()
----
In the preceding example, we create the `ExecutingResponseCreator` using the
`ClientHttpRequestFactory` from the `RestTemplate` _before_ `MockRestServiceServer` replaces
it with a different one that mocks responses.
Then we define expectations with two kinds of responses:
* a stub `200` response for the `/profile` endpoint (no actual request will be executed)
* a response obtained through a call to the `/quoteOfTheDay` endpoint
In the second case, the request is executed through the `ClientHttpRequestFactory` that was
captured earlier. This generates a response that could e.g. come from an actual remote server,
depending on how the `RestTemplate` was originally configured.
[[spring-mvc-test-client-static-imports]]
== Static Imports
@@ -445,6 +445,17 @@ all failures will be tracked and reported.
content().contentType("application/json;charset=UTF-8"));
----
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
import org.springframework.test.web.servlet.get
mockMvc.get("/accounts/1").andExpectAll {
status { isOk() }
content { contentType(APPLICATION_JSON) }
}
----
`MockMvcResultMatchers.*` provides a number of expectations, some of which are further
nested with more detailed expectations.
@@ -961,7 +972,7 @@ leads to a number of additional challenges:
* Testing can become slow, since each test would need to ensure that the database is in
the correct state.
* Since our database needs to be in a specific state, we cannot run tests in parallel.
* Performing assertions on such items as auto-generated ids, timestamps, and others can
* Performing assertions on such items as auto-generated IDs, timestamps, and others can
be difficult.
These challenges do not mean that we should abandon end-to-end integration testing
@@ -1,6 +1,6 @@
[[webflux-cors]]
= CORS
[.small]#<<web.adoc#mvc-cors, Web MVC>>#
[.small]#<<web.adoc#mvc-cors, See equivalent in the Servlet stack>>#
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.
[[webflux-cors-intro]]
== Introduction
[.small]#<<web.adoc#mvc-cors-intro, Web MVC>>#
[.small]#<<web.adoc#mvc-cors-intro, See equivalent in the Servlet stack>>#
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.
[[webflux-cors-processing]]
== Processing
[.small]#<<web.adoc#mvc-cors-processing, Web MVC>>#
[.small]#<<web.adoc#mvc-cors-processing, See equivalent in the Servlet stack>>#
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:
[[webflux-cors-controller]]
== `@CrossOrigin`
[.small]#<<web.adoc#mvc-cors-controller, Web MVC>>#
[.small]#<<web.adoc#mvc-cors-controller, See equivalent in the Servlet stack>>#
The {api-spring-framework}/web/bind/annotation/CrossOrigin.html[`@CrossOrigin`]
annotation enables cross-origin requests on annotated controller methods, as the
@@ -237,7 +237,7 @@ as the following example shows:
[[webflux-cors-global]]
== Global Configuration
[.small]#<<web.adoc#mvc-cors-global, Web MVC>>#
[.small]#<<web.adoc#mvc-cors-global, See equivalent in the Servlet stack>>#
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`
@@ -308,7 +308,7 @@ as the following example shows:
[[webflux-cors-webfilter]]
== CORS `WebFilter`
[.small]#<<web.adoc#mvc-cors-filter, Web MVC>>#
[.small]#<<web.adoc#mvc-cors-filter, See equivalent in the Servlet stack>>#
You can apply CORS support through the built-in
{api-spring-framework}/web/cors/reactive/CorsWebFilter.html[`CorsWebFilter`], which is a
@@ -1,6 +1,6 @@
[[webflux-fn]]
= Functional Endpoints
[.small]#<<web.adoc#webmvc-fn, Web MVC>>#
[.small]#<<web.adoc#webmvc-fn, See equivalent in the Servlet stack>>#
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.
[[webflux-fn-overview]]
== Overview
[.small]#<<web.adoc#webmvc-fn-overview, Web MVC>>#
[.small]#<<web.adoc#webmvc-fn-overview, See equivalent in the Servlet stack>>#
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>`).
@@ -113,7 +113,7 @@ Most applications can run through the WebFlux Java configuration, see <<webflux-
[[webflux-fn-handler-functions]]
== HandlerFunction
[.small]#<<web.adoc#webmvc-fn-handler-functions, Web MVC>>#
[.small]#<<web.adoc#webmvc-fn-handler-functions, See equivalent in the Servlet stack>>#
`ServerRequest` and `ServerResponse` are immutable interfaces that offer JDK 8-friendly
access to the HTTP request and response.
@@ -486,7 +486,7 @@ See <<core.adoc#validation-beanvalidation, Spring Validation>>.
[[webflux-fn-router-functions]]
== `RouterFunction`
[.small]#<<web.adoc#webmvc-fn-router-functions, Web MVC>>#
[.small]#<<web.adoc#webmvc-fn-router-functions, See equivalent in the Servlet stack>>#
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
@@ -687,7 +687,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, Web MVC>>#
[.small]#<<web.adoc#webmvc-fn-running, See equivalent in the Servlet stack>>#
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:
@@ -793,7 +793,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, Web MVC>>#
[.small]#<<web.adoc#webmvc-fn-handler-filter-function, See equivalent in the Servlet stack>>#
You can filter handler functions by using the `before`, `after`, or `filter` methods on the routing
function builder.
@@ -1,6 +1,6 @@
[[webflux-view]]
= View Technologies
[.small]#<<web.adoc#mvc-view, Web MVC>>#
[.small]#<<web.adoc#mvc-view, See equivalent in the Servlet stack>>#
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>>.
[[webflux-view-thymeleaf]]
== Thymeleaf
[.small]#<<web.adoc#mvc-view-thymeleaf, Web MVC>>#
[.small]#<<web.adoc#mvc-view-thymeleaf, See equivalent in the Servlet stack>>#
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
@@ -33,7 +33,7 @@ https://web.archive.org/web/20210623051330/http%3A//forum.thymeleaf.org/Thymelea
[[webflux-view-freemarker]]
== FreeMarker
[.small]#<<web.adoc#mvc-view-freemarker, Web MVC>>#
[.small]#<<web.adoc#mvc-view-freemarker, See equivalent in the Servlet stack>>#
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.
[[webflux-view-freemarker-contextconfig]]
=== View Configuration
[.small]#<<web.adoc#mvc-view-freemarker-contextconfig, Web MVC>>#
[.small]#<<web.adoc#mvc-view-freemarker-contextconfig, See equivalent in the Servlet stack>>#
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
[[webflux-views-freemarker]]
=== FreeMarker Configuration
[.small]#<<web.adoc#mvc-views-freemarker, Web MVC>>#
[.small]#<<web.adoc#mvc-views-freemarker, See equivalent in the Servlet stack>>#
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.
[[webflux-view-freemarker-forms]]
=== Form Handling
[.small]#<<web.adoc#mvc-view-freemarker-forms, Web MVC>>#
[.small]#<<web.adoc#mvc-view-freemarker-forms, See equivalent in the Servlet stack>>#
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
[[webflux-view-bind-macros]]
==== The Bind Macros
[.small]#<<web.adoc#mvc-view-bind-macros, Web MVC>>#
[.small]#<<web.adoc#mvc-view-bind-macros, See equivalent in the Servlet stack>>#
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.
[[webflux-view-script]]
== Script Views
[.small]#<<web.adoc#mvc-view-script, Web MVC>>#
[.small]#<<web.adoc#mvc-view-script, See equivalent in the Servlet stack>>#
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
[[webflux-view-script-dependencies]]
=== Requirements
[.small]#<<web.adoc#mvc-view-script-dependencies, Web MVC>>#
[.small]#<<web.adoc#mvc-view-script-dependencies, See equivalent in the Servlet stack>>#
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].
[[webflux-view-script-integrate]]
=== Script Templates
[.small]#<<web.adoc#mvc-view-script-integrate, Web MVC>>#
[.small]#<<web.adoc#mvc-view-script-integrate, See equivalent in the Servlet stack>>#
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.
[[webflux-view-httpmessagewriter]]
== JSON and XML
[.small]#<<web.adoc#mvc-view-jackson, Web MVC>>#
[.small]#<<web.adoc#mvc-view-jackson, See equivalent in the Servlet stack>>#
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),
@@ -1196,7 +1196,7 @@ response individually, and instead wait for the combined result:
The above is merely one example. There are lots of other patterns and operators for putting
together a reactive pipeline that makes many remote calls, potentially some nested,
inter-dependent, without ever blocking until the end.
interdependent, without ever blocking until the end.
[NOTE]
====
@@ -1,6 +1,6 @@
[[webflux-websocket]]
= WebSockets
[.small]#<<web.adoc#websocket, Same as in the Servlet stack>>#
[.small]#<<web.adoc#websocket, See equivalent in the Servlet stack>>#
This part of the reference documentation covers support for reactive-stack WebSocket
messaging.
@@ -12,7 +12,7 @@ include::websocket-intro.adoc[leveloffset=+1]
[[webflux-websocket-server]]
== WebSocket API
[.small]#<<web.adoc#websocket-server, Same as in the Servlet stack>>#
[.small]#<<web.adoc#websocket-server, See equivalent in the Servlet stack>>#
The Spring Framework provides a WebSocket API that you can use to write client- and
server-side applications that handle WebSocket messages.
@@ -21,7 +21,7 @@ server-side applications that handle WebSocket messages.
[[webflux-websocket-server-handler]]
=== Server
[.small]#<<web.adoc#websocket-server-handler, Same as in the Servlet stack>>#
[.small]#<<web.adoc#websocket-server-handler, See equivalent in the Servlet stack>>#
To create a WebSocket server, you can first create a `WebSocketHandler`.
The following example shows how to do so:
@@ -339,7 +339,7 @@ subsequently use `DataBufferUtils.release(dataBuffer)` when the buffers are cons
[[webflux-websocket-server-handshake]]
=== Handshake
[.small]#<<web.adoc#websocket-server-handshake, Same as in the Servlet stack>>#
[.small]#<<web.adoc#websocket-server-handshake, See equivalent in the Servlet stack>>#
`WebSocketHandlerAdapter` delegates to a `WebSocketService`. By default, that is an instance
of `HandshakeWebSocketService`, which performs basic checks on the WebSocket request and
@@ -354,7 +354,7 @@ into the attributes of the `WebSocketSession`.
[[webflux-websocket-server-config]]
=== Server Configuration
[.small]#<<web.adoc#websocket-server-runtime-configuration, Same as in the Servlet stack>>#
[.small]#<<web.adoc#websocket-server-runtime-configuration, See equivalent in the Servlet stack>>#
The `RequestUpgradeStrategy` for each server exposes configuration specific to the
underlying WebSocket server engine. When using the WebFlux Java config you can customize
@@ -408,7 +408,7 @@ only Tomcat and Jetty expose such options.
[[webflux-websocket-server-cors]]
=== CORS
[.small]#<<web.adoc#websocket-server-allowed-origins, Same as in the Servlet stack>>#
[.small]#<<web.adoc#websocket-server-allowed-origins, See equivalent in the Servlet stack>>#
The easiest way to configure CORS and restrict access to a WebSocket endpoint is to
have your `WebSocketHandler` implement `CorsConfigurationSource` and return a
@@ -248,20 +248,22 @@ current thread (and rely on callbacks instead) means that you do not need extra
there are no blocking calls to absorb.
==== Invoking a Blocking API
.Invoking a Blocking API
What if you do need to use a blocking library? Both Reactor and RxJava provide the
`publishOn` operator to continue processing on a different thread. That means there is an
easy escape hatch. Keep in mind, however, that blocking APIs are not a good fit for
this concurrency model.
.Mutable State
==== Mutable State
In Reactor and RxJava, you declare logic through operators. At runtime, a reactive
pipeline is formed where data is processed sequentially, in distinct stages. A key benefit
of this is that it frees applications from having to protect mutable state because
application code within that pipeline is never invoked concurrently.
.Threading Model
==== Threading Model
What threads should you expect to see on a server running with Spring WebFlux?
* On a "`vanilla`" Spring WebFlux server (for example, no data access nor other optional
@@ -285,7 +287,8 @@ specific thread pool `Scheduler` strategy.
* Data access libraries and other third party dependencies can also create and use threads
of their own.
.Configuring
==== Configuring
The Spring Framework does not provide support for starting and stopping
<<webflux-server-choice, servers>>. To configure the threading model for a server,
you need to use server-specific configuration APIs, or, if you use Spring Boot,
@@ -594,7 +597,7 @@ The `DefaultServerWebExchange` uses the configured `HttpMessageReader` to parse
[[webflux-multipart]]
==== Multipart Data
[.small]#<<web.adoc#mvc-multipart, Web MVC>>#
[.small]#<<web.adoc#mvc-multipart, See equivalent in the Servlet stack>>#
`ServerWebExchange` exposes the following method for accessing multipart data:
@@ -628,7 +631,7 @@ collecting to a `MultiValueMap`.
[[webflux-forwarded-headers]]
==== Forwarded Headers
[.small]#<<web.adoc#filters-forwarded-headers, Web MVC>>#
[.small]#<<web.adoc#filters-forwarded-headers, See equivalent in the Servlet stack>>#
As a request goes through proxies (such as load balancers), the host, port, and
scheme may change. That makes it a challenge, from a client perspective, to create links that point to the correct
@@ -659,7 +662,7 @@ filters, and `ForwardedHeaderTransformer` is used instead.
[[webflux-filters]]
=== Filters
[.small]#<<web.adoc#filters, Web MVC>>#
[.small]#<<web.adoc#filters, See equivalent in the Servlet stack>>#
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
@@ -670,7 +673,7 @@ the bean declaration or by implementing `Ordered`.
[[webflux-filters-cors]]
==== CORS
[.small]#<<web.adoc#filters-cors, Web MVC>>#
[.small]#<<web.adoc#filters-cors, See equivalent in the Servlet stack>>#
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
@@ -681,7 +684,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, Web MVC>>#
[.small]#<<web.adoc#mvc-ann-customer-servlet-container-error-page, See equivalent in the Servlet stack>>#
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
@@ -712,7 +715,7 @@ The following table describes the available `WebExceptionHandler` implementation
[[webflux-codecs]]
=== Codecs
[.small]#<<integration.adoc#rest-message-conversion, Web MVC>>#
[.small]#<<integration.adoc#rest-message-conversion, See equivalent in the Servlet stack>>#
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
@@ -807,7 +810,7 @@ another `HttpMessageReader` for the actual parsing to a `Flux<Part>` and then si
collects the parts into a `MultiValueMap`.
By default, the `DefaultPartHttpMessageReader` is used, but this can be changed through the
`ServerCodecConfigurer`.
For more information about the `DefaultPartHttpMessageReader`, refer to to the
For more information about the `DefaultPartHttpMessageReader`, refer to the
{api-spring-framework}/http/codec/multipart/DefaultPartHttpMessageReader.html[javadoc of `DefaultPartHttpMessageReader`].
On the server side where multipart form content may need to be accessed from multiple
@@ -852,7 +855,7 @@ To configure all three in WebFlux, you'll need to supply a pre-configured instan
[[webflux-codecs-streaming]]
==== Streaming
[.small]#<<web.adoc#mvc-ann-async-http-streaming, Web MVC>>#
[.small]#<<web.adoc#mvc-ann-async-http-streaming, See equivalent in the Servlet stack>>#
When streaming to the HTTP response (for example, `text/event-stream`,
`application/x-ndjson`), it is important to send data periodically, in order to
@@ -880,7 +883,7 @@ especially the section on <<core#databuffers-using, Using DataBuffer>>.
[[webflux-logging]]
=== Logging
[.small]#<<web.adoc#mvc-logging, Web MVC>>#
[.small]#<<web.adoc#mvc-logging, See equivalent in the Servlet stack>>#
`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
@@ -912,7 +915,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, Web MVC>>#
[.small]#<<web.adoc#mvc-logging-sensitive-data, See equivalent in the Servlet stack>>#
`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.
@@ -1014,7 +1017,7 @@ The following example shows how to do so for client-side requests:
[[webflux-dispatcher-handler]]
== `DispatcherHandler`
[.small]#<<web.adoc#mvc-servlet, Web MVC>>#
[.small]#<<web.adoc#mvc-servlet, See equivalent in the Servlet stack>>#
Spring WebFlux, similarly to Spring MVC, is designed around the front controller pattern,
where a central `WebHandler`, the `DispatcherHandler`, provides a shared algorithm for
@@ -1057,7 +1060,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, Web MVC>>#
[.small]#<<web.adoc#mvc-servlet-special-bean-types, See equivalent in the Servlet stack>>#
The `DispatcherHandler` delegates to special beans to process requests and render the
appropriate responses. By "`special beans,`" we mean Spring-managed `Object` instances that
@@ -1099,7 +1102,7 @@ there are also some other beans detected at a lower level (see
[[webflux-framework-config]]
=== WebFlux Config
[.small]#<<web.adoc#mvc-servlet-config, Web MVC>>#
[.small]#<<web.adoc#mvc-servlet-config, See equivalent in the Servlet stack>>#
Applications can declare the infrastructure beans (listed under
<<webflux-web-handler-api-special-beans, Web Handler API>> and
@@ -1114,7 +1117,7 @@ many extra convenient options.
[[webflux-dispatcher-handler-sequence]]
=== Processing
[.small]#<<web.adoc#mvc-servlet-sequence, Web MVC>>#
[.small]#<<web.adoc#mvc-servlet-sequence, See equivalent in the Servlet stack>>#
`DispatcherHandler` processes requests as follows:
@@ -1165,7 +1168,7 @@ as a `HandlerResult`, along with some additional context, and passed to the firs
[[webflux-dispatcher-exceptions]]
=== Exceptions
[.small]#<<web.adoc#mvc-exceptionhandlers, Web MVC>>#
[.small]#<<web.adoc#mvc-exceptionhandlers, See equivalent in the Servlet stack>>#
`HandlerAdapter` implementations can handle internally exceptions from invoking a request
handler, such as a controller method. However, an exception may be deferred if the request
@@ -1175,7 +1178,7 @@ A `HandlerAdapter` may expose its exception handling mechanism as a
`DispatchExceptionHandler` set on the `HandlerResult` it returns. When that's set,
`DispatcherHandler` will also apply it to the handling of the result.
A `HandlerAdapter` may also choose to implement `DispatchExceptionHandler`. Inn that case
A `HandlerAdapter` may also choose to implement `DispatchExceptionHandler`. In that case
`DispatcherHandler` will apply it to exceptions that arise before a handler is mapped,
e.g. during handler mapping, or earlier, e.g. in a `WebFilter`.
@@ -1186,7 +1189,7 @@ See also <<webflux-ann-controller-exceptions>> in the "`Annotated Controller`" s
[[webflux-viewresolution]]
=== View Resolution
[.small]#<<web.adoc#mvc-viewresolver, Web MVC>>#
[.small]#<<web.adoc#mvc-viewresolver, See equivalent in the Servlet stack>>#
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
@@ -1197,7 +1200,7 @@ instance. The `View` is then used to render the response.
[[webflux-viewresolution-handling]]
==== Handling
[.small]#<<web.adoc#mvc-viewresolver-handling, Web MVC>>#
[.small]#<<web.adoc#mvc-viewresolver-handling, See equivalent in the Servlet stack>>#
The `HandlerResult` passed into `ViewResolutionResultHandler` contains the return value
from the handler and the model that contains attributes added during request
@@ -1233,7 +1236,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, Web MVC>>#
[.small]#<<web.adoc#mvc-redirecting-redirect-prefix, See equivalent in the Servlet stack>>#
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
@@ -1248,7 +1251,7 @@ operate in terms of logical view names. A view name such as
[[webflux-multiple-representations]]
==== Content Negotiation
[.small]#<<web.adoc#mvc-multiple-representations, Web MVC>>#
[.small]#<<web.adoc#mvc-multiple-representations, See equivalent in the Servlet stack>>#
`ViewResolutionResultHandler` supports content negotiation. It compares the request
media types with the media types supported by each selected `View`. The first `View`
@@ -1265,7 +1268,7 @@ always selected and used if they match the requested media type.
[[webflux-controller]]
== Annotated Controllers
[.small]#<<web.adoc#mvc-controller, Web MVC>>#
[.small]#<<web.adoc#mvc-controller, See equivalent in the Servlet stack>>#
Spring WebFlux provides an annotation-based programming model, where `@Controller` and
`@RestController` components use annotations to express request mappings, request input,
@@ -1303,7 +1306,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, Web MVC>>#
[.small]#<<web.adoc#mvc-ann-controller, See equivalent in the Servlet stack>>#
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
@@ -1347,7 +1350,7 @@ directly to the response body versus view resolution and rendering with an HTML
[[webflux-ann-requestmapping-proxying]]
==== AOP Proxies
[.small]#<<web.adoc#mvc-ann-requestmapping-proxying, Web MVC>>#
[.small]#<<web.adoc#mvc-ann-requestmapping-proxying, See equivalent in the Servlet stack>>#
In some cases, you may need to decorate a controller with an AOP proxy at runtime.
One example is if you choose to have `@Transactional` annotations directly on the
@@ -1370,7 +1373,7 @@ Please, enable class based proxying, or otherwise the interface must also have a
[[webflux-ann-requestmapping]]
=== Request Mapping
[.small]#<<web.adoc#mvc-ann-requestmapping, Web MVC>>#
[.small]#<<web.adoc#mvc-ann-requestmapping, See equivalent in the Servlet stack>>#
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
@@ -1434,7 +1437,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, Web MVC>>#
[.small]#<<web.adoc#mvc-ann-requestmapping-uri-templates, See equivalent in the Servlet stack>>#
You can map requests by using glob patterns and wildcards:
@@ -1583,7 +1586,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, Web MVC>>#
[.small]#<<web.adoc#mvc-ann-requestmapping-pattern-comparison, See equivalent in the Servlet stack>>#
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.
@@ -1598,7 +1601,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, Web MVC>>#
[.small]#<<web.adoc#mvc-ann-requestmapping-consumes, See equivalent in the Servlet stack>>#
You can narrow the request mapping based on the `Content-Type` of the request,
as the following example shows:
@@ -1633,7 +1636,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, Web MVC>>#
[.small]#<<web.adoc#mvc-ann-requestmapping-produces, See equivalent in the Servlet stack>>#
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:
@@ -1670,7 +1673,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, Web MVC>>#
[.small]#<<web.adoc#mvc-ann-requestmapping-params-and-headers, See equivalent in the Servlet stack>>#
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
@@ -1722,7 +1725,7 @@ You can also use the same with request header conditions, as the following examp
[[webflux-ann-requestmapping-head-options]]
==== HTTP HEAD, OPTIONS
[.small]#<<web.adoc#mvc-ann-requestmapping-head-options, Web MVC>>#
[.small]#<<web.adoc#mvc-ann-requestmapping-head-options, See equivalent in the Servlet stack>>#
`@GetMapping` and `@RequestMapping(method=HttpMethod.GET)` support HTTP HEAD
transparently for request mapping purposes. Controller methods need not change.
@@ -1743,7 +1746,7 @@ is not necessary in the common case.
[[webflux-ann-requestmapping-composed]]
==== Custom Annotations
[.small]#<<web.adoc#mvc-ann-requestmapping-composed, Web MVC>>#
[.small]#<<web.adoc#mvc-ann-requestmapping-composed, See equivalent in the Servlet stack>>#
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
@@ -1764,7 +1767,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, Web MVC>>#
[.small]#<<web.adoc#mvc-ann-requestmapping-registration, See equivalent in the Servlet stack>>#
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
@@ -1821,7 +1824,7 @@ under different URLs. The following example shows how to do so:
[[webflux-ann-methods]]
=== Handler Methods
[.small]#<<web.adoc#mvc-ann-methods, Web MVC>>#
[.small]#<<web.adoc#mvc-ann-methods, See equivalent in the Servlet stack>>#
`@RequestMapping` handler methods have a flexible signature and can choose from a range of
supported controller method arguments and return values.
@@ -1829,7 +1832,7 @@ supported controller method arguments and return values.
[[webflux-ann-arguments]]
==== Method Arguments
[.small]#<<web.adoc#mvc-ann-arguments, Web MVC>>#
[.small]#<<web.adoc#mvc-ann-arguments, See equivalent in the Servlet stack>>#
The following table shows the supported controller method arguments.
@@ -1949,7 +1952,7 @@ and others) and is equivalent to `required=false`.
[[webflux-ann-return-types]]
==== Return Values
[.small]#<<web.adoc#mvc-ann-return-types, Web MVC>>#
[.small]#<<web.adoc#mvc-ann-return-types, See equivalent in the Servlet stack>>#
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
@@ -2030,7 +2033,7 @@ generally supported for all return values.
[[webflux-ann-typeconversion]]
==== Type Conversion
[.small]#<<web.adoc#mvc-ann-typeconversion, Web MVC>>#
[.small]#<<web.adoc#mvc-ann-typeconversion, See equivalent in the Servlet stack>>#
Some annotated controller method arguments that represent String-based request input (for example,
`@RequestParam`, `@RequestHeader`, `@PathVariable`, `@MatrixVariable`, and `@CookieValue`)
@@ -2050,7 +2053,7 @@ argument as `@Nullable`.
[[webflux-ann-matrix-variables]]
==== Matrix Variables
[.small]#<<web.adoc#mvc-ann-matrix-variables, Web MVC>>#
[.small]#<<web.adoc#mvc-ann-matrix-variables, See equivalent in the Servlet stack>>#
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
@@ -2185,7 +2188,7 @@ To get all matrix variables, use a `MultiValueMap`, as the following example sho
[[webflux-ann-requestparam]]
==== `@RequestParam`
[.small]#<<web.adoc#mvc-ann-requestparam, Web MVC>>#
[.small]#<<web.adoc#mvc-ann-requestparam, See equivalent in the Servlet stack>>#
You can use the `@RequestParam` annotation to bind query parameters to a method argument in a
controller. The following code snippet shows the usage:
@@ -2260,7 +2263,7 @@ with `@RequestParam`.
[[webflux-ann-requestheader]]
==== `@RequestHeader`
[.small]#<<web.adoc#mvc-ann-requestheader, Web MVC>>#
[.small]#<<web.adoc#mvc-ann-requestheader, See equivalent in the Servlet stack>>#
You can use the `@RequestHeader` annotation to bind a request header to a method argument in a
controller.
@@ -2321,7 +2324,7 @@ example, a method parameter annotated with `@RequestHeader("Accept")` may be of
[[webflux-ann-cookievalue]]
==== `@CookieValue`
[.small]#<<web.adoc#mvc-ann-cookievalue, Web MVC>>#
[.small]#<<web.adoc#mvc-ann-cookievalue, See equivalent in the Servlet stack>>#
You can use the `@CookieValue` annotation to bind the value of an HTTP cookie to a method argument
in a controller.
@@ -2362,7 +2365,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, Web MVC>>#
[.small]#<<web.adoc#mvc-ann-modelattrib-method-args, See equivalent in the Servlet stack>>#
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 overlaid with
@@ -2509,7 +2512,7 @@ with `@ModelAttribute`.
[[webflux-ann-sessionattributes]]
==== `@SessionAttributes`
[.small]#<<web.adoc#mvc-ann-sessionattributes, Web MVC>>#
[.small]#<<web.adoc#mvc-ann-sessionattributes, See equivalent in the Servlet stack>>#
`@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
@@ -2594,7 +2597,7 @@ as the following example shows:
[[webflux-ann-sessionattribute]]
==== `@SessionAttribute`
[.small]#<<web.adoc#mvc-ann-sessionattribute, Web MVC>>#
[.small]#<<web.adoc#mvc-ann-sessionattribute, See equivalent in the Servlet stack>>#
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,
@@ -2630,7 +2633,7 @@ workflow, consider using `SessionAttributes`, as described in
[[webflux-ann-requestattrib]]
==== `@RequestAttribute`
[.small]#<<web.adoc#mvc-ann-requestattrib, Web MVC>>#
[.small]#<<web.adoc#mvc-ann-requestattrib, See equivalent in the Servlet stack>>#
Similarly to `@SessionAttribute`, you can use the `@RequestAttribute` annotation to
access pre-existing request attributes created earlier (for example, by a `WebFilter`),
@@ -2659,7 +2662,7 @@ as the following example shows:
[[webflux-multipart-forms]]
==== Multipart Content
[.small]#<<web.adoc#mvc-multipart-forms, Web MVC>>#
[.small]#<<web.adoc#mvc-multipart-forms, See equivalent in the Servlet stack>>#
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
@@ -2936,7 +2939,7 @@ See <<webflux-client-body-multipart>>.
[[webflux-ann-requestbody]]
==== `@RequestBody`
[.small]#<<web.adoc#mvc-ann-requestbody, Web MVC>>#
[.small]#<<web.adoc#mvc-ann-requestbody, See equivalent in the Servlet stack>>#
You can use the `@RequestBody` annotation to have the request body read and deserialized into an
`Object` through an <<webflux-codecs,HttpMessageReader>>.
@@ -3010,7 +3013,7 @@ related operators:
[[webflux-ann-httpentity]]
==== `HttpEntity`
[.small]#<<web.adoc#mvc-ann-httpentity, Web MVC>>#
[.small]#<<web.adoc#mvc-ann-httpentity, See equivalent in the Servlet stack>>#
`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
@@ -3036,7 +3039,7 @@ container object that exposes request headers and the body. The following exampl
[[webflux-ann-responsebody]]
==== `@ResponseBody`
[.small]#<<web.adoc#mvc-ann-responsebody, Web MVC>>#
[.small]#<<web.adoc#mvc-ann-responsebody, See equivalent in the Servlet stack>>#
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
@@ -3079,7 +3082,7 @@ configure or customize message writing.
[[webflux-ann-responseentity]]
==== `ResponseEntity`
[.small]#<<web.adoc#mvc-ann-responseentity, Web MVC>>#
[.small]#<<web.adoc#mvc-ann-responseentity, See equivalent in the Servlet stack>>#
`ResponseEntity` is like <<webflux-ann-responsebody>> but with status and headers. For example:
@@ -3126,7 +3129,7 @@ Spring offers support for the Jackson JSON library.
[[webflux-ann-jsonview]]
===== JSON Views
[.small]#<<web.adoc#mvc-ann-jackson, Web MVC>>#
[.small]#<<web.adoc#mvc-ann-jackson, See equivalent in the Servlet stack>>#
Spring WebFlux provides built-in support for
https://www.baeldung.com/jackson-json-view-annotation[Jackson's Serialization Views],
@@ -3204,7 +3207,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, Web MVC>>#
[.small]#<<web.adoc#mvc-ann-modelattrib-methods, See equivalent in the Servlet stack>>#
You can use the `@ModelAttribute` annotation:
@@ -3343,7 +3346,7 @@ as the following example shows:
[[webflux-ann-initbinder]]
=== `DataBinder`
[.small]#<<web.adoc#mvc-ann-initbinder, Web MVC>>#
[.small]#<<web.adoc#mvc-ann-initbinder, See equivalent in the Servlet stack>>#
`@Controller` or `@ControllerAdvice` classes can have `@InitBinder` methods, to
initialize instances of `WebDataBinder`. Those, in turn, are used to:
@@ -3442,14 +3445,14 @@ controller-specific `Formatter` instances, as the following example shows:
[[webflux-ann-initbinder-model-design]]
==== Model Design
[.small]#<<web.adoc#mvc-ann-initbinder-model-design, Web MVC>>#
[.small]#<<web.adoc#mvc-ann-initbinder-model-design, See equivalent in the Servlet stack>>#
include::web-data-binding-model-design.adoc[]
[[webflux-ann-controller-exceptions]]
=== Exceptions
[.small]#<<web.adoc#mvc-ann-exceptionhandler, Web MVC>>#
[.small]#<<web.adoc#mvc-ann-exceptionhandler, See equivalent in the Servlet stack>>#
`@Controller` and <<webflux-ann-controller-advice, @ControllerAdvice>> classes can have
`@ExceptionHandler` methods to handle exceptions from controller methods. The following
@@ -3511,7 +3514,7 @@ for more detail.
[[webflux-ann-exceptionhandler-args]]
==== Method Arguments
[.small]#<<web.adoc#mvc-ann-exceptionhandler-args, Web MVC>>#
[.small]#<<web.adoc#mvc-ann-exceptionhandler-args, See equivalent in the Servlet stack>>#
`@ExceptionHandler` methods support the same <<webflux-ann-arguments,method arguments>>
as `@RequestMapping` methods, except the request body might have been consumed already.
@@ -3520,7 +3523,7 @@ as `@RequestMapping` methods, except the request body might have been consumed a
[[webflux-ann-exceptionhandler-return-values]]
==== Return Values
[.small]#<<web.adoc#mvc-ann-exceptionhandler-return-values, Web MVC>>#
[.small]#<<web.adoc#mvc-ann-exceptionhandler-return-values, See equivalent in the Servlet stack>>#
`@ExceptionHandler` methods support the same <<webflux-ann-return-types,return values>>
as `@RequestMapping` methods.
@@ -3529,7 +3532,7 @@ as `@RequestMapping` methods.
[[webflux-ann-controller-advice]]
=== Controller Advice
[.small]#<<web.adoc#mvc-ann-controller-advice, Web MVC>>#
[.small]#<<web.adoc#mvc-ann-controller-advice, See equivalent in the Servlet stack>>#
Typically, the `@ExceptionHandler`, `@InitBinder`, and `@ModelAttribute` methods apply
within the `@Controller` class (or class hierarchy) in which they are declared. If you
@@ -3597,7 +3600,7 @@ include::webflux-functional.adoc[leveloffset=+1]
[[webflux-uri-building]]
== URI Links
[.small]#<<web.adoc#mvc-uri-building, Web MVC>>#
[.small]#<<web.adoc#mvc-uri-building, See equivalent in the Servlet stack>>#
This section describes various options available in the Spring Framework to prepare URIs.
@@ -3608,7 +3611,7 @@ include::webflux-cors.adoc[leveloffset=+1]
[[webflux-ann-rest-exceptions]]
== Error Responses
[.small]#<<webmvc.adoc#mvc-ann-rest-exceptions, Web MVC>>#
[.small]#<<webmvc.adoc#mvc-ann-rest-exceptions, See equivalent in the Servlet stack>>#
A common requirement for REST services is to include details in the body of error
responses. The Spring Framework supports the "Problem Details for HTTP APIs"
@@ -3632,7 +3635,7 @@ and any `ErrorResponseException`, and renders an error response with a body.
[[webflux-ann-rest-exceptions-render]]
=== Render
[.small]#<<webmvc.adoc#mvc-ann-rest-exceptions-render, Web MVC>>#
[.small]#<<webmvc.adoc#mvc-ann-rest-exceptions-render, See equivalent in the Servlet stack>>#
You can return `ProblemDetail` or `ErrorResponse` from any `@ExceptionHandler` or from
any `@RequestMapping` method to render an RFC 7807 response. This is processed as follows:
@@ -3655,7 +3658,7 @@ use a protected method to map any exception to a `ProblemDetail`.
[[webflux-ann-rest-exceptions-non-standard]]
=== Non-Standard Fields
[.small]#<<webmvc.adoc#mvc-ann-rest-exceptions-non-standard, Web MVC>>#
[.small]#<<webmvc.adoc#mvc-ann-rest-exceptions-non-standard, See equivalent in the Servlet stack>>#
You can extend an RFC 7807 response with non-standard fields in one of two ways.
@@ -3675,7 +3678,7 @@ from an existing `ProblemDetail`. This could be done centrally, e.g. from an
[[webflux-ann-rest-exceptions-i18n]]
=== Internationalization
[.small]#<<webmvc.adoc#mvc-ann-rest-exceptions-i18n, Web MVC>>#
[.small]#<<webmvc.adoc#mvc-ann-rest-exceptions-i18n, See equivalent in the Servlet stack>>#
It is a common requirement to internationalize error response details, and good practice
to customize the problem details for Spring WebFlux exceptions. This is supported as follows:
@@ -3746,7 +3749,7 @@ qualified exception class name.
[[webflux-ann-rest-exceptions-client]]
=== Client Handling
[.small]#<<webmvc.adoc#mvc-ann-rest-exceptions-client, Web MVC>>#
[.small]#<<webmvc.adoc#mvc-ann-rest-exceptions-client, See equivalent in the Servlet stack>>#
A client application can catch `WebClientResponseException`, when using the `WebClient`,
or `RestClientResponseException` when using the `RestTemplate`, and use their
@@ -3758,7 +3761,7 @@ or `RestClientResponseException` when using the `RestTemplate`, and use their
[[webflux-web-security]]
== Web Security
[.small]#<<web.adoc#mvc-web-security, Web MVC>>#
[.small]#<<web.adoc#mvc-web-security, See equivalent in the Servlet stack>>#
The https://spring.io/projects/spring-security[Spring Security] project provides support
for protecting web applications from malicious exploits. See the Spring Security
@@ -3774,7 +3777,7 @@ reference documentation, including:
[[webflux-caching]]
== HTTP Caching
[.small]#<<web.adoc#mvc-caching, Web MVC>>#
[.small]#<<web.adoc#mvc-caching, See equivalent in the Servlet stack>>#
HTTP caching can significantly improve the performance of a web application. HTTP caching
revolves around the `Cache-Control` response header and subsequent conditional request
@@ -3790,7 +3793,7 @@ This section describes the HTTP caching related options available in Spring WebF
[[webflux-caching-cachecontrol]]
=== `CacheControl`
[.small]#<<web.adoc#mvc-caching-cachecontrol, Web MVC>>#
[.small]#<<web.adoc#mvc-caching-cachecontrol, See equivalent in the Servlet stack>>#
{api-spring-framework}/http/CacheControl.html[`CacheControl`] provides support for
configuring settings related to the `Cache-Control` header and is accepted as an argument
@@ -3838,7 +3841,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, Web MVC>>#
[.small]#<<web.adoc#mvc-caching-etag-lastmodified, See equivalent in the Servlet stack>>#
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
@@ -3939,7 +3942,7 @@ to 412 (PRECONDITION_FAILED) to prevent concurrent modification.
[[webflux-caching-static-resources]]
=== Static Resources
[.small]#<<web.adoc#mvc-caching-static-resources, Web MVC>>#
[.small]#<<web.adoc#mvc-caching-static-resources, See equivalent in the Servlet stack>>#
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>>.
@@ -3950,7 +3953,7 @@ include::webflux-view.adoc[leveloffset=+1]
[[webflux-config]]
== WebFlux Config
[.small]#<<web.adoc#mvc-config, Web MVC>>#
[.small]#<<web.adoc#mvc-config, See equivalent in the Servlet stack>>#
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
@@ -3967,7 +3970,7 @@ gain full control over the configuration through the
[[webflux-config-enable]]
=== Enabling WebFlux Config
[.small]#<<web.adoc#mvc-config-enable, Web MVC>>#
[.small]#<<web.adoc#mvc-config-enable, See equivalent in the Servlet stack>>#
You can use the `@EnableWebFlux` annotation in your Java config, as the following example shows:
@@ -3996,7 +3999,7 @@ available on the classpath -- for JSON, XML, and others.
[[webflux-config-customize]]
=== WebFlux config API
[.small]#<<web.adoc#mvc-config-customize, Web MVC>>#
[.small]#<<web.adoc#mvc-config-customize, See equivalent in the Servlet stack>>#
In your Java configuration, you can implement the `WebFluxConfigurer` interface,
as the following example shows:
@@ -4027,7 +4030,7 @@ class WebConfig : WebFluxConfigurer {
[[webflux-config-conversion]]
=== Conversion, formatting
[.small]#<<web.adoc#mvc-config-conversion, Web MVC>>#
[.small]#<<web.adoc#mvc-config-conversion, See equivalent in the Servlet stack>>#
By default, formatters for various number and date types are installed, along with support
for customization via `@NumberFormat` and `@DateTimeFormat` on fields.
@@ -4104,7 +4107,7 @@ use `FormatterRegistrar` implementations.
[[webflux-config-validation]]
=== Validation
[.small]#<<web.adoc#mvc-config-validation, Web MVC>>#
[.small]#<<web.adoc#mvc-config-validation, See equivalent in the Servlet stack>>#
By default, if <<core.adoc#validation-beanvalidation-overview, Bean Validation>> is present
on the classpath (for example, the Hibernate Validator), the `LocalValidatorFactoryBean`
@@ -4179,7 +4182,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, Web MVC>>#
[.small]#<<web.adoc#mvc-config-content-negotiation, See equivalent in the Servlet stack>>#
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,
@@ -4217,7 +4220,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, Web MVC>>#
[.small]#<<web.adoc#mvc-config-message-converters, See equivalent in the Servlet stack>>#
The following example shows how to customize how the request and response body are read and written:
@@ -4268,7 +4271,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, Web MVC>>#
[.small]#<<web.adoc#mvc-config-view-resolvers, See equivalent in the Servlet stack>>#
The following example shows how to configure view resolution:
@@ -4425,7 +4428,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, Web MVC>>#
[.small]#<<web.adoc#mvc-config-static-resources, See equivalent in the Servlet stack>>#
This option provides a convenient way to serve static resources from a list of
{api-spring-framework}/core/io/Resource.html[`Resource`]-based locations.
@@ -4549,7 +4552,7 @@ for fine-grained control, e.g. last-modified behavior and optimized resource res
[[webflux-config-path-matching]]
=== Path Matching
[.small]#<<web.adoc#mvc-config-path-matching, Web MVC>>#
[.small]#<<web.adoc#mvc-config-path-matching, See equivalent in the Servlet stack>>#
You can customize options related to path matching. For details on the individual options, see the
{api-spring-framework}/web/reactive/config/PathMatchConfigurer.html[`PathMatchConfigurer`] javadoc.
@@ -4650,7 +4653,7 @@ For example:
[[webflux-config-advanced-java]]
=== Advanced Configuration Mode
[.small]#<<web.adoc#mvc-config-advanced-java, Web MVC>>#
[.small]#<<web.adoc#mvc-config-advanced-java, See equivalent in the Servlet stack>>#
`@EnableWebFlux` imports `DelegatingWebFluxConfiguration` that:
@@ -4690,7 +4693,7 @@ the classpath.
[[webflux-http2]]
== HTTP/2
[.small]#<<web.adoc#mvc-http2, Web MVC>>#
[.small]#<<web.adoc#mvc-http2, See equivalent in the Servlet stack>>#
HTTP/2 is supported with Reactor Netty, Tomcat, Jetty, and Undertow. However, there are
considerations related to server configuration. For more details, see the
@@ -1,6 +1,6 @@
[[mvc-cors]]
= CORS
[.small]#<<web-reactive.adoc#webflux-cors, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-cors, See equivalent in the Reactive stack>>#
Spring MVC lets you handle CORS (Cross-Origin Resource Sharing). This section
describes how to do so.
@@ -10,7 +10,7 @@ describes how to do so.
[[mvc-cors-intro]]
== Introduction
[.small]#<<web-reactive.adoc#webflux-cors-intro, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-cors-intro, See equivalent in the Reactive stack>>#
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.
[[mvc-cors-processing]]
== Processing
[.small]#<<web-reactive.adoc#webflux-cors-processing, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-cors-processing, See equivalent in the Reactive stack>>#
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
[[mvc-cors-controller]]
== `@CrossOrigin`
[.small]#<<web-reactive.adoc#webflux-cors-controller, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-cors-controller, See equivalent in the Reactive stack>>#
The {api-spring-framework}/web/bind/annotation/CrossOrigin.html[`@CrossOrigin`]
annotation enables cross-origin requests on annotated controller methods,
@@ -226,7 +226,7 @@ as the following example shows:
[[mvc-cors-global]]
== Global Configuration
[.small]#<<web-reactive.adoc#webflux-cors-global, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-cors-global, See equivalent in the Reactive stack>>#
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`
@@ -252,7 +252,7 @@ the `allowOriginPatterns` property may be used to match to a dynamic set of orig
[[mvc-cors-global-java]]
=== Java Configuration
[.small]#<<web-reactive.adoc#webflux-cors-global, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-cors-global, See equivalent in the Reactive stack>>#
To enable CORS in the MVC Java config, you can use the `CorsRegistry` callback,
as the following example shows:
@@ -329,7 +329,7 @@ as the following example shows:
[[mvc-cors-filter]]
== CORS Filter
[.small]#<<webflux-cors.adoc#webflux-cors-webfilter, WebFlux>>#
[.small]#<<webflux-cors.adoc#webflux-cors-webfilter, See equivalent in the Reactive stack>>#
You can apply CORS support through the built-in
{api-spring-framework}/web/filter/CorsFilter.html[`CorsFilter`].
@@ -1,6 +1,6 @@
[[webmvc-fn]]
= Functional Endpoints
[.small]#<<web-reactive.adoc#webflux-fn, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-fn, See equivalent in the Reactive stack>>#
Spring Web MVC includes WebMvc.fn, a lightweight functional programming model in which functions
are used to route and handle requests and contracts are designed for immutability.
@@ -12,7 +12,7 @@ the same <<web#mvc-servlet>>.
[[webmvc-fn-overview]]
== Overview
[.small]#<<web-reactive.adoc#webflux-fn-overview, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-fn-overview, See equivalent in the Reactive stack>>#
In WebMvc.fn, an HTTP request is handled with a `HandlerFunction`: a function that takes
`ServerRequest` and returns a `ServerResponse`.
@@ -111,7 +111,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, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-fn-handler-functions, See equivalent in the Reactive stack>>#
`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.
@@ -238,22 +238,22 @@ allows you to send Strings, or other objects as JSON. For example:
.Java
----
public RouterFunction<ServerResponse> sse() {
return route(GET("/sse"), request -> ServerResponse.sse(sseBuilder -> {
// Save the sseBuilder object somewhere..
}));
return route(GET("/sse"), request -> ServerResponse.sse(sseBuilder -> {
// Save the sseBuilder object somewhere..
}));
}
// In some other thread, sending a String
sseBuilder.send("Hello world");
// Or an object, which will be transformed into JSON
Person person = ...
Person person = ...
sseBuilder.send(person);
// Customize the event by using the other methods
sseBuilder.id("42")
.event("sse event")
.data(person);
// Customize the event by using the other methods
sseBuilder.id("42")
.event("sse event")
.data(person);
// and done at some point
sseBuilder.complete();
@@ -261,23 +261,23 @@ allows you to send Strings, or other objects as JSON. For example:
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
.Kotlin
----
fun sse(): RouterFunction<ServerResponse> = router {
GET("/sse") { request -> ServerResponse.sse { sseBuilder ->
// Save the sseBuilder object somewhere..
}
}
fun sse(): RouterFunction<ServerResponse> = router {
GET("/sse") { request -> ServerResponse.sse { sseBuilder ->
// Save the sseBuilder object somewhere..
}
}
// In some other thread, sending a String
sseBuilder.send("Hello world")
// Or an object, which will be transformed into JSON
val person = ...
val person = ...
sseBuilder.send(person)
// Customize the event by using the other methods
sseBuilder.id("42")
.event("sse event")
.data(person)
// Customize the event by using the other methods
sseBuilder.id("42")
.event("sse event")
.data(person)
// and done at some point
sseBuilder.complete()
@@ -463,7 +463,7 @@ See <<core.adoc#validation-beanvalidation, Spring Validation>>.
[[webmvc-fn-router-functions]]
== `RouterFunction`
[.small]#<<web-reactive.adoc#webflux-fn-router-functions, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-fn-router-functions, See equivalent in the Reactive stack>>#
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
@@ -672,7 +672,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, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-fn-running, See equivalent in the Reactive stack>>#
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
@@ -765,7 +765,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, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-fn-handler-filter-function, See equivalent in the Reactive stack>>#
You can filter handler functions by using the `before`, `after`, or `filter` methods on the routing
function builder.
@@ -1,6 +1,6 @@
[[webmvc.test]]
= Testing
[.small]#<<web-reactive.adoc#webflux-test, Same in Spring WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-test, See equivalent in the Reactive stack>>#
This section summarizes the options available in `spring-test` for Spring MVC applications.
@@ -25,4 +25,4 @@ See <<testing.adoc#spring-mvc-test-client, Client REST Tests>> for more details.
* `WebTestClient`: Built for testing WebFlux applications, but it can also be used for
end-to-end integration testing, to any server, over an HTTP connection. It is a
non-blocking, reactive client and is well suited for testing asynchronous and streaming
scenarios.
scenarios. See <<testing.adoc#webtestclient, `WebTestClient`>> for more details.
@@ -1,6 +1,6 @@
[[mvc-view]]
= View Technologies
[.small]#<<web-reactive.adoc#webflux-view, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-view, See equivalent in the Reactive stack>>#
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 of
@@ -14,7 +14,7 @@ the templates are editable by external sources, since this can have security imp
[[mvc-view-thymeleaf]]
== Thymeleaf
[.small]#<<web-reactive.adoc#webflux-view-thymeleaf, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-view-thymeleaf, See equivalent in the Reactive stack>>#
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
@@ -34,7 +34,7 @@ See https://www.thymeleaf.org/documentation.html[Thymeleaf+Spring] for more deta
[[mvc-view-freemarker]]
== FreeMarker
[.small]#<<web-reactive.adoc#webflux-view-freemarker, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-view-freemarker, See equivalent in the Reactive stack>>#
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
@@ -44,7 +44,7 @@ integration for using Spring MVC with FreeMarker templates.
[[mvc-view-freemarker-contextconfig]]
=== View Configuration
[.small]#<<web-reactive.adoc#webflux-view-freemarker-contextconfig, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-view-freemarker-contextconfig, See equivalent in the Reactive stack>>#
The following example shows how to configure FreeMarker as a view technology:
@@ -125,7 +125,7 @@ returns a view name of `welcome`, the resolver looks for the
[[mvc-views-freemarker]]
=== FreeMarker Configuration
[.small]#<<web-reactive.adoc#webflux-views-freemarker, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-views-freemarker, See equivalent in the Reactive stack>>#
You can pass FreeMarker 'Settings' and 'SharedVariables' directly to the FreeMarker
`Configuration` object (which is managed by Spring) by setting the appropriate bean
@@ -164,7 +164,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, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-view-bind-macros, See equivalent in the Reactive stack>>#
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.
@@ -288,7 +288,7 @@ as the value for the `fieldType` parameter.
The parameters to any of the above macros have consistent meanings:
* `path`: The name of the field to bind to (ie "command.name")
* `path`: The name of the field to bind to (for example, "command.name")
* `options`: A `Map` of all the available values that can be selected from in the input
field. The keys to the map represent the values that are POSTed back from the form
and bound to the command object. Map objects stored against the keys are the labels
@@ -576,7 +576,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, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-view-script, See equivalent in the Reactive stack>>#
The Spring Framework has a built-in integration for using Spring MVC with any
templating library that can run on top of the
@@ -602,7 +602,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, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-view-script-dependencies, See equivalent in the Reactive stack>>#
You need to have the script engine on your classpath, the details of which vary by script engine:
@@ -622,7 +622,7 @@ through https://www.webjars.org/[WebJars].
[[mvc-view-script-integrate]]
=== Script Templates
[.small]#<<web-reactive.adoc#webflux-view-script, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-view-script, See equivalent in the Reactive stack>>#
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.
@@ -1965,7 +1965,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, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-view-httpmessagewriter, See equivalent in the Reactive stack>>#
Spring offers support for the Jackson JSON library.
@@ -1973,7 +1973,7 @@ Spring offers support for the Jackson JSON library.
[[mvc-view-json-mapping]]
=== Jackson-based JSON MVC Views
[.small]#<<web-reactive.adoc#webflux-view-httpmessagewriter, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-view-httpmessagewriter, See equivalent in the Reactive stack>>#
The `MappingJackson2JsonView` uses the Jackson library's `ObjectMapper` to render the response
content as JSON. By default, the entire contents of the model map (with the exception of
@@ -1992,7 +1992,7 @@ serializers and deserializers for specific types.
[[mvc-view-xml-mapping]]
=== Jackson-based XML Views
[.small]#<<web-reactive.adoc#webflux-view-httpmessagewriter, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-view-httpmessagewriter, See equivalent in the Reactive stack>>#
`MappingJackson2XmlView` uses the
https://github.com/FasterXML/jackson-dataformat-xml[Jackson XML extension's] `XmlMapper`
@@ -23,7 +23,7 @@ https://github.com/spring-projects/spring-framework/wiki/Spring-Framework-Versio
[[mvc-servlet]]
== DispatcherServlet
[.small]#<<web-reactive.adoc#webflux-dispatcher-handler, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-dispatcher-handler, See equivalent in the Reactive stack>>#
Spring MVC, as many other web frameworks, is designed around the front controller
pattern where a central `Servlet`, the `DispatcherServlet`, provides a shared algorithm
@@ -240,7 +240,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, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-special-bean-types, See equivalent in the Reactive stack>>#
The `DispatcherServlet` delegates to special beans to process requests and render the
appropriate responses. By "`special beans`" we mean Spring-managed `Object` instances that
@@ -300,7 +300,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, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-framework-config, See equivalent in the Reactive stack>>#
Applications can declare the infrastructure beans listed in <<mvc-servlet-special-bean-types>>
that are required to process requests. The `DispatcherServlet` checks the
@@ -503,7 +503,7 @@ override the `createDispatcherServlet` method.
[[mvc-servlet-sequence]]
=== Processing
[.small]#<<web-reactive.adoc#webflux-dispatcher-handler-sequence, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-dispatcher-handler-sequence, See equivalent in the Reactive stack>>#
The `DispatcherServlet` processes requests as follows:
@@ -662,7 +662,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, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-dispatcher-exceptions, See equivalent in the Reactive stack>>#
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`
@@ -773,7 +773,7 @@ however, use both a `WebApplicationInitializer` and a minimal `web.xml`.
[[mvc-viewresolver]]
=== View Resolution
[.small]#<<web-reactive.adoc#webflux-viewresolution, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-viewresolution, See equivalent in the Reactive stack>>#
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`
@@ -825,7 +825,7 @@ The following table provides more details on the `ViewResolver` hierarchy:
[[mvc-viewresolver-handling]]
==== Handling
[.small]#<<web-reactive.adoc#webflux-viewresolution-handling, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-viewresolution-handling, See equivalent in the Reactive stack>>#
You can chain view resolvers by declaring more than one resolver bean and, if necessary, by
setting the `order` property to specify ordering. Remember, the higher the order property,
@@ -846,7 +846,7 @@ rendering without controller logic.
[[mvc-redirecting-redirect-prefix]]
==== Redirecting
[.small]#<<web-reactive.adoc#webflux-redirecting-redirect-prefix, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-redirecting-redirect-prefix, See equivalent in the Reactive stack>>#
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
@@ -876,7 +876,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, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-multiple-representations, See equivalent in the Reactive stack>>#
{api-spring-framework}/web/servlet/view/ContentNegotiatingViewResolver.html[`ContentNegotiatingViewResolver`]
does not resolve views itself but rather delegates
@@ -978,7 +978,7 @@ The following table describes the properties `CookieLocaleResolver`:
| Property | Default | Description
| `cookieName`
| classname + LOCALE
| class name + LOCALE
| The name of the cookie
| `cookieMaxAge`
@@ -1138,7 +1138,7 @@ request with a simple request parameter.
[[mvc-multipart]]
=== Multipart Resolver
[.small]#<<web-reactive.adoc#webflux-multipart, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-multipart, See equivalent in the Reactive stack>>#
`MultipartResolver` from the `org.springframework.web.multipart` package is a strategy
for parsing multipart requests including file uploads. There is a container-based
@@ -1214,7 +1214,7 @@ javadoc for details and configuration options.
[[mvc-logging]]
=== Logging
[.small]#<<web-reactive.adoc#webflux-logging, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-logging, See equivalent in the Reactive stack>>#
DEBUG-level logging in Spring MVC is designed to be compact, minimal, and
human-friendly. It focuses on high-value bits of information that are useful over and
@@ -1230,7 +1230,7 @@ not meet the stated goals, please let us know.
[[mvc-logging-sensitive-data]]
==== Sensitive Data
[.small]#<<web-reactive.adoc#webflux-logging-sensitive-data, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-logging-sensitive-data, See equivalent in the Reactive stack>>#
DEBUG and TRACE logging may log sensitive information. This is why request parameters and
headers are masked by default and their logging in full must be enabled explicitly
@@ -1294,7 +1294,7 @@ public class MyInitializer
[[filters]]
== Filters
[.small]#<<web-reactive.adoc#webflux-filters, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-filters, See equivalent in the Reactive stack>>#
The `spring-web` module provides some useful filters:
@@ -1321,7 +1321,7 @@ available through the `ServletRequest.getParameter{asterisk}()` family of method
[[filters-forwarded-headers]]
=== Forwarded Headers
[.small]#<<web-reactive.adoc#webflux-forwarded-headers, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-forwarded-headers, See equivalent in the Reactive stack>>#
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
@@ -1382,7 +1382,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, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-filters-cors, See equivalent in the Reactive stack>>#
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
@@ -1395,7 +1395,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, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-controller, See equivalent in the Reactive stack>>#
Spring MVC provides an annotation-based programming model where `@Controller` and
`@RestController` components use annotations to express request mappings, request input,
@@ -1442,7 +1442,7 @@ programming model described in this section.
[[mvc-ann-controller]]
=== Declaration
[.small]#<<web-reactive.adoc#webflux-ann-controller, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-ann-controller, See equivalent in the Reactive stack>>#
You can define controller beans by using a standard Spring bean definition in the
Servlet's `WebApplicationContext`. The `@Controller` stereotype allows for auto-detection,
@@ -1504,7 +1504,7 @@ directly to the response body versus view resolution and rendering with an HTML
[[mvc-ann-requestmapping-proxying]]
==== AOP Proxies
[.small]#<<web-reactive.adoc#webflux-ann-requestmapping-proxying, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-ann-requestmapping-proxying, See equivalent in the Reactive stack>>#
In some cases, you may need to decorate a controller with an AOP proxy at runtime.
One example is if you choose to have `@Transactional` annotations directly on the
@@ -1526,7 +1526,7 @@ Please, enable class based proxying, or otherwise the interface must also have a
[[mvc-ann-requestmapping]]
=== Request Mapping
[.small]#<<web-reactive.adoc#webflux-ann-requestmapping, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-ann-requestmapping, See equivalent in the Reactive stack>>#
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
@@ -1591,7 +1591,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, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-ann-requestmapping-uri-templates, See equivalent in the Reactive stack>>#
`@RequestMapping` methods can be mapped using URL patterns. There are two alternatives:
@@ -1713,7 +1713,7 @@ some external configuration.
[[mvc-ann-requestmapping-pattern-comparison]]
==== Pattern Comparison
[.small]#<<web-reactive.adoc#webflux-ann-requestmapping-pattern-comparison, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-ann-requestmapping-pattern-comparison, See equivalent in the Reactive stack>>#
When multiple patterns match a URL, the best match must be selected. This is done with
one of the following depending on whether use of parsed `PathPattern` is enabled for use or not:
@@ -1794,7 +1794,7 @@ recommendations related to RFD.
[[mvc-ann-requestmapping-consumes]]
==== Consumable Media Types
[.small]#<<web-reactive.adoc#webflux-ann-requestmapping-consumes, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-ann-requestmapping-consumes, See equivalent in the Reactive stack>>#
You can narrow the request mapping based on the `Content-Type` of the request,
as the following example shows:
@@ -1832,7 +1832,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, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-ann-requestmapping-produces, See equivalent in the Reactive stack>>#
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:
@@ -1872,7 +1872,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, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-ann-requestmapping-params-and-headers, See equivalent in the Reactive stack>>#
You can narrow request mappings based on request parameter conditions. You can test for the
presence of a request parameter (`myParam`), for the absence of one (`!myParam`), or for a
@@ -1927,7 +1927,7 @@ instead.
[[mvc-ann-requestmapping-head-options]]
==== HTTP HEAD, OPTIONS
[.small]#<<web-reactive.adoc#webflux-ann-requestmapping-head-options, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-ann-requestmapping-head-options, See equivalent in the Reactive stack>>#
`@GetMapping` (and `@RequestMapping(method=HttpMethod.GET)`) support HTTP HEAD
transparently for request mapping. Controller methods do not need to change.
@@ -1953,7 +1953,7 @@ is not necessary in the common case.
[[mvc-ann-requestmapping-composed]]
==== Custom Annotations
[.small]#<<web-reactive.adoc#mvc-ann-requestmapping-head-options, WebFlux>>#
[.small]#<<web-reactive.adoc#mvc-ann-requestmapping-head-options, See equivalent in the Reactive stack>>#
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
@@ -1974,7 +1974,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, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-ann-requestmapping-registration, See equivalent in the Reactive stack>>#
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
@@ -2027,7 +2027,7 @@ under different URLs. The following example registers a handler method:
[[mvc-ann-methods]]
=== Handler Methods
[.small]#<<web-reactive.adoc#webflux-ann-methods, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-ann-methods, See equivalent in the Reactive stack>>#
`@RequestMapping` handler methods have a flexible signature and can choose from a range of
supported controller method arguments and return values.
@@ -2035,7 +2035,7 @@ supported controller method arguments and return values.
[[mvc-ann-arguments]]
==== Method Arguments
[.small]#<<web-reactive.adoc#webflux-ann-arguments, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-ann-arguments, See equivalent in the Reactive stack>>#
The next table describes the supported controller method arguments. Reactive types are not supported
for any arguments.
@@ -2176,7 +2176,7 @@ and others) and is equivalent to `required=false`.
[[mvc-ann-return-types]]
==== Return Values
[.small]#<<web-reactive.adoc#webflux-ann-return-types, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-ann-return-types, See equivalent in the Reactive stack>>#
The next table describes the supported controller method return values. Reactive types are
supported for all return values.
@@ -2280,7 +2280,7 @@ supported for all return values.
[[mvc-ann-typeconversion]]
==== Type Conversion
[.small]#<<web-reactive.adoc#webflux-ann-typeconversion, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-ann-typeconversion, See equivalent in the Reactive stack>>#
Some annotated controller method arguments that represent `String`-based request input (such as
`@RequestParam`, `@RequestHeader`, `@PathVariable`, `@MatrixVariable`, and `@CookieValue`)
@@ -2313,7 +2313,7 @@ an empty original value, so the corresponding `Missing...Exception` variants wil
[[mvc-ann-matrix-variables]]
==== Matrix Variables
[.small]#<<web-reactive.adoc#webflux-ann-matrix-variables, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-ann-matrix-variables, See equivalent in the Reactive stack>>#
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
@@ -2453,7 +2453,7 @@ you need to set a `UrlPathHelper` with `removeSemicolonContent=false` through
[[mvc-ann-requestparam]]
==== `@RequestParam`
[.small]#<<web-reactive.adoc#webflux-ann-requestparam, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-ann-requestparam, See equivalent in the Reactive stack>>#
You can use the `@RequestParam` annotation to bind Servlet request parameters (that is,
query parameters or form data) to a method argument in a controller.
@@ -2529,7 +2529,7 @@ with `@RequestParam`.
[[mvc-ann-requestheader]]
==== `@RequestHeader`
[.small]#<<web-reactive.adoc#webflux-ann-requestheader, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-ann-requestheader, See equivalent in the Reactive stack>>#
You can use the `@RequestHeader` annotation to bind a request header to a method argument in a
controller.
@@ -2590,7 +2590,7 @@ example, a method parameter annotated with `@RequestHeader("Accept")` can be of
[[mvc-ann-cookievalue]]
==== `@CookieValue`
[.small]#<<web-reactive.adoc#webflux-ann-cookievalue, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-ann-cookievalue, See equivalent in the Reactive stack>>#
You can use the `@CookieValue` annotation to bind the value of an HTTP cookie to a method argument
in a controller.
@@ -2630,7 +2630,7 @@ See <<mvc-ann-typeconversion>>.
[[mvc-ann-modelattrib-method-args]]
==== `@ModelAttribute`
[.small]#<<web-reactive.adoc#webflux-ann-modelattrib-method-args, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-ann-modelattrib-method-args, See equivalent in the Reactive stack>>#
You can use the `@ModelAttribute` annotation on a method argument to access an attribute from
the model or have it be instantiated if not present. The model attribute is also overlain with
@@ -2823,7 +2823,7 @@ with `@ModelAttribute`.
[[mvc-ann-sessionattributes]]
==== `@SessionAttributes`
[.small]#<<web-reactive.adoc#webflux-ann-sessionattributes, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-ann-sessionattributes, See equivalent in the Reactive stack>>#
`@SessionAttributes` is used to store model attributes in the HTTP Servlet session between
requests. It is a type-level annotation that declares the session attributes used by a
@@ -2907,7 +2907,7 @@ class EditPetForm {
[[mvc-ann-sessionattribute]]
==== `@SessionAttribute`
[.small]#<<web-reactive.adoc#webflux-ann-sessionattribute, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-ann-sessionattribute, See equivalent in the Reactive stack>>#
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,
@@ -2945,7 +2945,7 @@ workflow, consider using `@SessionAttributes` as described in
[[mvc-ann-requestattrib]]
==== `@RequestAttribute`
[.small]#<<web-reactive.adoc#webflux-ann-requestattrib, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-ann-requestattrib, See equivalent in the Reactive stack>>#
Similar to `@SessionAttribute`, you can use the `@RequestAttribute` annotations to
access pre-existing request attributes created earlier (for example, by a Servlet `Filter`
@@ -3072,7 +3072,7 @@ Therefore, we recommend that you use flash attributes mainly for redirect scenar
[[mvc-multipart-forms]]
==== Multipart
[.small]#<<web-reactive.adoc#webflux-multipart-forms, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-multipart-forms, See equivalent in the Reactive stack>>#
After a `MultipartResolver` has been <<mvc-multipart,enabled>>, the content of POST
requests with `multipart/form-data` is parsed and accessible as regular request
@@ -3257,7 +3257,7 @@ as the following example shows:
[[mvc-ann-requestbody]]
==== `@RequestBody`
[.small]#<<web-reactive.adoc#webflux-ann-requestbody, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-ann-requestbody, See equivalent in the Reactive stack>>#
You can use the `@RequestBody` annotation to have the request body read and deserialized into an
`Object` through an <<integration.adoc#rest-message-conversion, `HttpMessageConverter`>>.
@@ -3311,7 +3311,7 @@ as the following example shows:
[[mvc-ann-httpentity]]
==== HttpEntity
[.small]#<<web-reactive.adoc#webflux-ann-httpentity, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-ann-httpentity, See equivalent in the Reactive stack>>#
`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:
@@ -3337,7 +3337,7 @@ container object that exposes request headers and body. The following listing sh
[[mvc-ann-responsebody]]
==== `@ResponseBody`
[.small]#<<web-reactive.adoc#webflux-ann-responsebody, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-ann-responsebody, See equivalent in the Reactive stack>>#
You can use the `@ResponseBody` annotation on a method to have the return serialized
to the response body through an
@@ -3379,7 +3379,7 @@ See <<mvc-ann-jackson>> for details.
[[mvc-ann-responseentity]]
==== ResponseEntity
[.small]#<<web-reactive.adoc#webflux-ann-responseentity, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-ann-responseentity, See equivalent in the Reactive stack>>#
`ResponseEntity` is like <<mvc-ann-responsebody>> but with status and headers. For example:
@@ -3423,7 +3423,7 @@ Spring offers support for the Jackson JSON library.
[[mvc-ann-jsonview]]
===== JSON Views
[.small]#<<web-reactive.adoc#webflux-ann-jsonview, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-ann-jsonview, See equivalent in the Reactive stack>>#
Spring MVC provides built-in support for
https://www.baeldung.com/jackson-json-view-annotation[Jackson's Serialization Views],
@@ -3563,7 +3563,7 @@ to the model, as the following example shows:
[[mvc-ann-modelattrib-methods]]
=== Model
[.small]#<<web-reactive.adoc#webflux-ann-modelattrib-methods, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-ann-modelattrib-methods, See equivalent in the Reactive stack>>#
You can use the `@ModelAttribute` annotation:
@@ -3661,7 +3661,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, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-ann-initbinder, See equivalent in the Reactive stack>>#
`@Controller` or `@ControllerAdvice` classes can have `@InitBinder` methods that
initialize instances of `WebDataBinder`, and those, in turn, can:
@@ -3755,14 +3755,14 @@ controller-specific `Formatter` implementations, as the following example shows:
[[mvc-ann-initbinder-model-design]]
==== Model Design
[.small]#<<web-reactive.adoc#webflux-ann-initbinder-model-design, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-ann-initbinder-model-design, See equivalent in the Reactive stack>>#
include::web-data-binding-model-design.adoc[]
[[mvc-ann-exceptionhandler]]
=== Exceptions
[.small]#<<web-reactive.adoc#webflux-ann-controller-exceptions, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-ann-controller-exceptions, See equivalent in the Reactive stack>>#
`@Controller` and <<mvc-ann-controller-advice, @ControllerAdvice>> classes can have
`@ExceptionHandler` methods to handle exceptions from controller methods, as the following example shows:
@@ -3888,7 +3888,7 @@ level, <<mvc-exceptionhandlers, HandlerExceptionResolver>> mechanism.
[[mvc-ann-exceptionhandler-args]]
==== Method Arguments
[.small]#<<webflux.adoc#webflux-ann-exceptionhandler-args, WebFlux>>#
[.small]#<<webflux.adoc#webflux-ann-exceptionhandler-args, See equivalent in the Reactive stack>>#
`@ExceptionHandler` methods support the following arguments:
@@ -3953,7 +3953,7 @@ level, <<mvc-exceptionhandlers, HandlerExceptionResolver>> mechanism.
[[mvc-ann-exceptionhandler-return-values]]
==== Return Values
[.small]#<<webflux.adoc#webflux-ann-exceptionhandler-return-values, WebFlux>>#
[.small]#<<webflux.adoc#webflux-ann-exceptionhandler-return-values, See equivalent in the Reactive stack>>#
`@ExceptionHandler` methods support the following return values:
@@ -4023,7 +4023,7 @@ see <<mvc-ann-rest-exceptions>>
[[mvc-ann-controller-advice]]
=== Controller Advice
[.small]#<<web-reactive.adoc#webflux-ann-controller-advice, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-ann-controller-advice, See equivalent in the Reactive stack>>#
`@ExceptionHandler`, `@InitBinder`, and `@ModelAttribute` methods apply only to the
`@Controller` class, or class hierarchy, in which they are declared. If, instead, they
@@ -4087,7 +4087,7 @@ include::webmvc-functional.adoc[leveloffset=+1]
[[mvc-uri-building]]
== URI Links
[.small]#<<web-reactive.adoc#webflux-uri-building, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-uri-building, See equivalent in the Reactive stack>>#
This section describes various options available in the Spring Framework to work with URI's.
@@ -4366,7 +4366,6 @@ capital letters of the class and the method name (for example, the `getThing` me
[[mvc-ann-async]]
== Asynchronous Requests
[.small]#<<mvc-ann-async-vs-webflux, Compared to WebFlux>>#
Spring MVC has an extensive integration with Servlet asynchronous request
<<mvc-ann-async-processing,processing>>:
@@ -4379,11 +4378,10 @@ return value.
* Controllers can use reactive clients and return
<<mvc-ann-async-reactive-types, reactive types>> for response handling.
For an overview of how this differs from Spring WebFlux, see the <<mvc-ann-async-vs-webflux>> section below.
[[mvc-ann-async-deferredresult]]
=== `DeferredResult`
[.small]#<<mvc-ann-async-vs-webflux, Compared to WebFlux>>#
Once the asynchronous request processing feature is <<mvc-ann-async-configuration, enabled>>
in the Servlet container, controller methods can wrap any supported controller method
@@ -4425,7 +4423,6 @@ example, in response to an external event (JMS message), a scheduled task, or ot
[[mvc-ann-async-callable]]
=== `Callable`
[.small]#<<mvc-ann-async-vs-webflux, Compared to WebFlux>>#
A controller can wrap any supported return value with `java.util.concurrent.Callable`,
as the following example shows:
@@ -4455,7 +4452,6 @@ The return value can then be obtained by running the given task through the
[[mvc-ann-async-processing]]
=== Processing
[.small]#<<mvc-ann-async-vs-webflux, Compared to WebFlux>>#
Here is a very concise overview of Servlet asynchronous request processing:
@@ -4533,7 +4529,7 @@ methods for timeout and completion callbacks.
[[mvc-ann-async-vs-webflux]]
==== Compared to WebFlux
==== Async Spring MVC compared to WebFlux
The Servlet API was originally built for making a single pass through the Filter-Servlet
chain. Asynchronous request processing lets applications exit the Filter-Servlet chain
@@ -4560,11 +4556,13 @@ types in controller method arguments (for example, `@RequestBody`, `@RequestPart
nor does it have any explicit support for asynchronous and reactive types as model attributes.
Spring WebFlux does support all that.
Finally, from a configuration perspective the asynchronous request processing feature must be
<<mvc-ann-async-configuration, enabled at the Servlet container level>>.
[[mvc-ann-async-http-streaming]]
=== HTTP Streaming
[.small]#<<web-reactive.adoc#webflux-codecs-streaming, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-codecs-streaming, See equivalent in the Reactive stack>>#
You can use `DeferredResult` and `Callable` for a single asynchronous return value.
What if you want to produce multiple asynchronous values and have those written to the
@@ -4717,7 +4715,7 @@ customize the status and headers of the response.
[[mvc-ann-async-reactive-types]]
=== Reactive Types
[.small]#<<web-reactive.adoc#webflux-codecs-streaming, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-codecs-streaming, See equivalent in the Reactive stack>>#
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).
@@ -4792,7 +4790,7 @@ Propagation library.
[[mvc-ann-async-disconnects]]
=== Disconnects
[.small]#<<web-reactive.adoc#webflux-codecs-streaming, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-codecs-streaming, See equivalent in the Reactive stack>>#
The Servlet API does not provide any notification when a remote client goes away.
Therefore, while streaming to the response, whether through <<mvc-ann-async-sse, SseEmitter>>
@@ -4809,7 +4807,6 @@ that have a built-in heartbeat mechanism.
[[mvc-ann-async-configuration]]
=== Configuration
[.small]#<<mvc-ann-async-vs-webflux, Compared to WebFlux>>#
The asynchronous request processing feature must be enabled at the Servlet container level.
The MVC configuration also exposes several options for asynchronous requests.
@@ -4859,7 +4856,7 @@ include::webmvc-cors.adoc[leveloffset=+1]
[[mvc-ann-rest-exceptions]]
== Error Responses
[.small]#<<web-reactive.adoc#webflux-ann-rest-exceptions, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-ann-rest-exceptions, See equivalent in the Reactive stack>>#
A common requirement for REST services is to include details in the body of error
responses. The Spring Framework supports the "Problem Details for HTTP APIs"
@@ -4883,7 +4880,7 @@ and any `ErrorResponseException`, and renders an error response with a body.
[[mvc-ann-rest-exceptions-render]]
=== Render
[.small]#<<web-reactive.adoc#webflux-ann-rest-exceptions-render, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-ann-rest-exceptions-render, See equivalent in the Reactive stack>>#
You can return `ProblemDetail` or `ErrorResponse` from any `@ExceptionHandler` or from
any `@RequestMapping` method to render an RFC 7807 response. This is processed as follows:
@@ -4906,7 +4903,7 @@ use a protected method to map any exception to a `ProblemDetail`.
[[mvc-ann-rest-exceptions-non-standard]]
=== Non-Standard Fields
[.small]#<<web-reactive.adoc#webflux-ann-rest-exceptions-non-standard, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-ann-rest-exceptions-non-standard, See equivalent in the Reactive stack>>#
You can extend an RFC 7807 response with non-standard fields in one of two ways.
@@ -4926,7 +4923,7 @@ from an existing `ProblemDetail`. This could be done centrally, e.g. from an
[[mvc-ann-rest-exceptions-i18n]]
=== Internationalization
[.small]#<<web-reactive.adoc#webflux-ann-rest-exceptions-i18n, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-ann-rest-exceptions-i18n, See equivalent in the Reactive stack>>#
It is a common requirement to internationalize error response details, and good practice
to customize the problem details for Spring MVC exceptions. This is supported as follows:
@@ -5036,7 +5033,7 @@ qualified exception class name.
[[mvc-ann-rest-exceptions-client]]
=== Client Handling
[.small]#<<web-reactive.adoc#webflux-ann-rest-exceptions-client, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-ann-rest-exceptions-client, See equivalent in the Reactive stack>>#
A client application can catch `WebClientResponseException`, when using the `WebClient`,
or `RestClientResponseException` when using the `RestTemplate`, and use their
@@ -5047,7 +5044,7 @@ or `RestClientResponseException` when using the `RestTemplate`, and use their
[[mvc-web-security]]
== Web Security
[.small]#<<web-reactive.adoc#webflux-web-security, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-web-security, See equivalent in the Reactive stack>>#
The https://spring.io/projects/spring-security[Spring Security] project provides support
for protecting web applications from malicious exploits. See the Spring Security
@@ -5065,7 +5062,7 @@ https://hdiv.org/[HDIV] is another web security framework that integrates with S
[[mvc-caching]]
== HTTP Caching
[.small]#<<web-reactive.adoc#webflux-caching, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-caching, See equivalent in the Reactive stack>>#
HTTP caching can significantly improve the performance of a web application. HTTP caching
revolves around the `Cache-Control` response header and, subsequently, conditional request
@@ -5081,7 +5078,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, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-caching-cachecontrol, See equivalent in the Reactive stack>>#
{api-spring-framework}/http/CacheControl.html[`CacheControl`] provides support for
configuring settings related to the `Cache-Control` header and is accepted as an argument
@@ -5137,7 +5134,7 @@ works as follows:
[[mvc-caching-etag-lastmodified]]
=== Controllers
[.small]#<<web-reactive.adoc#webflux-caching-etag-lastmodified, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-caching-etag-lastmodified, See equivalent in the Reactive stack>>#
Controllers can add explicit support for HTTP caching. We recommended doing so, since the
`lastModified` or `ETag` value for a resource needs to be calculated before it can be compared
@@ -5238,7 +5235,7 @@ to 412 (PRECONDITION_FAILED), to prevent concurrent modification.
[[mvc-caching-static-resources]]
=== Static Resources
[.small]#<<web-reactive.adoc#webflux-caching-static-resources, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-caching-static-resources, See equivalent in the Reactive stack>>#
You should serve static resources with a `Cache-Control` and conditional response headers
for optimal performance. See the section on configuring <<mvc-config-static-resources>>.
@@ -5258,7 +5255,7 @@ include::webmvc-view.adoc[leveloffset=+1]
[[mvc-config]]
== MVC Config
[.small]#<<web-reactive.adoc#webflux-config, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-config, See equivalent in the Reactive stack>>#
The MVC Java configuration and the MVC XML namespace provide default configuration
suitable for most applications and a configuration API to customize it.
@@ -5274,7 +5271,7 @@ and <<mvc-servlet-config>>.
[[mvc-config-enable]]
=== Enable MVC Configuration
[.small]#<<web-reactive.adoc#webflux-config-enable, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-config-enable, See equivalent in the Reactive stack>>#
In Java configuration, you can use the `@EnableWebMvc` annotation to enable MVC
configuration, as the following example shows:
@@ -5323,7 +5320,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, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-config-customize, See equivalent in the Reactive stack>>#
In Java configuration, you can implement the `WebMvcConfigurer` interface, as the
following example shows:
@@ -5359,7 +5356,7 @@ sub-elements are available.
[[mvc-config-conversion]]
=== Type Conversion
[.small]#<<web-reactive.adoc#webflux-config-conversion, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-config-conversion, See equivalent in the Reactive stack>>#
By default, formatters for various number and date types are installed, along with support
for customization via `@NumberFormat` and `@DateTimeFormat` on fields.
@@ -5474,7 +5471,7 @@ FormatterRegistrar implementations.
[[mvc-config-validation]]
=== Validation
[.small]#<<web-reactive.adoc#webflux-config-validation, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-config-validation, See equivalent in the Reactive stack>>#
By default, if <<core.adoc#validation-beanvalidation-overview, Bean Validation>> is present
on the classpath (for example, Hibernate Validator), the `LocalValidatorFactoryBean` is
@@ -5624,7 +5621,7 @@ unwanted characters in URL paths.
[[mvc-config-content-negotiation]]
=== Content Types
[.small]#<<web-reactive.adoc#webflux-config-content-negotiation, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-config-content-negotiation, See equivalent in the Reactive stack>>#
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).
@@ -5688,7 +5685,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, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-config-message-codecs, See equivalent in the Reactive stack>>#
You can customize `HttpMessageConverter` in Java configuration by overriding
{api-spring-framework}/web/servlet/config/annotation/WebMvcConfigurer.html#configureMessageConverters-java.util.List-[`configureMessageConverters()`]
@@ -5841,7 +5838,7 @@ splitting URL handling across an annotated controller and a view controller.
[[mvc-config-view-resolvers]]
=== View Resolvers
[.small]#<<web-reactive.adoc#webflux-config-view-resolvers, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-config-view-resolvers, See equivalent in the Reactive stack>>#
The MVC configuration simplifies the registration of view resolvers.
@@ -5959,7 +5956,7 @@ as the following example shows:
[[mvc-config-static-resources]]
=== Static Resources
[.small]#<<web-reactive.adoc#webflux-config-static-resources, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-config-static-resources, See equivalent in the Reactive stack>>#
This option provides a convenient way to serve static resources from a list of
{api-spring-framework}/core/io/Resource.html[`Resource`]-based locations.
@@ -6199,7 +6196,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, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-config-path-matching, See equivalent in the Reactive stack>>#
You can customize options related to path matching and treatment of the URL.
For details on the individual options, see the
@@ -6259,7 +6256,7 @@ The following example shows how to customize path matching in XML configuration:
[[mvc-config-advanced-java]]
=== Advanced Java Config
[.small]#<<web-reactive.adoc#webflux-config-advanced-java, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-config-advanced-java, See equivalent in the Reactive stack>>#
`@EnableWebMvc` imports `DelegatingWebMvcConfiguration`, which:
@@ -6334,7 +6331,7 @@ by letting it be detected through a `<component-scan/>` declaration.
[[mvc-http2]]
== HTTP/2
[.small]#<<web-reactive.adoc#webflux-http2, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-http2, See equivalent in the Reactive stack>>#
Servlet 4 containers are required to support HTTP/2, and Spring Framework 5 is compatible
with Servlet API 4. From a programming model perspective, there is nothing specific that
@@ -84,7 +84,7 @@ In the absence of that, they need to come up with their own conventions.
== When to Use WebSockets
WebSockets can make a web page be dynamic and interactive. However, in many cases,
a combination of Ajax and HTTP streaming or long polling can provide a simple and
a combination of AJAX and HTTP streaming or long polling can provide a simple and
effective solution.
For example, news, mail, and social feeds need to update dynamically, but it may be
@@ -1,6 +1,6 @@
[[websocket]]
= WebSockets
[.small]#<<web-reactive.adoc#webflux-websocket, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-websocket, See equivalent in the Reactive stack>>#
This part of the reference documentation covers support for Servlet stack, WebSocket
messaging that includes raw WebSocket interactions, WebSocket emulation through SockJS, and
@@ -13,7 +13,7 @@ include::websocket-intro.adoc[leveloffset=+1]
[[websocket-server]]
== WebSocket API
[.small]#<<web-reactive.adoc#webflux-websocket-server, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-websocket-server, See equivalent in the Reactive stack>>#
The Spring Framework provides a WebSocket API that you can use to write client- and
server-side applications that handle WebSocket messages.
@@ -22,7 +22,7 @@ server-side applications that handle WebSocket messages.
[[websocket-server-handler]]
=== `WebSocketHandler`
[.small]#<<web-reactive.adoc#webflux-websocket-server-handler, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-websocket-server-handler, See equivalent in the Reactive stack>>#
Creating a WebSocket server is as simple as implementing `WebSocketHandler` or, more
likely, extending either `TextWebSocketHandler` or `BinaryWebSocketHandler`. The following
@@ -108,7 +108,7 @@ sending. One option is to wrap the `WebSocketSession` with
[[websocket-server-handshake]]
=== WebSocket Handshake
[.small]#<<web-reactive.adoc#webflux-websocket-server-handshake, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-websocket-server-handshake, See equivalent in the Reactive stack>>#
The easiest way to customize the initial HTTP WebSocket handshake request is through
a `HandshakeInterceptor`, which exposes methods for "`before`" and "`after`" the handshake.
@@ -242,7 +242,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, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-websocket-server-config, See equivalent in the Reactive stack>>#
Each underlying WebSocket engine exposes configuration properties that control
runtime characteristics, such as the size of message buffer sizes, idle timeout,
@@ -365,7 +365,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, WebFlux>>#
[.small]#<<web-reactive.adoc#webflux-websocket-server-cors, See equivalent in the Reactive stack>>#
As of Spring Framework 4.1.5, the default behavior for WebSocket and SockJS is to accept
only same-origin requests. It is also possible to allow all or a specified list of origins.
+19 -19
View File
@@ -8,18 +8,18 @@ javaPlatform {
dependencies {
api(platform("com.fasterxml.jackson:jackson-bom:2.14.1"))
api(platform("io.micrometer:micrometer-bom:1.10.2"))
api(platform("io.netty:netty-bom:4.1.85.Final"))
api(platform("io.micrometer:micrometer-bom:1.10.3"))
api(platform("io.netty:netty-bom:4.1.86.Final"))
api(platform("io.netty:netty5-bom:5.0.0.Alpha5"))
api(platform("io.projectreactor:reactor-bom:2022.0.1"))
api(platform("io.projectreactor:reactor-bom:2022.0.2"))
api(platform("io.rsocket:rsocket-bom:1.1.3"))
api(platform("org.apache.groovy:groovy-bom:4.0.6"))
api(platform("org.apache.logging.log4j:log4j-bom:2.19.0"))
api(platform("org.eclipse.jetty:jetty-bom:11.0.12"))
api(platform("org.eclipse.jetty:jetty-bom:11.0.13"))
api(platform("org.jetbrains.kotlinx:kotlinx-coroutines-bom:1.6.4"))
api(platform("org.jetbrains.kotlinx:kotlinx-serialization-bom:1.4.0"))
api(platform("org.junit:junit-bom:5.9.1"))
api(platform("org.mockito:mockito-bom:4.9.0"))
api(platform("org.junit:junit-bom:5.9.2"))
api(platform("org.mockito:mockito-bom:4.9.0")) // spring-beans tests fail with 4.10+
constraints {
api("com.fasterxml:aalto-xml:1.3.1")
@@ -29,7 +29,7 @@ dependencies {
api("com.google.code.findbugs:findbugs:3.0.1")
api("com.google.code.findbugs:jsr305:3.0.2")
api("com.google.code.gson:gson:2.10")
api("com.google.protobuf:protobuf-java-util:3.21.11")
api("com.google.protobuf:protobuf-java-util:3.21.12")
api("com.googlecode.protobuf-java-format:protobuf-java-format:1.4")
api("com.h2database:h2:2.1.214")
api("com.jayway.jsonpath:json-path:2.7.0")
@@ -55,9 +55,9 @@ dependencies {
api("io.r2dbc:r2dbc-spi:1.0.0.RELEASE")
api("io.reactivex.rxjava3:rxjava:3.1.5")
api("io.smallrye.reactive:mutiny:1.8.0")
api("io.undertow:undertow-core:2.3.0.Final")
api("io.undertow:undertow-servlet:2.3.0.Final")
api("io.undertow:undertow-websockets-jsr:2.3.0.Final")
api("io.undertow:undertow-core:2.3.2.Final")
api("io.undertow:undertow-servlet:2.3.2.Final")
api("io.undertow:undertow-websockets-jsr:2.3.2.Final")
api("io.vavr:vavr:0.10.4")
api("jakarta.activation:jakarta.activation-api:2.0.1")
api("jakarta.annotation:jakarta.annotation-api:2.0.0")
@@ -87,7 +87,7 @@ dependencies {
api("jaxen:jaxen:1.2.0")
api("junit:junit:4.13.2")
api("net.sf.jopt-simple:jopt-simple:5.0.4")
api("net.sourceforge.htmlunit:htmlunit:2.67.0")
api("net.sourceforge.htmlunit:htmlunit:2.69.0")
api("org.apache-extras.beanshell:bsh:2.0b6")
api("org.apache.activemq:activemq-broker:5.16.2")
api("org.apache.activemq:activemq-kahadb-store:5.16.2")
@@ -96,17 +96,17 @@ dependencies {
api("org.apache.derby:derby:10.16.1.1")
api("org.apache.derby:derbyclient:10.16.1.1")
api("org.apache.derby:derbytools:10.16.1.1")
api("org.apache.httpcomponents.client5:httpclient5:5.2")
api("org.apache.httpcomponents.client5:httpclient5:5.2.1")
api("org.apache.httpcomponents.core5:httpcore5-reactive:5.2")
api("org.apache.poi:poi-ooxml:5.2.3")
api("org.apache.tomcat.embed:tomcat-embed-core:10.1.1")
api("org.apache.tomcat.embed:tomcat-embed-websocket:10.1.1")
api("org.apache.tomcat:tomcat-util:10.1.1")
api("org.apache.tomcat:tomcat-websocket:10.1.1")
api("org.apache.tomcat.embed:tomcat-embed-core:10.1.4")
api("org.apache.tomcat.embed:tomcat-embed-websocket:10.1.4")
api("org.apache.tomcat:tomcat-util:10.1.4")
api("org.apache.tomcat:tomcat-websocket:10.1.4")
api("org.aspectj:aspectjrt:1.9.9.1")
api("org.aspectj:aspectjtools:1.9.9.1")
api("org.aspectj:aspectjweaver:1.9.9.1")
api("org.assertj:assertj-core:3.23.1")
api("org.assertj:assertj-core:3.24.1")
api("org.awaitility:awaitility:3.1.6")
api("org.bouncycastle:bcpkix-jdk18on:1.71")
api("org.codehaus.jettison:jettison:1.3.8")
@@ -134,10 +134,10 @@ dependencies {
api("org.ogce:xpp3:1.1.6")
api("org.python:jython-standalone:2.7.1")
api("org.quartz-scheduler:quartz:2.3.2")
api("org.seleniumhq.selenium:htmlunit-driver:2.66.0")
api("org.seleniumhq.selenium:htmlunit-driver:2.67.0")
api("org.seleniumhq.selenium:selenium-java:3.141.59")
api("org.skyscreamer:jsonassert:1.5.0")
api("org.slf4j:slf4j-api:2.0.5")
api("org.slf4j:slf4j-api:2.0.6")
api("org.testng:testng:7.6.1")
api("org.webjars:underscorejs:1.8.3")
api("org.webjars:webjars-locator-core:0.52")
+1 -1
View File
@@ -1,4 +1,4 @@
version=6.0.3-SNAPSHOT
version=6.0.4
org.gradle.caching=true
org.gradle.jvmargs=-Xmx2048m
+2 -2
View File
@@ -7,8 +7,8 @@ pluginManagement {
}
plugins {
id "com.gradle.enterprise" version "3.11.1"
id "io.spring.ge.conventions" version "0.0.11"
id "com.gradle.enterprise" version "3.12.1"
id "io.spring.ge.conventions" version "0.0.13"
}
include "spring-aop"
@@ -655,10 +655,10 @@ public abstract class AbstractAspectJAdvice implements Advice, AspectJPrecedence
@Nullable
protected JoinPointMatch getJoinPointMatch() {
MethodInvocation mi = ExposeInvocationInterceptor.currentInvocation();
if (!(mi instanceof ProxyMethodInvocation)) {
if (!(mi instanceof ProxyMethodInvocation pmi)) {
throw new IllegalStateException("MethodInvocation is not a Spring ProxyMethodInvocation: " + mi);
}
return getJoinPointMatch((ProxyMethodInvocation) mi);
return getJoinPointMatch(pmi);
}
// Note: We can't use JoinPointMatch.getClass().getName() as the key, since
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -61,12 +61,12 @@ public abstract class AspectJAopUtils {
*/
@Nullable
public static AspectJPrecedenceInformation getAspectJPrecedenceInformationFor(Advisor anAdvisor) {
if (anAdvisor instanceof AspectJPrecedenceInformation) {
return (AspectJPrecedenceInformation) anAdvisor;
if (anAdvisor instanceof AspectJPrecedenceInformation ajpi) {
return ajpi;
}
Advice advice = anAdvisor.getAdvice();
if (advice instanceof AspectJPrecedenceInformation) {
return (AspectJPrecedenceInformation) advice;
if (advice instanceof AspectJPrecedenceInformation ajpi) {
return ajpi;
}
return null;
}
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -200,8 +200,8 @@ public class AspectJExpressionPointcut extends AbstractExpressionPointcut
*/
@Nullable
private ClassLoader determinePointcutClassLoader() {
if (this.beanFactory instanceof ConfigurableBeanFactory) {
return ((ConfigurableBeanFactory) this.beanFactory).getBeanClassLoader();
if (this.beanFactory instanceof ConfigurableBeanFactory cbf) {
return cbf.getBeanClassLoader();
}
if (this.pointcutDeclarationScope != null) {
return this.pointcutDeclarationScope.getClassLoader();
@@ -335,10 +335,10 @@ public class AspectJExpressionPointcut extends AbstractExpressionPointcut
try {
MethodInvocation mi = ExposeInvocationInterceptor.currentInvocation();
targetObject = mi.getThis();
if (!(mi instanceof ProxyMethodInvocation)) {
if (!(mi instanceof ProxyMethodInvocation _pmi)) {
throw new IllegalStateException("MethodInvocation is not a Spring ProxyMethodInvocation: " + mi);
}
pmi = (ProxyMethodInvocation) mi;
pmi = _pmi;
thisObject = pmi.getProxy();
}
catch (IllegalStateException ex) {
@@ -404,8 +404,8 @@ public class AspectJExpressionPointcut extends AbstractExpressionPointcut
}
private RuntimeTestWalker getRuntimeTestWalker(ShadowMatch shadowMatch) {
if (shadowMatch instanceof DefensiveShadowMatch) {
return new RuntimeTestWalker(((DefensiveShadowMatch) shadowMatch).primary);
if (shadowMatch instanceof DefensiveShadowMatch defensiveShadowMatch) {
return new RuntimeTestWalker(defensiveShadowMatch.primary);
}
return new RuntimeTestWalker(shadowMatch);
}
@@ -422,7 +422,8 @@ public class AspectJExpressionPointcut extends AbstractExpressionPointcut
private ShadowMatch getTargetShadowMatch(Method method, Class<?> targetClass) {
Method targetMethod = AopUtils.getMostSpecificMethod(method, targetClass);
if (targetMethod.getDeclaringClass().isInterface()) {
if (targetMethod.getDeclaringClass().isInterface() && targetMethod.getDeclaringClass() != targetClass &&
obtainPointcutExpression().getPointcutExpression().contains("." + targetMethod.getName() + "(")) {
// Try to build the most specific interface possible for inherited methods to be
// considered for sub-interface matches as well, in particular for proxy classes.
// Note: AspectJ is only going to take Method.getDeclaringClass() into account.
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -71,8 +71,8 @@ public abstract class AspectJProxyUtils {
private static boolean isAspectJAdvice(Advisor advisor) {
return (advisor instanceof InstantiationModelAwarePointcutAdvisor ||
advisor.getAdvice() instanceof AbstractAspectJAdvice ||
(advisor instanceof PointcutAdvisor &&
((PointcutAdvisor) advisor).getPointcut() instanceof AspectJExpressionPointcut));
(advisor instanceof PointcutAdvisor pointcutAdvisor &&
pointcutAdvisor.getPointcut() instanceof AspectJExpressionPointcut));
}
static boolean isVariableName(@Nullable String name) {
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -202,8 +202,8 @@ class RuntimeTestWalker {
}
Class<?> typeClass = null;
ResolvedType type = (ResolvedType) i.getType();
if (type instanceof ReferenceType) {
ReferenceTypeDelegate delegate = ((ReferenceType) type).getDelegate();
if (type instanceof ReferenceType referenceType) {
ReferenceTypeDelegate delegate = referenceType.getDelegate();
if (delegate instanceof ReflectionBasedReferenceTypeDelegate) {
try {
ReflectionUtils.makeAccessible(myClassField);
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -69,8 +69,8 @@ public class SingletonAspectInstanceFactory implements AspectInstanceFactory, Se
*/
@Override
public int getOrder() {
if (this.aspectInstance instanceof Ordered) {
return ((Ordered) this.aspectInstance).getOrder();
if (this.aspectInstance instanceof Ordered ordered) {
return ordered.getOrder();
}
return getOrderForAspectClass(this.aspectInstance.getClass());
}
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -117,9 +117,9 @@ public class TypePatternClassFilter implements ClassFilter {
}
@Override
public boolean equals(Object other) {
return (this == other || (other instanceof TypePatternClassFilter &&
ObjectUtils.nullSafeEquals(this.typePattern, ((TypePatternClassFilter) other).typePattern)));
public boolean equals(Object obj) {
return (this == obj || (obj instanceof TypePatternClassFilter that &&
ObjectUtils.nullSafeEquals(this.typePattern, that.typePattern)));
}
@Override
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -93,9 +93,8 @@ public class BeanFactoryAspectInstanceFactory implements MetadataAwareAspectInst
@Override
@Nullable
public ClassLoader getAspectClassLoader() {
return (this.beanFactory instanceof ConfigurableBeanFactory ?
((ConfigurableBeanFactory) this.beanFactory).getBeanClassLoader() :
ClassUtils.getDefaultClassLoader());
return (this.beanFactory instanceof ConfigurableBeanFactory cbf ?
cbf.getBeanClassLoader() : ClassUtils.getDefaultClassLoader());
}
@Override
@@ -110,11 +109,11 @@ public class BeanFactoryAspectInstanceFactory implements MetadataAwareAspectInst
// Rely on singleton semantics provided by the factory -> no local lock.
return null;
}
else if (this.beanFactory instanceof ConfigurableBeanFactory) {
else if (this.beanFactory instanceof ConfigurableBeanFactory cbf) {
// No singleton guarantees from the factory -> let's lock locally but
// reuse the factory's singleton lock, just in case a lazy dependency
// of our advice bean happens to trigger the singleton lock implicitly...
return ((ConfigurableBeanFactory) this.beanFactory).getSingletonMutex();
return cbf.getSingletonMutex();
}
else {
return this;
@@ -274,8 +274,8 @@ final class InstantiationModelAwarePointcutAdvisorImpl
this.declaredPointcut = declaredPointcut;
this.preInstantiationPointcut = preInstantiationPointcut;
if (aspectInstanceFactory instanceof LazySingletonAspectInstanceFactoryDecorator) {
this.aspectInstanceFactory = (LazySingletonAspectInstanceFactoryDecorator) aspectInstanceFactory;
if (aspectInstanceFactory instanceof LazySingletonAspectInstanceFactoryDecorator lazyFactory) {
this.aspectInstanceFactory = lazyFactory;
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -93,8 +93,8 @@ public abstract class AbstractInterceptorDrivenBeanDefinitionDecorator implement
// copy autowire settings from original bean definition.
proxyDefinition.setAutowireCandidate(targetDefinition.isAutowireCandidate());
proxyDefinition.setPrimary(targetDefinition.isPrimary());
if (targetDefinition instanceof AbstractBeanDefinition) {
proxyDefinition.copyQualifiersFrom((AbstractBeanDefinition) targetDefinition);
if (targetDefinition instanceof AbstractBeanDefinition abd) {
proxyDefinition.copyQualifiersFrom(abd);
}
// wrap it in a BeanDefinitionHolder with bean name
result = new BeanDefinitionHolder(proxyDefinition, existingBeanName);
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -153,13 +153,13 @@ class ConfigBeanDefinitionParser implements BeanDefinitionParser {
}
Object pointcut = parsePointcutProperty(advisorElement, parserContext);
if (pointcut instanceof BeanDefinition) {
if (pointcut instanceof BeanDefinition beanDefinition) {
advisorDef.getPropertyValues().add(POINTCUT, pointcut);
parserContext.registerComponent(
new AdvisorComponentDefinition(advisorBeanName, advisorDef, (BeanDefinition) pointcut));
new AdvisorComponentDefinition(advisorBeanName, advisorDef, beanDefinition));
}
else if (pointcut instanceof String) {
advisorDef.getPropertyValues().add(POINTCUT, new RuntimeBeanReference((String) pointcut));
else if (pointcut instanceof String beanName) {
advisorDef.getPropertyValues().add(POINTCUT, new RuntimeBeanReference(beanName));
parserContext.registerComponent(
new AdvisorComponentDefinition(advisorBeanName, advisorDef));
}
@@ -389,12 +389,12 @@ class ConfigBeanDefinitionParser implements BeanDefinitionParser {
cav.addIndexedArgumentValue(METHOD_INDEX, methodDef);
Object pointcut = parsePointcutProperty(adviceElement, parserContext);
if (pointcut instanceof BeanDefinition) {
if (pointcut instanceof BeanDefinition beanDefinition) {
cav.addIndexedArgumentValue(POINTCUT_INDEX, pointcut);
beanDefinitions.add((BeanDefinition) pointcut);
beanDefinitions.add(beanDefinition);
}
else if (pointcut instanceof String) {
RuntimeBeanReference pointcutRef = new RuntimeBeanReference((String) pointcut);
else if (pointcut instanceof String beanName) {
RuntimeBeanReference pointcutRef = new RuntimeBeanReference(beanName);
cav.addIndexedArgumentValue(POINTCUT_INDEX, pointcutRef);
beanReferences.add(pointcutRef);
}
@@ -71,8 +71,8 @@ public class SimpleBeanFactoryAwareAspectInstanceFactory implements AspectInstan
@Override
@Nullable
public ClassLoader getAspectClassLoader() {
if (this.beanFactory instanceof ConfigurableBeanFactory) {
return ((ConfigurableBeanFactory) this.beanFactory).getBeanClassLoader();
if (this.beanFactory instanceof ConfigurableBeanFactory cbf) {
return cbf.getBeanClassLoader();
}
else {
return ClassUtils.getDefaultClassLoader();
@@ -74,8 +74,9 @@ public abstract class AbstractAdvisingBeanPostProcessor extends ProxyProcessorSu
// Use original ClassLoader if bean class not locally loaded in overriding class loader
ClassLoader classLoader = getProxyClassLoader();
if (classLoader instanceof SmartClassLoader && classLoader != beanClass.getClassLoader()) {
classLoader = ((SmartClassLoader) classLoader).getOriginalClassLoader();
if (classLoader instanceof SmartClassLoader smartClassLoader &&
classLoader != beanClass.getClassLoader()) {
classLoader = smartClassLoader.getOriginalClassLoader();
}
return proxyFactory.getProxyClass(classLoader);
}
@@ -113,8 +114,9 @@ public abstract class AbstractAdvisingBeanPostProcessor extends ProxyProcessorSu
// Use original ClassLoader if bean class not locally loaded in overriding class loader
ClassLoader classLoader = getProxyClassLoader();
if (classLoader instanceof SmartClassLoader && classLoader != bean.getClass().getClassLoader()) {
classLoader = ((SmartClassLoader) classLoader).getOriginalClassLoader();
if (classLoader instanceof SmartClassLoader smartClassLoader &&
classLoader != bean.getClass().getClassLoader()) {
classLoader = smartClassLoader.getOriginalClassLoader();
}
return proxyFactory.getProxy(classLoader);
}
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -194,8 +194,8 @@ public abstract class AbstractSingletonProxyFactoryBean extends ProxyConfig
* @return a TargetSource for this object
*/
protected TargetSource createTargetSource(Object target) {
if (target instanceof TargetSource) {
return (TargetSource) target;
if (target instanceof TargetSource targetSource) {
return targetSource;
}
else {
return new SingletonTargetSource(target);
@@ -229,8 +229,8 @@ public abstract class AbstractSingletonProxyFactoryBean extends ProxyConfig
if (this.proxyInterfaces != null && this.proxyInterfaces.length == 1) {
return this.proxyInterfaces[0];
}
if (this.target instanceof TargetSource) {
return ((TargetSource) this.target).getTargetClass();
if (this.target instanceof TargetSource targetSource) {
return targetSource.getTargetClass();
}
if (this.target != null) {
return this.target.getClass();
@@ -258,8 +258,8 @@ public class AdvisedSupport extends ProxyConfig implements Advised {
@Override
public void addAdvisor(int pos, Advisor advisor) throws AopConfigException {
if (advisor instanceof IntroductionAdvisor) {
validateIntroductionAdvisor((IntroductionAdvisor) advisor);
if (advisor instanceof IntroductionAdvisor introductionAdvisor) {
validateIntroductionAdvisor(introductionAdvisor);
}
addAdvisorInternal(pos, advisor);
}
@@ -287,9 +287,9 @@ public class AdvisedSupport extends ProxyConfig implements Advised {
}
Advisor advisor = this.advisors.remove(index);
if (advisor instanceof IntroductionAdvisor ia) {
if (advisor instanceof IntroductionAdvisor introductionAdvisor) {
// We need to remove introduction interfaces.
for (Class<?> ifc : ia.getInterfaces()) {
for (Class<?> ifc : introductionAdvisor.getInterfaces()) {
removeInterface(ifc);
}
}
@@ -387,10 +387,10 @@ public class AdvisedSupport extends ProxyConfig implements Advised {
@Override
public void addAdvice(int pos, Advice advice) throws AopConfigException {
Assert.notNull(advice, "Advice must not be null");
if (advice instanceof IntroductionInfo) {
if (advice instanceof IntroductionInfo introductionInfo) {
// We don't need an IntroductionAdvisor for this kind of introduction:
// It's fully self-describing.
addAdvisor(pos, new DefaultIntroductionAdvisor(advice, (IntroductionInfo) advice));
addAdvisor(pos, new DefaultIntroductionAdvisor(advice, introductionInfo));
}
else if (advice instanceof DynamicIntroductionAdvice) {
// We need an IntroductionAdvisor for this kind of introduction.
@@ -506,8 +506,8 @@ public class AdvisedSupport extends ProxyConfig implements Advised {
this.advisorChainFactory = other.advisorChainFactory;
this.interfaces = new ArrayList<>(other.interfaces);
for (Advisor advisor : advisors) {
if (advisor instanceof IntroductionAdvisor) {
validateIntroductionAdvisor((IntroductionAdvisor) advisor);
if (advisor instanceof IntroductionAdvisor introductionAdvisor) {
validateIntroductionAdvisor(introductionAdvisor);
}
Assert.notNull(advisor, "Advisor must not be null");
this.advisors.add(advisor);
@@ -580,8 +580,8 @@ public class AdvisedSupport extends ProxyConfig implements Advised {
@Override
public boolean equals(@Nullable Object other) {
return (this == other || (other instanceof MethodCacheKey &&
this.method == ((MethodCacheKey) other).method));
return (this == other || (other instanceof MethodCacheKey methodCacheKey &&
this.method == methodCacheKey.method));
}
@Override
@@ -188,8 +188,8 @@ class CglibAopProxy implements AopProxy, Serializable {
Enhancer enhancer = createEnhancer();
if (classLoader != null) {
enhancer.setClassLoader(classLoader);
if (classLoader instanceof SmartClassLoader &&
((SmartClassLoader) classLoader).isClassReloadable(proxySuperClass)) {
if (classLoader instanceof SmartClassLoader smartClassLoader &&
smartClassLoader.isClassReloadable(proxySuperClass)) {
enhancer.setUseCache(false);
}
}
@@ -365,8 +365,8 @@ class CglibAopProxy implements AopProxy, Serializable {
@Override
public boolean equals(@Nullable Object other) {
return (this == other || (other instanceof CglibAopProxy &&
AopProxyUtils.equalsInProxy(this.advised, ((CglibAopProxy) other).advised)));
return (this == other || (other instanceof CglibAopProxy cglibAopProxy &&
AopProxyUtils.equalsInProxy(this.advised, cglibAopProxy.advised)));
}
@Override
@@ -590,12 +590,12 @@ class CglibAopProxy implements AopProxy, Serializable {
if (proxy == other) {
return true;
}
if (other instanceof Factory) {
Callback callback = ((Factory) other).getCallback(INVOKE_EQUALS);
if (!(callback instanceof EqualsInterceptor)) {
if (other instanceof Factory factory) {
Callback callback = factory.getCallback(INVOKE_EQUALS);
if (!(callback instanceof EqualsInterceptor equalsInterceptor)) {
return false;
}
AdvisedSupport otherAdvised = ((EqualsInterceptor) callback).advised;
AdvisedSupport otherAdvised = equalsInterceptor.advised;
return AopProxyUtils.equalsInProxy(this.advised, otherAdvised);
}
else {
@@ -719,8 +719,8 @@ class CglibAopProxy implements AopProxy, Serializable {
@Override
public boolean equals(@Nullable Object other) {
return (this == other ||
(other instanceof DynamicAdvisedInterceptor &&
this.advised.equals(((DynamicAdvisedInterceptor) other).advised)));
(other instanceof DynamicAdvisedInterceptor dynamicAdvisedInterceptor &&
this.advised.equals(dynamicAdvisedInterceptor.advised)));
}
/**
@@ -962,9 +962,9 @@ class CglibAopProxy implements AopProxy, Serializable {
private static boolean equalsPointcuts(Advisor a, Advisor b) {
// If only one of the advisor (but not both) is PointcutAdvisor, then it is a mismatch.
// Takes care of the situations where an IntroductionAdvisor is used (see SPR-3959).
return (!(a instanceof PointcutAdvisor) ||
(b instanceof PointcutAdvisor &&
ObjectUtils.nullSafeEquals(((PointcutAdvisor) a).getPointcut(), ((PointcutAdvisor) b).getPointcut())));
return (!(a instanceof PointcutAdvisor pointcutAdvisor1) ||
(b instanceof PointcutAdvisor pointcutAdvisor2 &&
ObjectUtils.nullSafeEquals(pointcutAdvisor1.getPointcut(), pointcutAdvisor2.getPointcut())));
}
@Override
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -65,11 +65,11 @@ public class DefaultAdvisorChainFactory implements AdvisorChainFactory, Serializ
if (config.isPreFiltered() || pointcutAdvisor.getPointcut().getClassFilter().matches(actualClass)) {
MethodMatcher mm = pointcutAdvisor.getPointcut().getMethodMatcher();
boolean match;
if (mm instanceof IntroductionAwareMethodMatcher) {
if (mm instanceof IntroductionAwareMethodMatcher iamm) {
if (hasIntroductions == null) {
hasIntroductions = hasMatchingIntroductions(advisors, actualClass);
}
match = ((IntroductionAwareMethodMatcher) mm).matches(method, actualClass, hasIntroductions);
match = iamm.matches(method, actualClass, hasIntroductions);
}
else {
match = mm.matches(method, actualClass);
@@ -262,15 +262,15 @@ final class JdkDynamicAopProxy implements AopProxy, InvocationHandler, Serializa
}
JdkDynamicAopProxy otherProxy;
if (other instanceof JdkDynamicAopProxy) {
otherProxy = (JdkDynamicAopProxy) other;
if (other instanceof JdkDynamicAopProxy jdkDynamicAopProxy) {
otherProxy = jdkDynamicAopProxy;
}
else if (Proxy.isProxyClass(other.getClass())) {
InvocationHandler ih = Proxy.getInvocationHandler(other);
if (!(ih instanceof JdkDynamicAopProxy)) {
if (!(ih instanceof JdkDynamicAopProxy jdkDynamicAopProxy)) {
return false;
}
otherProxy = (JdkDynamicAopProxy) ih;
otherProxy = jdkDynamicAopProxy;
}
else {
// Not a valid comparison...
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -55,8 +55,8 @@ public class AdvisorAdapterRegistrationManager implements BeanPostProcessor {
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof AdvisorAdapter){
this.advisorAdapterRegistry.registerAdvisorAdapter((AdvisorAdapter) bean);
if (bean instanceof AdvisorAdapter advisorAdapter) {
this.advisorAdapterRegistry.registerAdvisorAdapter(advisorAdapter);
}
return bean;
}
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -55,8 +55,8 @@ public class DefaultAdvisorAdapterRegistry implements AdvisorAdapterRegistry, Se
@Override
public Advisor wrap(Object adviceObject) throws UnknownAdviceTypeException {
if (adviceObject instanceof Advisor) {
return (Advisor) adviceObject;
if (adviceObject instanceof Advisor advisor) {
return advisor;
}
if (!(adviceObject instanceof Advice advice)) {
throw new UnknownAdviceTypeException(adviceObject);
@@ -78,8 +78,8 @@ public class DefaultAdvisorAdapterRegistry implements AdvisorAdapterRegistry, Se
public MethodInterceptor[] getInterceptors(Advisor advisor) throws UnknownAdviceTypeException {
List<MethodInterceptor> interceptors = new ArrayList<>(3);
Advice advice = advisor.getAdvice();
if (advice instanceof MethodInterceptor) {
interceptors.add((MethodInterceptor) advice);
if (advice instanceof MethodInterceptor methodInterceptor) {
interceptors.add(methodInterceptor);
}
for (AdvisorAdapter adapter : this.adapters) {
if (adapter.supportsAdvice(advice)) {
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -58,11 +58,11 @@ public abstract class AbstractAdvisorAutoProxyCreator extends AbstractAutoProxyC
@Override
public void setBeanFactory(BeanFactory beanFactory) {
super.setBeanFactory(beanFactory);
if (!(beanFactory instanceof ConfigurableListableBeanFactory)) {
if (!(beanFactory instanceof ConfigurableListableBeanFactory clbf)) {
throw new IllegalArgumentException(
"AdvisorAutoProxyCreator requires a ConfigurableListableBeanFactory: " + beanFactory);
}
initBeanFactory((ConfigurableListableBeanFactory) beanFactory);
initBeanFactory(clbf);
}
protected void initBeanFactory(ConfigurableListableBeanFactory beanFactory) {
@@ -473,8 +473,8 @@ public abstract class AbstractAutoProxyCreator extends ProxyProcessorSupport
private Object buildProxy(Class<?> beanClass, @Nullable String beanName,
@Nullable Object[] specificInterceptors, TargetSource targetSource, boolean classOnly) {
if (this.beanFactory instanceof ConfigurableListableBeanFactory) {
AutoProxyUtils.exposeTargetClass((ConfigurableListableBeanFactory) this.beanFactory, beanName, beanClass);
if (this.beanFactory instanceof ConfigurableListableBeanFactory clbf) {
AutoProxyUtils.exposeTargetClass(clbf, beanName, beanClass);
}
ProxyFactory proxyFactory = new ProxyFactory();
@@ -511,8 +511,8 @@ public abstract class AbstractAutoProxyCreator extends ProxyProcessorSupport
// Use original ClassLoader if bean class not locally loaded in overriding class loader
ClassLoader classLoader = getProxyClassLoader();
if (classLoader instanceof SmartClassLoader && classLoader != beanClass.getClassLoader()) {
classLoader = ((SmartClassLoader) classLoader).getOriginalClassLoader();
if (classLoader instanceof SmartClassLoader smartClassLoader && classLoader != beanClass.getClassLoader()) {
classLoader = smartClassLoader.getOriginalClassLoader();
}
return (classOnly ? proxyFactory.getProxyClass(classLoader) : proxyFactory.getProxy(classLoader));
}
@@ -527,8 +527,8 @@ public abstract class AbstractAutoProxyCreator extends ProxyProcessorSupport
* @see AutoProxyUtils#shouldProxyTargetClass
*/
protected boolean shouldProxyTargetClass(Class<?> beanClass, @Nullable String beanName) {
return (this.beanFactory instanceof ConfigurableListableBeanFactory &&
AutoProxyUtils.shouldProxyTargetClass((ConfigurableListableBeanFactory) this.beanFactory, beanName));
return (this.beanFactory instanceof ConfigurableListableBeanFactory clbf &&
AutoProxyUtils.shouldProxyTargetClass(clbf, beanName));
}
/**
@@ -592,7 +592,7 @@ public abstract class AbstractAutoProxyCreator extends ProxyProcessorSupport
*/
private Advisor[] resolveInterceptorNames() {
BeanFactory bf = this.beanFactory;
ConfigurableBeanFactory cbf = (bf instanceof ConfigurableBeanFactory ? (ConfigurableBeanFactory) bf : null);
ConfigurableBeanFactory cbf = (bf instanceof ConfigurableBeanFactory _cbf ? _cbf : null);
List<Advisor> advisors = new ArrayList<>();
for (String beanName : this.interceptorNames) {
if (cbf == null || !cbf.isCurrentlyInCreation(beanName)) {
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -46,8 +46,7 @@ public abstract class AbstractBeanFactoryAwareAdvisingPostProcessor extends Abst
@Override
public void setBeanFactory(BeanFactory beanFactory) {
this.beanFactory = (beanFactory instanceof ConfigurableListableBeanFactory ?
(ConfigurableListableBeanFactory) beanFactory : null);
this.beanFactory = (beanFactory instanceof ConfigurableListableBeanFactory clbf ? clbf : null);
}
@Override
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -67,11 +67,11 @@ public abstract class AbstractBeanFactoryBasedTargetSourceCreator
@Override
public final void setBeanFactory(BeanFactory beanFactory) {
if (!(beanFactory instanceof ConfigurableBeanFactory)) {
if (!(beanFactory instanceof ConfigurableBeanFactory clbf)) {
throw new IllegalStateException("Cannot do auto-TargetSource creation with a BeanFactory " +
"that doesn't implement ConfigurableBeanFactory: " + beanFactory.getClass());
}
this.beanFactory = (ConfigurableBeanFactory) beanFactory;
this.beanFactory = clbf;
}
/**
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -66,9 +66,8 @@ public class LazyInitTargetSourceCreator extends AbstractBeanFactoryBasedTargetS
protected AbstractBeanFactoryBasedTargetSource createBeanFactoryBasedTargetSource(
Class<?> beanClass, String beanName) {
if (getBeanFactory() instanceof ConfigurableListableBeanFactory) {
BeanDefinition definition =
((ConfigurableListableBeanFactory) getBeanFactory()).getBeanDefinition(beanName);
if (getBeanFactory() instanceof ConfigurableListableBeanFactory clbf) {
BeanDefinition definition = clbf.getBeanDefinition(beanName);
if (definition.isLazyInit()) {
return new LazyInitTargetSource();
}
@@ -182,8 +182,8 @@ public abstract class AsyncExecutionAspectSupport implements BeanFactoryAware {
if (targetExecutor == null) {
return null;
}
executor = (targetExecutor instanceof AsyncTaskExecutor ?
(AsyncTaskExecutor) targetExecutor : new TaskExecutorAdapter(targetExecutor));
executor = (targetExecutor instanceof AsyncTaskExecutor asyncTaskExecutor ?
asyncTaskExecutor : new TaskExecutorAdapter(targetExecutor));
this.executors.put(method, executor);
}
return executor;
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -113,8 +113,8 @@ public class AsyncExecutionInterceptor extends AsyncExecutionAspectSupport imple
Callable<Object> task = () -> {
try {
Object result = invocation.proceed();
if (result instanceof Future) {
return ((Future<?>) result).get();
if (result instanceof Future<?> future) {
return future.get();
}
}
catch (ExecutionException ex) {
@@ -75,7 +75,7 @@ class ScopedProxyBeanRegistrationAotProcessor implements BeanRegistrationAotProc
@Nullable
private String getTargetBeanName(BeanDefinition beanDefinition) {
Object value = beanDefinition.getPropertyValues().get("targetBeanName");
return (value instanceof String ? (String) value : null);
return (value instanceof String targetBeanName ? targetBeanName : null);
}
@Nullable
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -82,8 +82,8 @@ public abstract class AbstractBeanFactoryPointcutAdvisor extends AbstractPointcu
}
private void resetAdviceMonitor() {
if (this.beanFactory instanceof ConfigurableBeanFactory) {
this.adviceMonitor = ((ConfigurableBeanFactory) this.beanFactory).getSingletonMutex();
if (this.beanFactory instanceof ConfigurableBeanFactory cbf) {
this.adviceMonitor = cbf.getSingletonMutex();
}
else {
this.adviceMonitor = new Object();
@@ -107,8 +107,8 @@ public abstract class AopUtils {
public static Class<?> getTargetClass(Object candidate) {
Assert.notNull(candidate, "Candidate object must not be null");
Class<?> result = null;
if (candidate instanceof TargetClassAware) {
result = ((TargetClassAware) candidate).getTargetClass();
if (candidate instanceof TargetClassAware targetClassAware) {
result = targetClassAware.getTargetClass();
}
if (result == null) {
result = (isCglibProxy(candidate) ? candidate.getClass().getSuperclass() : candidate.getClass());
@@ -234,8 +234,8 @@ public abstract class AopUtils {
}
IntroductionAwareMethodMatcher introductionAwareMethodMatcher = null;
if (methodMatcher instanceof IntroductionAwareMethodMatcher) {
introductionAwareMethodMatcher = (IntroductionAwareMethodMatcher) methodMatcher;
if (methodMatcher instanceof IntroductionAwareMethodMatcher iamm) {
introductionAwareMethodMatcher = iamm;
}
Set<Class<?>> classes = new LinkedHashSet<>();
@@ -281,8 +281,8 @@ public abstract class AopUtils {
* @return whether the pointcut can apply on any method
*/
public static boolean canApply(Advisor advisor, Class<?> targetClass, boolean hasIntroductions) {
if (advisor instanceof IntroductionAdvisor) {
return ((IntroductionAdvisor) advisor).getClassFilter().matches(targetClass);
if (advisor instanceof IntroductionAdvisor ia) {
return ia.getClassFilter().matches(targetClass);
}
else if (advisor instanceof PointcutAdvisor pca) {
return canApply(pca.getPointcut(), targetClass, hasIntroductions);
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -109,9 +109,9 @@ public abstract class ClassFilters {
}
@Override
public boolean equals(@Nullable Object other) {
return (this == other || (other instanceof UnionClassFilter &&
ObjectUtils.nullSafeEquals(this.filters, ((UnionClassFilter) other).filters)));
public boolean equals(@Nullable Object obj) {
return (this == obj || (obj instanceof UnionClassFilter that &&
ObjectUtils.nullSafeEquals(this.filters, that.filters)));
}
@Override
@@ -150,9 +150,9 @@ public abstract class ClassFilters {
}
@Override
public boolean equals(@Nullable Object other) {
return (this == other || (other instanceof IntersectionClassFilter &&
ObjectUtils.nullSafeEquals(this.filters, ((IntersectionClassFilter) other).filters)));
public boolean equals(@Nullable Object obj) {
return (this == obj || (obj instanceof IntersectionClassFilter that &&
ObjectUtils.nullSafeEquals(this.filters, that.filters)));
}
@Override
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -56,7 +56,7 @@ public class DefaultIntroductionAdvisor implements IntroductionAdvisor, ClassFil
* @see #addInterface
*/
public DefaultIntroductionAdvisor(Advice advice) {
this(advice, (advice instanceof IntroductionInfo ? (IntroductionInfo) advice : null));
this(advice, (advice instanceof IntroductionInfo introductionInfo ? introductionInfo : null));
}
/**
@@ -112,8 +112,8 @@ public class DefaultIntroductionAdvisor implements IntroductionAdvisor, ClassFil
@Override
public void validateInterfaces() throws IllegalArgumentException {
for (Class<?> ifc : this.interfaces) {
if (this.advice instanceof DynamicIntroductionAdvice &&
!((DynamicIntroductionAdvice) this.advice).implementsInterface(ifc)) {
if (this.advice instanceof DynamicIntroductionAdvice dynamicIntroductionAdvice &&
!dynamicIntroductionAdvice.implementsInterface(ifc)) {
throw new IllegalArgumentException("DynamicIntroductionAdvice [" + this.advice + "] " +
"does not implement interface [" + ifc.getName() + "] specified for introduction");
}
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -98,8 +98,8 @@ public class DelegatePerTargetObjectIntroductionInterceptor extends Introduction
// Massage return value if possible: if the delegate returned itself,
// we really want to return the proxy.
if (retVal == delegate && mi instanceof ProxyMethodInvocation) {
retVal = ((ProxyMethodInvocation) mi).getProxy();
if (retVal == delegate && mi instanceof ProxyMethodInvocation pmi) {
retVal = pmi.getProxy();
}
return retVal;
}
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -112,8 +112,8 @@ public class DelegatingIntroductionInterceptor extends IntroductionInfoSupport
// Massage return value if possible: if the delegate returned itself,
// we really want to return the proxy.
if (retVal == this.delegate && mi instanceof ProxyMethodInvocation) {
Object proxy = ((ProxyMethodInvocation) mi).getProxy();
if (retVal == this.delegate && mi instanceof ProxyMethodInvocation pmi) {
Object proxy = pmi.getProxy();
if (mi.getMethod().getReturnType().isInstance(proxy)) {
retVal = proxy;
}
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -100,9 +100,9 @@ public class NameMatchMethodPointcut extends StaticMethodMatcherPointcut impleme
@Override
public boolean equals(@Nullable Object other) {
return (this == other || (other instanceof NameMatchMethodPointcut &&
this.mappedNames.equals(((NameMatchMethodPointcut) other).mappedNames)));
public boolean equals(@Nullable Object obj) {
return (this == obj || (obj instanceof NameMatchMethodPointcut that &&
this.mappedNames.equals(that.mappedNames)));
}
@Override
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -45,9 +45,9 @@ public class RootClassFilter implements ClassFilter, Serializable {
}
@Override
public boolean equals(Object other) {
return (this == other || (other instanceof RootClassFilter &&
this.clazz.equals(((RootClassFilter) other).clazz)));
public boolean equals(Object obj) {
return (this == obj || (obj instanceof RootClassFilter that &&
this.clazz.equals(that.clazz)));
}
@Override
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -77,12 +77,12 @@ public abstract class AbstractPrototypeBasedTargetSource extends AbstractBeanFac
if (logger.isDebugEnabled()) {
logger.debug("Destroying instance of bean '" + getTargetBeanName() + "'");
}
if (getBeanFactory() instanceof ConfigurableBeanFactory) {
((ConfigurableBeanFactory) getBeanFactory()).destroyBean(getTargetBeanName(), target);
if (getBeanFactory() instanceof ConfigurableBeanFactory cbf) {
cbf.destroyBean(getTargetBeanName(), target);
}
else if (target instanceof DisposableBean) {
else if (target instanceof DisposableBean disposableBean) {
try {
((DisposableBean) target).destroy();
disposableBean.destroy();
}
catch (Throwable ex) {
logger.warn("Destroy method on bean with name '" + getTargetBeanName() + "' threw an exception", ex);
@@ -100,9 +100,9 @@ public class HotSwappableTargetSource implements TargetSource, Serializable {
* objects are equal.
*/
@Override
public boolean equals(Object other) {
return (this == other || (other instanceof HotSwappableTargetSource &&
this.target.equals(((HotSwappableTargetSource) other).target)));
public boolean equals(Object obj) {
return (this == obj || (obj instanceof HotSwappableTargetSource that &&
this.target.equals(that.target)));
}
@Override
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -34,10 +34,11 @@ import org.springframework.util.ReflectionUtils;
/**
* Internal class for managing injection metadata.
* Not intended for direct use in applications.
*
* <p>Not intended for direct use in applications.
*
* <p>Used by {@link AutowiredAnnotationBeanPostProcessor},
* {@link org.springframework.context.annotation.CommonAnnotationBeanPostProcessor} and
* {@link org.springframework.context.annotation.CommonAnnotationBeanPostProcessor}, and
* {@link org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor}.
*
* @author Juergen Hoeller
@@ -107,15 +108,20 @@ public class InjectionMetadata {
}
public void checkConfigMembers(RootBeanDefinition beanDefinition) {
Set<InjectedElement> checkedElements = new LinkedHashSet<>(this.injectedElements.size());
for (InjectedElement element : this.injectedElements) {
Member member = element.getMember();
if (!beanDefinition.isExternallyManagedConfigMember(member)) {
beanDefinition.registerExternallyManagedConfigMember(member);
checkedElements.add(element);
}
if (this.injectedElements.isEmpty()) {
this.checkedElements = Collections.emptySet();
}
else {
Set<InjectedElement> checkedElements = new LinkedHashSet<>((this.injectedElements.size() * 4 / 3) + 1);
for (InjectedElement element : this.injectedElements) {
Member member = element.getMember();
if (!beanDefinition.isExternallyManagedConfigMember(member)) {
beanDefinition.registerExternallyManagedConfigMember(member);
checkedElements.add(element);
}
}
this.checkedElements = checkedElements;
}
this.checkedElements = checkedElements;
}
public void inject(Object target, @Nullable String beanName, @Nullable PropertyValues pvs) throws Throwable {
@@ -110,27 +110,55 @@ public class ResourceEntityResolver extends DelegatingEntityResolver {
}
}
else if (systemId.endsWith(DTD_SUFFIX) || systemId.endsWith(XSD_SUFFIX)) {
// External dtd/xsd lookup via https even for canonical http declaration
String url = systemId;
if (url.startsWith("http:")) {
url = "https:" + url.substring(5);
}
try {
source = new InputSource(ResourceUtils.toURL(url).openStream());
source.setPublicId(publicId);
source.setSystemId(systemId);
}
catch (IOException ex) {
if (logger.isDebugEnabled()) {
logger.debug("Could not resolve XML entity [" + systemId + "] through URL [" + url + "]", ex);
}
// Fall back to the parser's default behavior.
source = null;
}
source = resolveSchemaEntity(publicId, systemId);
}
}
return source;
}
/**
* A fallback method for {@link #resolveEntity(String, String)} that is used when a
* "schema" entity (DTD or XSD) cannot be resolved as a local resource. The default
* behavior is to perform remote resolution over HTTPS.
* <p>Subclasses can override this method to change the default behavior.
* <ul>
* <li>Return {@code null} to fall back to the parser's
* {@linkplain org.xml.sax.EntityResolver#resolveEntity(String, String) default behavior}.</li>
* <li>Throw an exception to prevent remote resolution of the DTD or XSD.</li>
* </ul>
* @param publicId the public identifier of the external entity being referenced,
* or null if none was supplied
* @param systemId the system identifier of the external entity being referenced,
* representing the URL of the DTD or XSD
* @return an InputSource object describing the new input source, or null to request
* that the parser open a regular URI connection to the system identifier
* @since 6.0.4
*/
@Nullable
protected InputSource resolveSchemaEntity(@Nullable String publicId, String systemId) {
InputSource source;
// External dtd/xsd lookup via https even for canonical http declaration
String url = systemId;
if (url.startsWith("http:")) {
url = "https:" + url.substring(5);
}
if (logger.isWarnEnabled()) {
logger.warn("DTD/XSD XML entity [" + systemId + "] not found, falling back to remote https resolution");
}
try {
source = new InputSource(ResourceUtils.toURL(url).openStream());
source.setPublicId(publicId);
source.setSystemId(systemId);
}
catch (IOException ex) {
if (logger.isDebugEnabled()) {
logger.debug("Could not resolve XML entity [" + systemId + "] through URL [" + url + "]", ex);
}
// Fall back to the parser's default behavior.
source = null;
}
return source;
}
}
@@ -0,0 +1,129 @@
/*
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.beans.factory.xml;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.Mockito;
import org.xml.sax.InputSource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.lang.Nullable;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
/**
* Unit tests for ResourceEntityResolver.
*
* @author Simon Baslé
* @author Sam Brannen
* @since 6.0.4
*/
class ResourceEntityResolverTests {
@ParameterizedTest
@ValueSource(strings = { "https://example.org/schema/", "https://example.org/schema.xml" })
void resolveEntityDoesNotCallFallbackIfNotSchema(String systemId) throws Exception {
ConfigurableFallbackEntityResolver resolver = new ConfigurableFallbackEntityResolver(true);
assertThat(resolver.resolveEntity("testPublicId", systemId)).isNull();
assertThat(resolver.fallbackInvoked).isFalse();
}
@ParameterizedTest
@ValueSource(strings = { "https://example.org/schema.dtd", "https://example.org/schema.xsd" })
void resolveEntityCallsFallbackThatReturnsNull(String systemId) throws Exception {
ConfigurableFallbackEntityResolver resolver = new ConfigurableFallbackEntityResolver(null);
assertThat(resolver.resolveEntity("testPublicId", systemId)).isNull();
assertThat(resolver.fallbackInvoked).isTrue();
}
@ParameterizedTest
@ValueSource(strings = { "https://example.org/schema.dtd", "https://example.org/schema.xsd" })
void resolveEntityCallsFallbackThatThrowsException(String systemId) {
ConfigurableFallbackEntityResolver resolver = new ConfigurableFallbackEntityResolver(true);
assertThatExceptionOfType(ResolutionRejectedException.class)
.isThrownBy(() -> resolver.resolveEntity("testPublicId", systemId));
assertThat(resolver.fallbackInvoked).isTrue();
}
@ParameterizedTest
@ValueSource(strings = { "https://example.org/schema.dtd", "https://example.org/schema.xsd" })
void resolveEntityCallsFallbackThatReturnsInputSource(String systemId) throws Exception {
InputSource expected = Mockito.mock(InputSource.class);
ConfigurableFallbackEntityResolver resolver = new ConfigurableFallbackEntityResolver(expected);
assertThat(resolver.resolveEntity("testPublicId", systemId)).isSameAs(expected);
assertThat(resolver.fallbackInvoked).isTrue();
}
private static final class NoOpResourceLoader implements ResourceLoader {
@Override
public Resource getResource(String location) {
return null;
}
@Override
public ClassLoader getClassLoader() {
return ResourceEntityResolverTests.class.getClassLoader();
}
}
private static class ConfigurableFallbackEntityResolver extends ResourceEntityResolver {
private final boolean shouldThrow;
@Nullable
private final InputSource returnValue;
boolean fallbackInvoked = false;
private ConfigurableFallbackEntityResolver(boolean shouldThrow) {
super(new NoOpResourceLoader());
this.shouldThrow = shouldThrow;
this.returnValue = null;
}
private ConfigurableFallbackEntityResolver(@Nullable InputSource returnValue) {
super(new NoOpResourceLoader());
this.shouldThrow = false;
this.returnValue = returnValue;
}
@Override
@Nullable
protected InputSource resolveSchemaEntity(String publicId, String systemId) {
this.fallbackInvoked = true;
if (this.shouldThrow) {
throw new ResolutionRejectedException();
}
return this.returnValue;
}
}
@SuppressWarnings("serial")
static class ResolutionRejectedException extends RuntimeException {}
}
@@ -0,0 +1,72 @@
/*
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.context.aot;
import org.springframework.aot.generate.GenerationContext;
import org.springframework.aot.hint.MemberCategory;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.beans.factory.aot.BeanRegistrationAotContribution;
import org.springframework.beans.factory.aot.BeanRegistrationAotProcessor;
import org.springframework.beans.factory.aot.BeanRegistrationCode;
import org.springframework.beans.factory.support.RegisteredBean;
import org.springframework.core.KotlinDetector;
import org.springframework.lang.Nullable;
/**
* AOT {@code BeanRegistrationAotProcessor} that adds additional hints
* required by Kotlin reflection.
*
* @author Sebastien Deleuze
* @since 6.0.4
*/
class KotlinReflectionBeanRegistrationAotProcessor implements BeanRegistrationAotProcessor {
@Nullable
@Override
public BeanRegistrationAotContribution processAheadOfTime(RegisteredBean registeredBean) {
Class<?> beanClass = registeredBean.getBeanClass();
if (KotlinDetector.isKotlinType(beanClass)) {
return new KotlinReflectionBeanRegistrationAotContribution(beanClass);
}
return null;
}
private static class KotlinReflectionBeanRegistrationAotContribution implements BeanRegistrationAotContribution {
private final Class<?> beanClass;
public KotlinReflectionBeanRegistrationAotContribution(Class<?> beanClass) {
this.beanClass = beanClass;
}
@Override
public void applyTo(GenerationContext generationContext, BeanRegistrationCode beanRegistrationCode) {
registerHints(this.beanClass, generationContext.getRuntimeHints());
}
private void registerHints(Class<?> type, RuntimeHints runtimeHints) {
if (KotlinDetector.isKotlinType(type)) {
runtimeHints.reflection().registerType(type, MemberCategory.INTROSPECT_DECLARED_METHODS);
}
Class<?> superClass = type.getSuperclass();
if (superClass != null) {
registerHints(superClass, runtimeHints);
}
}
}
}
@@ -34,6 +34,7 @@ public interface Trigger {
/**
* Determine the next execution time according to the given trigger context.
* <p>The default implementation delegates to {@link #nextExecution(TriggerContext)}.
* @param triggerContext context object encapsulating last execution times
* and last completion time
* @return the next execution time as defined by the trigger,
@@ -33,9 +33,9 @@ public interface TriggerContext {
/**
* Return the clock to use for trigger calculation.
* <p>Defaults to {@link Clock#systemDefaultZone()}.
* @since 5.3
* @see TaskScheduler#getClock()
* @see Clock#systemDefaultZone()
*/
default Clock getClock() {
return Clock.systemDefaultZone();
@@ -44,6 +44,7 @@ public interface TriggerContext {
/**
* Return the last <i>scheduled</i> execution time of the task,
* or {@code null} if not scheduled before.
* <p>The default implementation delegates to {@link #lastScheduledExecution()}.
* @deprecated as of 6.0, in favor on {@link #lastScheduledExecution()}
*/
@Nullable
@@ -64,10 +65,11 @@ public interface TriggerContext {
/**
* Return the last <i>actual</i> execution time of the task,
* or {@code null} if not scheduled before.
* <p>The default implementation delegates to {@link #lastActualExecution()}.
* @deprecated as of 6.0, in favor on {@link #lastActualExecution()}
*/
@Deprecated(since = "6.0")
@Nullable
@Deprecated(since = "6.0")
default Date lastActualExecutionTime() {
Instant instant = lastActualExecution();
return instant != null ? Date.from(instant) : null;
@@ -83,6 +85,7 @@ public interface TriggerContext {
/**
* Return the last completion time of the task,
* or {@code null} if not scheduled before.
* <p>The default implementation delegates to {@link #lastCompletion()}.
* @deprecated as of 6.0, in favor on {@link #lastCompletion()}
*/
@Deprecated(since = "6.0")
@@ -1,3 +1,5 @@
org.springframework.beans.factory.aot.BeanFactoryInitializationAotProcessor= \
org.springframework.context.aot.ReflectiveProcessorBeanFactoryInitializationAotProcessor
org.springframework.beans.factory.aot.BeanRegistrationAotProcessor=\
org.springframework.context.aot.KotlinReflectionBeanRegistrationAotProcessor
@@ -0,0 +1,23 @@
/*
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.context.aot;
class SampleJavaBean {
void sample() {
}
}
@@ -0,0 +1,91 @@
/*
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.context.aot
import org.assertj.core.api.Assertions
import org.junit.jupiter.api.Test
import org.mockito.Mockito
import org.springframework.aot.generate.GenerationContext
import org.springframework.aot.hint.MemberCategory
import org.springframework.aot.hint.predicate.RuntimeHintsPredicates
import org.springframework.aot.test.generate.TestGenerationContext
import org.springframework.beans.factory.aot.*
import org.springframework.beans.factory.support.DefaultListableBeanFactory
import org.springframework.beans.factory.support.RegisteredBean
import org.springframework.beans.factory.support.RootBeanDefinition
/**
* Tests for [KotlinReflectionBeanRegistrationAotProcessor].
*
* @author Sebastien Deleuze
*/
class KotlinReflectionBeanRegistrationAotProcessorTests {
private val processor = KotlinReflectionBeanRegistrationAotProcessor()
private val generationContext = TestGenerationContext()
@Test
fun processorIsRegistered() {
Assertions.assertThat(
AotServices.factories(javaClass.classLoader).load(BeanRegistrationAotProcessor::class.java))
.anyMatch(KotlinReflectionBeanRegistrationAotProcessor::class.java::isInstance)
}
@Test
fun shouldProcessKotlinBean() {
process(SampleKotlinBean::class.java)
Assertions.assertThat(
RuntimeHintsPredicates.reflection()
.onType(SampleKotlinBean::class.java)
.withMemberCategory(MemberCategory.INTROSPECT_DECLARED_METHODS)
).accepts(generationContext.runtimeHints)
Assertions.assertThat(
RuntimeHintsPredicates.reflection()
.onType(BaseKotlinBean::class.java)
.withMemberCategory(MemberCategory.INTROSPECT_DECLARED_METHODS)
).accepts(generationContext.runtimeHints)
}
@Test
fun shouldNotProcessJavaBean() {
process(SampleJavaBean::class.java)
Assertions.assertThat(generationContext.runtimeHints.reflection().typeHints()).isEmpty()
}
private fun process(beanClass: Class<*>) {
createContribution(beanClass)?.applyTo(generationContext, Mockito.mock(BeanRegistrationCode::class.java))
}
private fun createContribution(beanClass: Class<*>): BeanRegistrationAotContribution? {
val beanFactory = DefaultListableBeanFactory()
beanFactory.registerBeanDefinition(beanClass.name, RootBeanDefinition(beanClass))
return processor.processAheadOfTime(RegisteredBean.of(beanFactory, beanClass.name))
}
class SampleKotlinBean : BaseKotlinBean() {
fun sample() {
}
}
open class BaseKotlinBean {
fun base() {
}
}
}
@@ -45,7 +45,7 @@ import org.springframework.util.MimeTypeUtils;
* Decode from a data buffer stream to a {@code String} stream, either splitting
* or aggregating incoming data chunks to realign along newlines delimiters
* and produce a stream of strings. This is useful for streaming but is also
* necessary to ensure that that multibyte characters can be decoded correctly,
* necessary to ensure that multi-byte characters can be decoded correctly,
* avoiding split-character issues. The default delimiters used by default are
* {@code \n} and {@code \r\n} but that can be customized.
*
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -37,7 +37,7 @@ import java.util.Map;
* <pre class="code">
* ConfigurableEnvironment environment = new StandardEnvironment();
* MutablePropertySources propertySources = environment.getPropertySources();
* Map&lt;String, String&gt; myMap = new HashMap&lt;&gt;();
* Map&lt;String, Object&gt; myMap = new HashMap&lt;&gt;();
* myMap.put("xyz", "myValue");
* propertySources.addFirst(new MapPropertySource("MY_MAP", myMap));
* </pre>
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -134,9 +134,9 @@ public abstract class PropertySource<T> {
* <p>No properties other than {@code name} are evaluated.
*/
@Override
public boolean equals(@Nullable Object other) {
return (this == other || (other instanceof PropertySource &&
ObjectUtils.nullSafeEquals(getName(), ((PropertySource<?>) other).getName())));
public boolean equals(@Nullable Object obj) {
return (this == obj || (obj instanceof PropertySource<?> other &&
ObjectUtils.nullSafeEquals(getName(), other.getName())));
}
/**
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@ package org.springframework.core.type.classreading;
import java.lang.annotation.Annotation;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@@ -92,8 +93,10 @@ class MergedAnnotationReadingVisitor<A extends Annotation> extends AnnotationVis
@Override
public void visitEnd() {
Map<String, Object> compactedAttributes =
(this.attributes.size() == 0 ? Collections.emptyMap() : this.attributes);
MergedAnnotation<A> annotation = MergedAnnotation.of(
this.classLoader, this.source, this.annotationType, this.attributes);
this.classLoader, this.source, this.annotationType, compactedAttributes);
this.consumer.accept(annotation);
}
@@ -89,7 +89,7 @@ public class SQLExceptionSubclassTranslator extends AbstractFallbackSQLException
return new DataIntegrityViolationException(buildMessage(task, sql, ex), ex);
}
if (ex instanceof SQLIntegrityConstraintViolationException) {
if ("23505".equals(ex.getSQLState())) {
if (SQLStateSQLExceptionTranslator.indicatesDuplicateKey(ex.getSQLState(), ex.getErrorCode())) {
return new DuplicateKeyException(buildMessage(task, sql, ex), ex);
}
return new DataIntegrityViolationException(buildMessage(task, sql, ex), ex);
@@ -99,7 +99,7 @@ public class SQLStateSQLExceptionTranslator extends AbstractFallbackSQLException
return new BadSqlGrammarException(task, (sql != null ? sql : ""), ex);
}
else if (DATA_INTEGRITY_VIOLATION_CODES.contains(classCode)) {
if ("23505".equals(sqlState)) {
if (indicatesDuplicateKey(sqlState, ex.getErrorCode())) {
return new DuplicateKeyException(buildMessage(task, sql, ex), ex);
}
return new DataIntegrityViolationException(buildMessage(task, sql, ex), ex);
@@ -148,4 +148,19 @@ public class SQLStateSQLExceptionTranslator extends AbstractFallbackSQLException
return sqlState;
}
/**
* Check whether the given SQL state (and the associated error code in case
* of a generic SQL state value) indicate a duplicate key exception:
* either SQL state 23505 as a specific indication, or the generic SQL state
* 23000 with well-known vendor codes (1 for Oracle, 1062 for MySQL/MariaDB,
* 2627 for MS SQL Server).
* @param sqlState the SQL state value
* @param errorCode the error code value
*/
static boolean indicatesDuplicateKey(@Nullable String sqlState, int errorCode) {
return ("23505".equals(sqlState) ||
("23000".equals(sqlState) &&
(errorCode == 1 || errorCode == 1062 || errorCode == 2627)));
}
}
@@ -51,7 +51,7 @@ import static org.mockito.Mockito.verify;
*/
public class SQLErrorCodeSQLExceptionTranslatorTests {
private static SQLErrorCodes ERROR_CODES = new SQLErrorCodes();
private static final SQLErrorCodes ERROR_CODES = new SQLErrorCodes();
static {
ERROR_CODES.setBadSqlGrammarCodes("1", "2");
ERROR_CODES.setInvalidResultSetAccessCodes("3", "4");
@@ -64,6 +64,7 @@ public class SQLErrorCodeSQLExceptionTranslatorTests {
}
@SuppressWarnings("deprecation")
@Test
public void errorCodeTranslation() {
SQLExceptionTranslator sext = new SQLErrorCodeSQLExceptionTranslator(ERROR_CODES);
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -57,6 +57,9 @@ public class SQLExceptionSubclassTranslatorTests {
doTest(new SQLFeatureNotSupportedException("", "", 0), InvalidDataAccessApiUsageException.class);
doTest(new SQLIntegrityConstraintViolationException("", "", 0), DataIntegrityViolationException.class);
doTest(new SQLIntegrityConstraintViolationException("", "23505", 0), DuplicateKeyException.class);
doTest(new SQLIntegrityConstraintViolationException("", "23000", 1), DuplicateKeyException.class);
doTest(new SQLIntegrityConstraintViolationException("", "23000", 1062), DuplicateKeyException.class);
doTest(new SQLIntegrityConstraintViolationException("", "23000", 2627), DuplicateKeyException.class);
doTest(new SQLInvalidAuthorizationSpecException("", "", 0), PermissionDeniedDataAccessException.class);
doTest(new SQLNonTransientConnectionException("", "", 0), DataAccessResourceFailureException.class);
doTest(new SQLRecoverableException("", "", 0), RecoverableDataAccessException.class);
@@ -61,6 +61,21 @@ public class SQLStateSQLExceptionTranslatorTests {
doTest("23505", DuplicateKeyException.class);
}
@Test
public void translateDuplicateKeyOracle() {
doTest("23000", 1, DuplicateKeyException.class);
}
@Test
public void translateDuplicateKeyMySQL() {
doTest("23000", 1062, DuplicateKeyException.class);
}
@Test
public void translateDuplicateKeyMSSQL() {
doTest("23000", 2627, DuplicateKeyException.class);
}
@Test
public void translateDataAccessResourceFailure() {
doTest("53", DataAccessResourceFailureException.class);
@@ -105,8 +120,12 @@ public class SQLStateSQLExceptionTranslatorTests {
private void doTest(@Nullable String sqlState, @Nullable Class<?> dataAccessExceptionType) {
doTest(sqlState, 0, dataAccessExceptionType);
}
private void doTest(@Nullable String sqlState, int errorCode, @Nullable Class<?> dataAccessExceptionType) {
SQLExceptionTranslator translator = new SQLStateSQLExceptionTranslator();
SQLException ex = new SQLException("reason", sqlState);
SQLException ex = new SQLException("reason", sqlState, errorCode);
DataAccessException dax = translator.translate("task", "SQL", ex);
if (dataAccessExceptionType == null) {
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -192,7 +192,13 @@ public abstract class JmsAccessor implements InitializingBean {
* @see jakarta.jms.ConnectionFactory#createConnection()
*/
protected Connection createConnection() throws JMSException {
return obtainConnectionFactory().createConnection();
ConnectionFactory cf = obtainConnectionFactory();
Connection con = cf.createConnection();
if (con == null) {
throw new jakarta.jms.IllegalStateException(
"ConnectionFactory returned null from createConnection(): " + cf);
}
return con;
}
/**
@@ -130,7 +130,7 @@ public abstract class AbstractBrokerMessageHandler
}
/**
* Return destination prefixes prefixes to use to filter messages to forward
* Return destination prefixes to use to filter messages to forward
* to the broker. Messages that have a destination and where the destination
* doesn't match are ignored.
* <p>By default this is not set.
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -23,6 +23,7 @@ import java.util.List;
import javax.lang.model.element.Modifier;
import jakarta.persistence.Convert;
import jakarta.persistence.Converter;
import jakarta.persistence.EntityListeners;
import jakarta.persistence.IdClass;
@@ -156,9 +157,20 @@ class PersistenceManagedTypesBeanRegistrationAotProcessor implements BeanRegistr
private void contributeConverterHints(RuntimeHints hints, Class<?> managedClass) {
Converter converter = AnnotationUtils.findAnnotation(managedClass, Converter.class);
ReflectionHints reflectionHints = hints.reflection();
if (converter != null) {
hints.reflection().registerType(managedClass, MemberCategory.INVOKE_DECLARED_CONSTRUCTORS, MemberCategory.INVOKE_PUBLIC_METHODS);
reflectionHints.registerType(managedClass, MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
}
Convert convertClassAnnotation = AnnotationUtils.findAnnotation(managedClass, Convert.class);
if (convertClassAnnotation != null) {
reflectionHints.registerType(convertClassAnnotation.converter(), MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
}
ReflectionUtils.doWithFields(managedClass, field -> {
Convert convertFieldAnnotation = AnnotationUtils.findAnnotation(field, Convert.class);
if (convertFieldAnnotation != null && convertFieldAnnotation.converter() != void.class) {
reflectionHints.registerType(convertFieldAnnotation.converter(), MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
}
});
}
private void contributeCallbackHints(RuntimeHints hints, Class<?> managedClass) {
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
package org.springframework.orm.jpa.persistenceunit;
import jakarta.persistence.spi.ClassTransformer;
import org.apache.commons.logging.LogFactory;
import org.springframework.core.DecoratingClassLoader;
import org.springframework.instrument.classloading.LoadTimeWeaver;
@@ -79,10 +80,12 @@ class SpringPersistenceUnitInfo extends MutablePersistenceUnitInfo {
*/
@Override
public void addTransformer(ClassTransformer classTransformer) {
if (this.loadTimeWeaver == null) {
throw new IllegalStateException("Cannot apply class transformer without LoadTimeWeaver specified");
if (this.loadTimeWeaver != null) {
this.loadTimeWeaver.addTransformer(new ClassFileTransformerAdapter(classTransformer));
}
else {
LogFactory.getLog(getClass()).info("No LoadTimeWeaver setup: ignoring JPA class transformer");
}
this.loadTimeWeaver.addTransformer(new ClassFileTransformerAdapter(classTransformer));
}
/**
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -17,6 +17,7 @@
package org.springframework.orm.jpa.domain;
import jakarta.persistence.Column;
import jakarta.persistence.Convert;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.IdClass;
@@ -24,6 +25,7 @@ import jakarta.persistence.PreRemove;
@Entity
@IdClass(EmployeeId.class)
@Convert(converter = EmployeeKindConverter.class, attributeName = "kind")
public class Employee {
@Id
@@ -36,6 +38,10 @@ public class Employee {
private EmployeeLocation location;
@Convert(converter = EmployeeCategoryConverter.class)
private EmployeeCategory category;
private EmployeeKind kind;
public String getName() {
return name;
@@ -61,6 +67,22 @@ public class Employee {
this.location = location;
}
public EmployeeCategory getCategory() {
return category;
}
public void setCategory(EmployeeCategory category) {
this.category = category;
}
public EmployeeKind getKind() {
return kind;
}
public void setKind(EmployeeKind kind) {
this.kind = kind;
}
@PreRemove
public void preRemove() {
}
@@ -0,0 +1,29 @@
/*
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.orm.jpa.domain;
public class EmployeeCategory {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@@ -0,0 +1,40 @@
/*
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.orm.jpa.domain;
import jakarta.persistence.AttributeConverter;
public class EmployeeCategoryConverter implements AttributeConverter<EmployeeCategory, String> {
@Override
public String convertToDatabaseColumn(EmployeeCategory employeeCategory) {
if (employeeCategory != null) {
return employeeCategory.getName();
}
return null;
}
@Override
public EmployeeCategory convertToEntityAttribute(String data) {
if (data != null) {
EmployeeCategory employeeCategory = new EmployeeCategory();
employeeCategory.setName(data);
return employeeCategory;
}
return null;
}
}
@@ -0,0 +1,29 @@
/*
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.orm.jpa.domain;
public class EmployeeKind {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@@ -0,0 +1,40 @@
/*
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.orm.jpa.domain;
import jakarta.persistence.AttributeConverter;
public class EmployeeKindConverter implements AttributeConverter<EmployeeKind, String> {
@Override
public String convertToDatabaseColumn(EmployeeKind employeeKind) {
if (employeeKind != null) {
return employeeKind.getName();
}
return null;
}
@Override
public EmployeeKind convertToEntityAttribute(String data) {
if (data != null) {
EmployeeKind employeeKind = new EmployeeKind();
employeeKind.setName(data);
return employeeKind;
}
return null;
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -40,7 +40,9 @@ import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.domain.DriversLicense;
import org.springframework.orm.jpa.domain.Employee;
import org.springframework.orm.jpa.domain.EmployeeCategoryConverter;
import org.springframework.orm.jpa.domain.EmployeeId;
import org.springframework.orm.jpa.domain.EmployeeKindConverter;
import org.springframework.orm.jpa.domain.EmployeeLocation;
import org.springframework.orm.jpa.domain.EmployeeLocationConverter;
import org.springframework.orm.jpa.domain.Person;
@@ -96,8 +98,11 @@ class PersistenceManagedTypesBeanRegistrationAotProcessorTests {
assertThat(RuntimeHintsPredicates.reflection().onType(EmployeeId.class)
.withMemberCategories(MemberCategory.DECLARED_FIELDS)).accepts(hints);
assertThat(RuntimeHintsPredicates.reflection().onType(EmployeeLocationConverter.class)
.withMemberCategories(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS, MemberCategory.INVOKE_PUBLIC_METHODS))
.accepts(hints);
.withMemberCategories(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS)).accepts(hints);
assertThat(RuntimeHintsPredicates.reflection().onType(EmployeeCategoryConverter.class)
.withMemberCategories(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS)).accepts(hints);
assertThat(RuntimeHintsPredicates.reflection().onType(EmployeeKindConverter.class)
.withMemberCategories(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS)).accepts(hints);
assertThat(RuntimeHintsPredicates.reflection().onType(EmployeeLocation.class)
.withMemberCategories(MemberCategory.DECLARED_FIELDS)).accepts(hints);
});
@@ -231,7 +231,7 @@ public abstract class ConnectionFactoryUtils {
return new DataAccessResourceFailureException(buildMessage(task, sql, ex), ex);
}
if (ex instanceof R2dbcDataIntegrityViolationException) {
if ("23505".equals(ex.getSqlState())) {
if (indicatesDuplicateKey(ex.getSqlState(), ex.getErrorCode())) {
return new DuplicateKeyException(buildMessage(task, sql, ex), ex);
}
return new DataIntegrityViolationException(buildMessage(task, sql, ex), ex);
@@ -246,6 +246,19 @@ public abstract class ConnectionFactoryUtils {
return new UncategorizedR2dbcException(buildMessage(task, sql, ex), sql, ex);
}
/**
* Check whether the given SQL state (and the associated error code in case
* of a generic SQL state value) indicate a duplicate key exception. See
* {@code org.springframework.jdbc.support.SQLStateSQLExceptionTranslator#indicatesDuplicateKey}.
* @param sqlState the SQL state value
* @param errorCode the error code value
*/
static boolean indicatesDuplicateKey(@Nullable String sqlState, int errorCode) {
return ("23505".equals(sqlState) ||
("23000".equals(sqlState) &&
(errorCode == 1 || errorCode == 1062 || errorCode == 2627)));
}
/**
* Build a message {@code String} for the given {@link R2dbcException}.
* <p>To be called by translator subclasses when creating an instance of a generic
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -74,7 +74,7 @@ class NamedParameterExpander {
* placeholders to be used for a select list. Select lists should be limited
* to 100 or fewer elements. A larger number of elements is not guaranteed to be
* supported by the database and is strictly vendor-dependent.
* @param sql sql the original SQL statement
* @param sql the original SQL statement
* @param bindMarkersFactory the bind marker factory
* @param paramSource the source for named parameters
* @return the expanded sql that accepts bind parameters and allows for execution
@@ -95,6 +95,18 @@ public class ConnectionFactoryUtilsUnitTests {
exception = ConnectionFactoryUtils.convertR2dbcException("", "",
new R2dbcDataIntegrityViolationException("reason", "23505"));
assertThat(exception).isExactlyInstanceOf(DuplicateKeyException.class);
exception = ConnectionFactoryUtils.convertR2dbcException("", "",
new R2dbcDataIntegrityViolationException("reason", "23000", 1));
assertThat(exception).isExactlyInstanceOf(DuplicateKeyException.class);
exception = ConnectionFactoryUtils.convertR2dbcException("", "",
new R2dbcDataIntegrityViolationException("reason", "23000", 1062));
assertThat(exception).isExactlyInstanceOf(DuplicateKeyException.class);
exception = ConnectionFactoryUtils.convertR2dbcException("", "",
new R2dbcDataIntegrityViolationException("reason", "23000", 2627));
assertThat(exception).isExactlyInstanceOf(DuplicateKeyException.class);
}
@Test

Some files were not shown because too many files have changed in this diff Show More