Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Webflux:将文件和DTO传递到单个请求中_Java_Spring_Spring Webflux_Spring Rest - Fatal编程技术网

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部件