[[mvc-ann-typeconversion]]
= Type Conversion

[.small]#xref:web/webflux/controller/ann-methods/typeconversion.adoc[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`)
can require type conversion if the argument is declared as something other than `String`.

For such cases, type conversion is automatically applied based on the configured converters.
By default, simple types (`int`, `long`, `Date`, and others) are supported. You can customize
type conversion through a `WebDataBinder` (see xref:web/webmvc/mvc-controller/ann-initbinder.adoc[`DataBinder`]) or by registering
`Formatters` with the `FormattingConversionService`.
See xref:core/validation/format.adoc[Spring Field Formatting].

A practical issue in type conversion is the treatment of an empty String source value.
Such a value is treated as missing if it becomes `null` as a result of type conversion.
This can be the case for `Long`, `UUID`, and other target types. If you want to allow `null`
to be injected, either use the `required` flag on the argument annotation, or declare the
argument as `@Nullable`.

[NOTE]
====
As of 5.3, non-null arguments will be enforced even after type conversion. If your handler
method intends to accept a null value as well, either declare your argument as `@Nullable`
or mark it as `required=false` in the corresponding `@RequestParam`, etc. annotation. This is
a best practice and the recommended solution for regressions encountered in a 5.3 upgrade.

Alternatively, you may specifically handle e.g. the resulting `MissingPathVariableException`
in the case of a required `@PathVariable`. A null value after conversion will be treated like
an empty original value, so the corresponding `Missing...Exception` variants will be thrown.
====


