0

I am trying to upload a file as xlsx to a document.

@Autowired
HttpHeaders headers;
    

        headers.setContentType(MediaType.MULTIPART_FORM_DATA);          
        MultiValueMap<String, Object> body = new LinkedMultiValueMap<String, Object>();
        File fileToUpload = new File(attachmentLocation); // /tmp/xyz.xlsx
        body.add("file", new FileSystemResource(fileToUpload));    
        HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);    
        try {
            ResponseEntity<byte[]> response = restTemplate.exchange(myURL,
                    HttpMethod.POST, requestEntity, byte[].class);
        }
        catch (RestClientException e) {
            e.printStackTrace();
        }

I'm getting the following error message:

org.springframework.http.converter.HttpMessageConversionException: Type definition error: [simple type, class sun.nio.ch.ChannelInputStream]; nested exception is 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.util.LinkedMultiValueMap["file"]->java.util.ArrayList[0]->org.springframework.core.io.FileSystemResource["inputStream"])
Nidheesh
  • 4,390
  • 29
  • 87
  • 150

1 Answers1

0

This works for me. You need to disable FAIL_ON_EMPTY_BEANS in Jackson.

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ObjectMapperConfiguration {

  @Bean
  public ObjectMapper objectMapper() {
    return new ObjectMapper().disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
  }
}