@@ -732,7 +732,7 @@ public class OwnerController {
732732URI variables are automatically converted to the appropriate type or`TypeMismatchException`
733733is raised. Simple types -- `int`, `long`, `Date`, are supported by default and you can
734734register support for any other data type.
735- // TODO: see <<webflux-ann-typeconversion>> and <<webflux-ann-webdatabinder >>.
735+ See <<webflux-ann-typeconversion>> and <<webflux-ann-initbinder >>.
736736
737737URI variables can be named explicitly -- e.g. `@PathVariable("customId")`, but you can
738738leave that detail out if the names are the same and your code is compiled with debugging
@@ -962,13 +962,15 @@ Java 8+: `java.time.ZoneId`
962962
963963|`@RequestParam`
964964|For access to Servlet request parameters. Parameter values are converted to the declared
965- method argument type.
966- // TODO: See <<webflux-ann-requestparam>>.
965+ method argument type. See <<webflux-ann-requestparam>>.
967966
968967|`@RequestHeader`
969968|For access to request headers. Header values are converted to the declared method argument
970- type.
971- // TODO: See <<webflux-ann-requestheader>>.
969+ type. See <<webflux-ann-requestheader>>.
970+
971+ |`@CookieValue`
972+ |For access to cookies. Cookies values are converted to the declared method argument
973+ type. See <<webflux-ann-cookievalue>>.
972974
973975|`@RequestBody`
974976|For access to the HTTP request body. Body content is converted to the declared method
@@ -1087,6 +1089,200 @@ class name of the return type.
10871089|===
10881090
10891091
1092+ [[webflux-ann-typeconversion]]
1093+ ==== Type Conversion
1094+ [.small]#<<web.adoc#mvc-ann-typeconversion,Same in Spring MVC>>#
1095+
1096+ Some annotated controller method arguments that represent String-based request input -- e.g.
1097+ `@RequestParam`, `@RequestHeader`, `@PathVariable`, `@MatrixVariable`, and `@CookieValue`,
1098+ may require type conversion if the argument is declared as something other than `String`.
1099+
1100+ For such cases type conversion is automatically applied based on the configured converters.
1101+ By default simple types such as `int`, `long`, `Date`, etc. are supported. Type conversion
1102+ can be customized through a `WebDataBinder`, see <<mvc-ann-initbinder>>, or by registering
1103+ `Formatters` with the `FormattingConversionService`, see
1104+ <<core.adoc#format, Spring Field Formatting>>.
1105+
1106+
1107+ [[webflux-ann-requestparam]]
1108+ ==== @RequestParam
1109+ [.small]#<<web.adoc#mvc-ann-requestparam,Same in Spring MVC>>#
1110+
1111+ Use the `@RequestParam` annotation to bind Servlet request parameters (i.e. query
1112+ parameters or form data) to a method argument in a controller.
1113+
1114+ The following code snippet shows the usage:
1115+
1116+ [source,java,indent=0]
1117+ [subs="verbatim,quotes"]
1118+ ----
1119+ @Controller
1120+ @RequestMapping("/pets")
1121+ public class EditPetForm {
1122+
1123+ // ...
1124+
1125+ @GetMapping
1126+ public String setupForm(**@RequestParam("petId") int petId**, Model model) {
1127+ Pet pet = this.clinic.loadPet(petId);
1128+ model.addAttribute("pet", pet);
1129+ return "petForm";
1130+ }
1131+
1132+ // ...
1133+
1134+ }
1135+ ----
1136+
1137+ Parameters using this annotation are required by default, but you can specify that a
1138+ parameter is optional by setting ``@RequestParam``'s `required` flag to `false` or
1139+ declare the argument with a `java.util.Optional` wrapper.
1140+
1141+ Type conversion is applied automatically if the target method parameter type is not
1142+ `String`. See <<mvc-ann-typeconversion>>.
1143+
1144+ When an `@RequestParam` annotation is declared as `Map<String, String>` or
1145+ `MultiValueMap<String, String>` argument, the map is populated with all request
1146+ parameters.
1147+
1148+
1149+ [[webflux-ann-requestheader]]
1150+ ==== @RequestHeader
1151+ [.small]#<<web.adoc#mvc-ann-requestheader,Same in Spring MVC>>#
1152+
1153+ Use the `@RequestHeader` annotation to bind a request header to a method argument in a
1154+ controller.
1155+
1156+ Given request with headers:
1157+
1158+ [literal]
1159+ [subs="verbatim,quotes"]
1160+ ----
1161+ Host localhost:8080
1162+ Accept text/html,application/xhtml+xml,application/xml;q=0.9
1163+ Accept-Language fr,en-gb;q=0.7,en;q=0.3
1164+ Accept-Encoding gzip,deflate
1165+ Accept-Charset ISO-8859-1,utf-8;q=0.7,*;q=0.7
1166+ Keep-Alive 300
1167+ ----
1168+
1169+ The following gets the value of the `Accept-Encoding` and `Keep-Alive` headers:
1170+
1171+ [source,java,indent=0]
1172+ [subs="verbatim,quotes"]
1173+ ----
1174+ @GetMapping("/demo")
1175+ public void handle(
1176+ **@RequestHeader("Accept-Encoding")** String encoding,
1177+ **@RequestHeader("Keep-Alive")** long keepAlive) {
1178+ //...
1179+ }
1180+ ----
1181+
1182+ Type conversion is applied automatically if the target method parameter type is not
1183+ `String`. See <<mvc-ann-typeconversion>>.
1184+
1185+ When an `@RequestHeader` annotation is used on a `Map<String, String>`,
1186+ `MultiValueMap<String, String>`, or `HttpHeaders` argument, the map is populated
1187+ with all header values.
1188+
1189+ [TIP]
1190+ ====
1191+ Built-in support is available for converting a comma-separated string into an
1192+ array/collection of strings or other types known to the type conversion system. For
1193+ example a method parameter annotated with `@RequestHeader("Accept")` may be of type
1194+ `String` but also `String[]` or `List<String>`.
1195+ ====
1196+
1197+
1198+ [[webflux-ann-cookievalue]]
1199+ ==== @CookieValue
1200+ [.small]#<<web.adoc#mvc-ann-cookievalue,Same in Spring MVC>>#
1201+
1202+ Use the `@CookieValue` annotation to bind the value of an HTTP cookie to a method argument
1203+ in a controller.
1204+
1205+ Given request with the following cookie:
1206+
1207+ [literal]
1208+ [subs="verbatim,quotes"]
1209+ ----
1210+ JSESSIONID=415A4AC178C59DACE0B2C9CA727CDD84
1211+ ----
1212+
1213+ The following code sample demonstrates how to get the cookie value:
1214+
1215+ [source,java,indent=0]
1216+ [subs="verbatim,quotes"]
1217+ ----
1218+ @GetMapping("/demo")
1219+ public void handle(**@CookieValue("JSESSIONID")** String cookie) {
1220+ //...
1221+ }
1222+ ----
1223+
1224+ Type conversion is applied automatically if the target method parameter type is not
1225+ `String`. See <<mvc-ann-typeconversion>>.
1226+
1227+
1228+
1229+ [[webflux-ann-initbinder]]
1230+ === Binder Methods
1231+ [.small]#<<web.adoc#mvc-ann-initbinder,Same in Spring MVC>>#
1232+
1233+ `@InitBinder` methods in an `@Controller` or `@ControllerAdvice` class can be used to
1234+ customize type conversion for method arguments that represent String-based request values
1235+ (e.g. request parameters, path variables, headers, cookies, and others). Type conversion
1236+ also applies during data binding of request parameters onto `@ModelAttribute` arguments
1237+ (i.e. command objects).
1238+
1239+ `@InitBinder` methods can register controller-specific `java.bean.PropertyEditor`, or
1240+ Spring `Converter` and `Formatter` components. In addition, the
1241+ <<webflux-config-conversion,WebFlux Java config>> can be used to register `Converter` and
1242+ `Formatter` types in a globally shared `FormattingConversionService`.
1243+
1244+ `@InitBinder` methods support many of the same arguments that a `@RequestMapping` methods
1245+ do, except for `@ModelAttribute` (command object) arguments. Typically they're are declared
1246+ with a `WebDataBinder` argument, for registrations, and a `void` return value.
1247+ Below is an example:
1248+
1249+ [source,java,indent=0]
1250+ [subs="verbatim,quotes"]
1251+ ----
1252+ @Controller
1253+ public class FormController {
1254+
1255+ **@InitBinder**
1256+ public void initBinder(WebDataBinder binder) {
1257+ SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
1258+ dateFormat.setLenient(false);
1259+ binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
1260+ }
1261+
1262+ // ...
1263+ }
1264+ ----
1265+
1266+ Alternatively when using a `Formatter`-based setup through a shared
1267+ `FormattingConversionService`, you could re-use the same approach and register
1268+ controller-specific ``Formatter``'s:
1269+
1270+ [source,java,indent=0]
1271+ [subs="verbatim,quotes"]
1272+ ----
1273+ @Controller
1274+ public class FormController {
1275+
1276+ **@InitBinder**
1277+ protected void initBinder(WebDataBinder binder) {
1278+ binder.addCustomFormatter(new DateFormatter("yyyy-MM-dd"));
1279+ }
1280+
1281+ // ...
1282+ }
1283+ ----
1284+
1285+
10901286
10911287
10921288include::webflux-functional.adoc[leveloffset=+1]
0 commit comments