我正在尝试上传一个文件作为xlsx到一个文档。
@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();
}
我收到以下错误消息:
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"])
1条答案
按热度按时间iovurdzv1#
这对我很有效。您需要在Jackson中禁用FAIL_ON_EMPTY_BEANS。