OiO.lk Blog java How to send Multipart form data with restTemplate Spring-mvc
java

How to send Multipart form data with restTemplate Spring-mvc


I try send request via RestClient:

fun uploadFile(file: MultipartFile): MyDtoRs {
  val resource: Resource = file.resource
  val parts = LinkedMultiValueMap<String, Any>()
  parts.add("file", resource)
  val httpHeaders = HttpHeaders()
  httpHeaders.contentType = MediaType.MULTIPART_FORM_DATA
  val httpEntity = HttpEntity(parts, httpHeaders)
  val restClient = RestClient.create()

  return restClient.post()
            .uri("my/url")
            .body(httpEntity)
            .retrieve()
            .toEntity(MyDtoRs::class.java)
            .body!!
}

When sending the MultipartFile body via RestClient, I get an error

"Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException:No serializer found for class sun.nio.ch.ChannelInputStream and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: org.springframework.http.HttpEntity["body"]->org.springframework.util.LinkedMultiValueMap["file"]->java.util.ArrayList[0]->org.springframework.web.multipart.MultipartFileResource["inputStream"])"

Spring Boot version 3.3.2

How to send Multipart form data with restTemplate Spring-mvc – it work only for RestTemplate and not RestClient



You need to sign in to view this answers

Exit mobile version