Webflux:将文件和DTO传递到单个请求中

问题描述 投票:1回答:1

我需要同时传递文件(通过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来完成此工作,但这并不是一个完美的解决方案。

java spring spring-webflux spring-rest
1个回答
1
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.