1
MockMultipartFile file = new MockMultipartFile("files",
                             "Test.txt", 
                             "text/plain", 
  this.getClass().getResourceAsStream("/Test.txt"));
        
webTestClient.post()
                .uri("/foo").header("test", "1")
                .header("test1", "1")
                .contentType(MediaType.MULTIPART_FORM_DATA)
                .body(BodyInserters.fromMultipartData("files", file))
            .exchange()
                .expectStatus().isOk();

获取异常:

org.springframework.core.codec.CodecException: 
Type definition error: [simple type, class java.io.ByteArrayInputStream]; 
nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: 
No serializer found for class java.io.ByteArrayInputStream and no properties discovered to create BeanSerializer
 (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS)
 (through reference chain: org.springframework.mock.web.MockMultipartFile["inputStream"])

因为它和我的邮递员一起工作得很好。请在此处找到邮递员的附件截图输入图像描述

4

1 回答 1

0
webTestClient.post()
                .uri("/foo").header("test", "1")
                .header("test1", "1")
                .contentType(MediaType.MULTIPART_FORM_DATA)
                .body(BodyInserters.fromMultipartData("files", this.getClass().getResource("/Test.txt")))
            .exchange()
                .expectStatus().isOk();
于 2021-06-04T06:31:01.923 回答