我正在构建一个 API,其响应为multipart/form-data格式,其中包含application/json内容,但遇到了问题。
例如:
http://localhost:8080/getData
应该返回
--HaRZrgSMRFElYDqkcBMfTMp3BUMHKQAtP
Content-Disposition: form-data; name="response"
Content-Type: application/json
[{"name":"xyz"}]
--HaRZrgSMRFElYDqkcBMfTMp3BUMHKQAtP--
当前的代码片段是
@RequestMapping(value="/getData", method=RequestMethod.GET,
produces=MediaType.MULTIPART_FORM_DATA_VALUE)
public MultipartFile getMultipartAsFileAsObject() throws Exception {
ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource("sample.json").getFile());
String readFile = readFile("sample.json");
DiskFileItem fileItem = new DiskFileItem("file", "application/json", false, "response", (int) file.length() , file);
fileItem.getOutputStream();
MultipartFile multipartFile = new CommonsMultipartFile(fileItem);
return multipartFile;
}
我收到的响应是{}一个空的JSON对象。 可以有人告诉我哪里出错了吗?