最近在做一個圖檔上傳功能、在入參 body 中同時傳入檔案和其它基本資訊結果出現如題異常、在此記錄下解決辦法、以做記錄。
controller 代碼如下:

1 @RequestMapping(value = "/upload", method =RequestMethod.POST)2 @ResponseBody3 public String upload(@RequestParam("file") MultipartFile file, @RequestBody User user) {4 // 業務處理5 ......6 ......7 8 }

在使用工具測試(Postman、 swagger )時報如下異常
"timestamp": 1473349676109,
"status": 415,
"error": "Unsupported Media Type",
"exception": "org.springframework.web.HttpMediaTypeNotSupportedException",
"message": "Content type 'multipart/form-data;boundary=----WebKitFormBoundaryTVc9eDC2a2elulOx;charset=UTF-8' not supported",
"path": "/upload"
解決方案:
去掉 @RequestBody 注解就行了

1 @RequestMapping(value = "/upload", method =RequestMethod.POST)2 @ResponseBody3 public String upload(@RequestParam("file") MultipartFile file, User user) {4 // 業務處理5 ......6 ......7 8 }