无法编写 JSON:未找到类 java.io.FileDescriptor 的序列化程序,也未发现用于创建 BeanSerializer 的属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33740141/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 22:03:52  来源:igfitidea点击:

Could not write JSON: No serializer found for class java.io.FileDescriptor and no properties discovered to create BeanSerializer

javaspringrestpost

提问by Dhanu

I am trying to do and https post to an site using spring rest template(It accepts post, but doesn't accept JSON) with Spring MultiPart file upload.

我正在尝试使用 spring 休息模板(它接受发布,但不接受 JSON)和 Spring MultiPart 文件上传将 https 发布到站点。

Following error was received, when doing so,

收到以下错误,执行此操作时,

org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: No serializer found for class java.io.FileDescriptor and no properties discovered to create BeanSerializer.

org.springframework.http.converter.HttpMessageNotWritableException:无法编写 JSON:找不到类 java.io.FileDescriptor 的序列化程序,也没有发现用于创建 BeanSerializer 的属性。

MultiValueMap<String, Object> formData = new LinkedMultiValueMap<String, Object>();
formData.add("NUMBER", "ABC");
formData.add("ID", "123");
formData.add("FILE",file); // this is spring multipart file
HttpHeaders headers = new HttpHeaders();
headers.set("Content-Type", "multipart/form-data"); 
headers.set("Accept", "text/plain"); 
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<MultiValueMap<String, Object>>(formData, headers);
RestTemplate restTemplate = getRestTemplate();

String result  = restTemplate.postForObject(uploadUri, requestEntity, String.class);

回答by Tharsan Sivakumar

I too faced the same problem and I was able to sort this out when I was using ByteArrayResource instead of FileSystem resource. I copied the byte contents of the multipart file into the httprequest entity using ByteArrayResource.

我也遇到了同样的问题,当我使用 ByteArrayResource 而不是 FileSystem 资源时,我能够解决这个问题。我使用 ByteArrayResource 将多部分文件的字节内容复制到 httprequest 实体中。

Iterator<String> itr = request.getFileNames();
MultipartFile file = request.getFile(itr.next());
//Set the headers
................
formData .add("files", new ByteArrayResource(file.getBytes()));

You may refer this linkto get more info

您可以参考此链接以获取更多信息

回答by Max Telepchuk

Updating gradle plugin to version

将 gradle 插件更新到版本

id "io.spring.dependency-management" version "1.0.9.RELEASE"

helped me

帮助过我