Java Webflux:将文件和DTO传递到单个请求中
我需要传递文件(通过Java Webflux:将文件和DTO传递到单个请求中,java,spring,spring-webflux,spring-rest,Java,Spring,Spring Webflux,Spring Rest,我需要传递文件(通过表单数据)和DTO。因此,我尝试做以下工作: @PostMapping 公共Mono方法(@RequestPart(“文件”)通量文件, Dto(Dto){ 返回Mono.empty(); } 并通过每个Dto字段的参数初始化Dto 因此,我得到以下错误: org.springframework.core.codec.CodecException: Type definition error: [simple type, class org.springframework.
表单数据)和DTO。因此,我尝试做以下工作:
@PostMapping
公共Mono方法(@RequestPart(“文件”)通量文件,
Dto(Dto){
返回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"])
Content type 'multipart/form-data;charset=UTF-8;boundary=rh4lsv9DycBf8hpV2snhKfRjSrj1GvHzVy' not supported for bodyType=com.example.Dto
如果我完全删除Dto
参数,图像将正确加载
将@RequestBody
注释添加到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"])
Content type 'multipart/form-data;charset=UTF-8;boundary=rh4lsv9DycBf8hpV2snhKfRjSrj1GvHzVy' not supported for bodyType=com.example.Dto
通过将Dto
作为@RequestParam(“Dto”)字符串Dto
传递并手动解析JSON来实现这一点,但这并不是一个完美的解决方案。所以需要所谓的混合多部分
@PostMapping("/test")
public Mono<Void> method(@RequestPart("dto") Dto dto, @RequestPart("files") Flux<FilePart> files) {
return Mono.empty();
}
注:
Content-Type: application/json
对于dto零件,必须执行命令
看看这个答案:
您是否尝试为Dto添加@RequestBody
?这对字符串
部分有效,但在我的情况下,我遇到了此错误内容类型'text/plain;bodyType=com.example.Dto不支持charset=UTF-8'将内容类型:application/json添加到Dto部件