java 在Spring WebFlux中接收多个同名的multipart/form-data属性

gzjq41n4  于 2022-11-20  发布在  Java
关注(0)|答案(2)|浏览(187)

在前端,我们有一个表单,它将数据作为多部分/表单数据发送,我们需要在控制器中接收数据,同一属性可能有几条记录

Content-Disposition: form-data; name="ids"

30
-----------------------------313589022531437741264012237550
Content-Disposition: form-data; name="ids"

225
-----------------------------313589022531437741264012237550
Content-Disposition: form-data; name="ids"

226
-----------------------------313589022531437741264012237550
Content-Disposition: form-data; name="ids"

如何在控制器中接收这些值?

@PostMapping(value = "/create", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public Mono<String> create(@RequestPart("ids") List<Long> ids) {
// ...
}

List<Long>Long[]无法正常工作。
所述请求

MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder();
bodyBuilder.part("ids", 22);
bodyBuilder.part("ids", 33);

webTestClient.post()
        .uri("/create")
        .contentType(MediaType.MULTIPART_FORM_DATA)
        .body(BodyInserters.fromMultipartData(bodyBuilder.build()))
        .exchange()
        .expectStatus().isOk();

错误

org.springframework.web.server.ServerWebInputException: 400 BAD_REQUEST "Failed to read HTTP message"; nested exception is org.springframework.core.codec.DecodingException: JSON decoding error: Cannot deserialize value of type `java.util.ArrayList<java.lang.Integer>` from Integer value (token `JsonToken.VALUE_NUMBER_INT`); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize value of type `java.util.ArrayList<java.lang.Integer>` from Integer value (token `JsonToken.VALUE_NUMBER_INT`)
 at [Source: (org.springframework.core.io.buffer.DefaultDataBuffer$DefaultDataBufferInputStream); line: 1, column: 1]
    at org.springframework.web.reactive.result.method.annotation.AbstractMessageReaderArgumentResolver.handleReadError(AbstractMessageReaderArgumentResolver.java:224)
    at org.springframework.web.reactive.result.method.annotation.AbstractMessageReaderArgumentResolver.lambda$readBody$3(AbstractMessageReaderArgumentResolver.java:190)
    at reactor.core.publisher.FluxOnErrorResume$ResumeSubscriber.onError(FluxOnErrorResume.java:94)

下面的回购协议演示了这个问题:
https://github.com/alxxyz/spring-request-part-demo

bxgwgixi

bxgwgixi1#

如果您将ID形成为数组,则会正确处理它们:

MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder();
        bodyBuilder.part("ids", Arrays.asList(22, 33));
        //bodyBuilder.part("ids", 33);

        webTestClient.post()
                .uri("/create")
                .contentType(MediaType.MULTIPART_FORM_DATA)
                .body(BodyInserters.fromMultipartData(bodyBuilder.build()))
                .exchange()
                .expectStatus().isOk();
vatpfxk5

vatpfxk52#

试试看:

import org.springframework.http.codec.multipart.Part;
import java.nio.charset.Charset;

@PostMapping(value = "/create", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
public Mono<String> create(@RequestPart("ids") List<Part> ids) {
  var utf8 = Charset.fromString("UTF-8");

  // Part.content() returns a Flux<DataBuffer>.
  // This returns "33":
  return ids.get(1).content().map(dataBuffer -> dataBuffer.toString(utf8)).next();
}

相关问题