spring-framework icon indicating copy to clipboard operation
spring-framework copied to clipboard

Spring RestClient returns an error when sending a MultipartFile "No serializer found for class sun.nio.ch.ChannelInputStream"

Open fachel opened this issue 1 year ago • 0 comments

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!!
}

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 I found solve for RestTemplate: How to send Multipart form data with restTemplate Spring-mvc - it work only for RestTemplate and not RestClient

fachel avatar Oct 16 '24 18:10 fachel