在做文件上传时,采用@RequestBody接受映射参数,在如下代码测试时,出现报错
@RequestMapping(value = "/upload", method =RequestMethod.POST) public String upload(@RequestParam("file") MultipartFile file, @RequestBody @Valid FileVO fileVO) { ... }
控制面板上显示:WARN org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver -Resolved [org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'multipart/form-data;boundary=--------------------------117954444450080952702590;charset=UTF-8' not supported]
只需要将方法中接受参数的实体类前@RequestBody去除即可,因为接受文件本身就是form-data类型,不需要额外再加注解
@RequestMapping(value = "/upload", method =RequestMethod.POST) public String upload(@RequestParam("file") MultipartFile file, @Valid FileVO fileVO) { ... }
这样问题就得到了解决