I'm trying to perform POST request with Multipart Form Data. I have this controller receiving request
@PostMapping(value = "/photos", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
@Operation(summary = "Accepts photo. Adds photo to queue for sending to Telegram chat")
private void sendPhotos(@RequestPart List<MultipartFile> photos,
@RequestParam long cityId,
@RequestParam(required = false) String caption) {
sendToCityChatUseCase.execute(photos, cityId, caption);
}
I'm using RestTemplate to send List of MultipartFile:
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
MultiValueMap<String, Object> files = new LinkedMultiValueMap<>();
for (MultipartFile file : photos) {
files.add("photos", file);
}
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(files, headers);
ResponseEntity<String> response = restTemplate.postForEntity(chatBotUrl, requestEntity,String.class);
When I use such implementation I get an exception:
org.springframework.http.converter.HttpMessageConversionException: Type definition error: [simple type, class java.io.FileDescriptor]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class java.io.FileDescriptor and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: org.springframework.web.multipart.support.StandardMultipartHttpServletRequest$StandardMultipartFile["inputStream"]->java.io.FileInputStream["fd"])
When I've changed files.add("photos", file);
to files.add("photos", file.getBytes());
I've managed to perform request but got MissingServletRequestPartException
because there wasn't photos
part in that request.
I think I'm missing something. Have no pleasure writing new converter myself and hope there is other way