我有接受文件和JSON對象以下資源(使用Spring 4.05.RELEASE實現):如何在Spring寧靜服務中處理由文件和JSON對象組成的多部分請求?
(PS activityTemplate是可序列化的實體類)
...
@RequestMapping(value="/create", method=RequestMethod.POST)
public @ResponseBody ActivityTemplate createActivityTemplate(
@RequestPart ActivityTemplate activityTemplate, @RequestPart MultipartFile jarFile)
{
//process the file and JSON
}
...
,這是形式I我從測試:
<form method="POST" enctype="multipart/form-data"
action="http://localhost:8080/activityTemplates/create">
JSON: <input type="text" name="activityTemplate" value='/* the JSON object*/'><br />
File to upload: <input type="file" name="file">
<input type="submit" value="Upload">
</form>
,這是我的錯誤:
There was an unexpected error (type=Unsupported Media Type, status=415).
Content type 'application/octet-stream' not supported
那麼應該如何讓資源接受JSON對象作爲多部分請求的一部分,還是應該以不同的方式發送表單?
您可以將json對象作爲請求參數請求。 – 2014-12-04 13:10:32
@DarshanLila使用請求參數時,它將JSON對象看作是一個字符串,並且不會將其轉換爲開箱即用的相應對象。 – Sami 2014-12-04 13:29:33
在簽名('jarFile')中,文件輸入的名稱('file')和註釋的參數名稱之間存在不匹配。這對你的問題有什麼要求嗎? – miw 2015-01-09 19:51:07