gpt4 book ai didi

java - Webflux : pass files and DTO into single request

转载 作者:行者123 更新时间:2023-12-04 10:26:08 25 4
gpt4 key购买 nike

我需要同时传递文件(通过 form-data )和 DTO。所以我尝试执行以下操作:

@PostMapping
public Mono<Void> method(@RequestPart("files") Flux<FilePart> files,
Dto dto) {
return Mono.empty();
}

并初始化 Dto通过参数为每个 dto的领域。

所以我收到以下错误:
org.springframework.core.codec.CodecException: Type definition error: [simple type, class org.springframework.http.codec.multipart.SynchronossPartHttpMessageReader$SynchronossFilePart]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.springframework.http.codec.multipart.SynchronossPartHttpMessageReader$SynchronossFilePart and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: java.util.LinkedHashMap["errors"]->java.util.Collections$UnmodifiableList[0]->org.springframework.validation.FieldError["rejectedValue"])
Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.springframework.http.codec.multipart.SynchronossPartHttpMessageReader$SynchronossFilePart and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: java.util.LinkedHashMap["errors"]->java.util.Collections$UnmodifiableList[0]->org.springframework.validation.FieldError["rejectedValue"])

如果我删除 Dto,图像将正确加载参数完全。

添加 @RequestBody Dto的注释参数产生以下错误:
Content type 'multipart/form-data;charset=UTF-8;boundary=rh4lsv9DycBf8hpV2snhKfRjSrj1GvHzVy' not supported for bodyType=com.example.Dto

通过 Dto 得到了这个工作如 @RequestParam("dto") String dto并手动解析 JSON,但这并不是一个完美的解决方案。

最佳答案

所以你需要所谓的混合多部分

@PostMapping("/test")
public Mono<Void> method(@RequestPart("dto") Dto dto, @RequestPart("files") Flux<FilePart> files) {

return Mono.empty();
}

这适用于这个 http 请求:
POST /test HTTP/1.1
Host: localhost:8080
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
Cache-Control: no-cache
Postman-Token: 425629ec-335f-4d49-8df4-d6130af67889

------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="files"; filename="Capture.PNG"
Content-Type: image/png


------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="dto"
Content-Type: application/json

{"test":"aaa"}

###

笔记:
   Content-Type: application/json

dto 部分是 强制性

看到这个答案:

Spring MVC Multipart Request with JSON

关于java - Webflux : pass files and DTO into single request,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60629962/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com