ivanjermakov :
I need to pass both file (through form-data
) and DTO. So I try to do the following:
@PostMapping
public Mono<Void> method(@RequestPart("files") Flux<FilePart> files,
Dto dto) {
return Mono.empty();
}
and initialize Dto
through parameters for each dto
's field.
So I get the following error:
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"])
Images are loaded correctly if I remove Dto
parameter completely.
Adding @RequestBody
annotation to the Dto
parameter produces the following error:
Content type 'multipart/form-data;charset=UTF-8;boundary=rh4lsv9DycBf8hpV2snhKfRjSrj1GvHzVy' not supported for bodyType=com.example.Dto
Got this working by passing Dto
as @RequestParam("dto") String dto
and parsing JSON manually, but it is not really a perfect solution.
Nonika :
So you need so called mixed multipart
@PostMapping("/test")
public Mono<Void> method(@RequestPart("dto") Dto dto, @RequestPart("files") Flux<FilePart> files) {
return Mono.empty();
}
this works with this http request:
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"}
###
Note:
Content-Type: application/json
for dto part is mandatory
See this answer: