Browse Source

Refine multipart-forms.adoc

See gh-36094
pull/36110/head
Sébastien Deleuze 4 weeks ago
parent
commit
c5044bfdbc
  1. 8
      framework-docs/modules/ROOT/pages/web/webflux/controller/ann-methods/multipart-forms.adoc
  2. 13
      framework-docs/src/main/java/org/springframework/docs/web/webflux/controller/annmethods/partevent/PartEventController.java
  3. 12
      framework-docs/src/main/kotlin/org/springframework/docs/web/webflux/controller/annmethods/partevent/PartEventController.kt

8
framework-docs/modules/ROOT/pages/web/webflux/controller/ann-methods/multipart-forms.adoc

@ -41,8 +41,8 @@ Kotlin:: @@ -41,8 +41,8 @@ Kotlin::
[source,kotlin,indent=0,subs="verbatim,quotes"]
----
class MyForm(
val name: String,
val file: FilePart)
private val name: String,
private val file: FilePart)
@Controller
class FileUploadController {
@ -104,7 +104,7 @@ Kotlin:: @@ -104,7 +104,7 @@ Kotlin::
----
@PostMapping("/")
fun handle(@RequestPart("meta-data") metadata: Part, // <1>
@RequestPart("file-data") file: FilePart): String { // <2>
@RequestPart("file-data") file: FilePart): String { // <2>
// ...
}
----
@ -170,7 +170,7 @@ Kotlin:: @@ -170,7 +170,7 @@ Kotlin::
----
@PostMapping("/")
fun handle(@Valid @RequestPart("meta-data") metadata: Mono<MetaData>): String {
// ...
// use one of the onError* operators...
}
----
======

13
framework-docs/src/main/java/org/springframework/docs/web/webflux/controller/annmethods/partevent/PartEventController.java

@ -31,29 +31,28 @@ public class PartEventController { @@ -31,29 +31,28 @@ public class PartEventController {
// tag::snippet[]
@PostMapping("/")
public void handle(@RequestBody Flux<PartEvent> allPartsEvents) { // Using @RequestBody.
public void handle(@RequestBody Flux<PartEvent> allPartEvents) {
// The final PartEvent for a particular part will have isLast() set to true, and can be
// followed by additional events belonging to subsequent parts.
// This makes the isLast property suitable as a predicate for the Flux::windowUntil operator, to
// split events from all parts into windows that each belong to a single part.
allPartsEvents.windowUntil(PartEvent::isLast)
allPartEvents.windowUntil(PartEvent::isLast)
// The Flux::switchOnFirst operator allows you to see whether you are handling
// a form field or file upload.
// a form field or file upload
.concatMap(p -> p.switchOnFirst((signal, partEvents) -> {
if (signal.hasValue()) {
PartEvent event = signal.get();
if (event instanceof FormPartEvent formEvent) {
String value = formEvent.value();
// Handling the form field.
// Handling of the form field
}
else if (event instanceof FilePartEvent fileEvent) {
String filename = fileEvent.filename();
// The body contents must be completely consumed, relayed, or released to avoid memory leaks.
// The body contents must be completely consumed, relayed, or released to avoid memory leaks
Flux<DataBuffer> contents = partEvents.map(PartEvent::content);
// Handling the file upload.
// Handling of the file upload
}
else {
return Mono.error(new RuntimeException("Unexpected event: " + event));

12
framework-docs/src/main/kotlin/org/springframework/docs/web/webflux/controller/annmethods/partevent/PartEventController.kt

@ -31,29 +31,29 @@ class PartEventController { @@ -31,29 +31,29 @@ class PartEventController {
// tag::snippet[]
@PostMapping("/")
fun handle(@RequestBody allPartsEvents: Flux<PartEvent>) { // Using @RequestBody.
fun handle(@RequestBody allPartEvents: Flux<PartEvent>) {
// The final PartEvent for a particular part will have isLast() set to true, and can be
// followed by additional events belonging to subsequent parts.
// This makes the isLast property suitable as a predicate for the Flux::windowUntil operator, to
// split events from all parts into windows that each belong to a single part.
allPartsEvents.windowUntil(PartEvent::isLast)
allPartEvents.windowUntil(PartEvent::isLast)
.concatMap {
// The Flux::switchOnFirst operator allows you to see whether you are handling
// a form field or file upload.
// a form field or file upload
it.switchOnFirst { signal, partEvents ->
if (signal.hasValue()) {
val event = signal.get()
if (event is FormPartEvent) {
val value: String = event.value()
// Handling the form field.
// Handling of the form field
} else if (event is FilePartEvent) {
val filename: String = event.filename()
// The body contents must be completely consumed, relayed, or released to avoid memory leaks.
// The body contents must be completely consumed, relayed, or released to avoid memory leaks
val contents: Flux<DataBuffer> = partEvents.map(PartEvent::content)
// Handling the file upload.
// Handling of the file upload
} else {
return@switchOnFirst Mono.error(RuntimeException("Unexpected event: $event"))
}

Loading…
Cancel
Save