mirror of
https://github.com/spring-projects/spring-framework
synced 2026-06-08 17:33:33 +00:00
Refine multipart-forms.adoc
See gh-36094
This commit is contained in:
+4
-4
@@ -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::
|
||||
----
|
||||
@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::
|
||||
----
|
||||
@PostMapping("/")
|
||||
fun handle(@Valid @RequestPart("meta-data") metadata: Mono<MetaData>): String {
|
||||
// ...
|
||||
// use one of the onError* operators...
|
||||
}
|
||||
----
|
||||
======
|
||||
|
||||
+6
-7
@@ -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));
|
||||
|
||||
+6
-6
@@ -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"))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user