【问题标题】:Spring boot Web test client mock multipart file not workingSpring Boot Web测试客户端模拟多部分文件不起作用
【发布时间】:2021-08-22 04:55:48
【问题描述】:
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"])

我的邮递员在哪里工作得很好。附上postmanenter image description here的截图

【问题讨论】:

标签: spring-boot webtestclient


【解决方案1】:
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();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-28
    • 2021-10-24
    • 2021-04-02
    • 2017-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多