SpringMVC 提示:HttpMediaTypeNotSupportedException: Content type 'multipart/form-data;boundary=----

今天在编写文件上传模块时,使用postman 上传文件测试相关功能时,提示如下错误信息:

"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"

错误代码:

@ApiOperation(httpMethod = "POST", value = "文件上传")
	@RequestMapping(value = "/fileUpload", method = { RequestMethod.POST })
	@ResponseBody
	public Result upload(
			@RequestBody @ApiParam(name = "上传对象", value = "json格式对象", required = true) ChunkInfoModel entity) {
		if(logger.isDebugEnabled()){
			logger.debug(entity.toString());
		}
		EfileInfo model = upload.smallFileUpload(entity);
		return Result.ok("文件上传成功").setDatas("model", model);
	}

正确代码:

    @RequestMapping(value = "/fileUpload", method = { RequestMethod.POST })
	@ResponseBody
	@ApiOperation(httpMethod = "POST", value = "文件上传")
	public Result upload(ChunkInfoModel entity) {
		if (logger.isDebugEnabled()) {
			logger.debug(entity.toString());
		}
		EfileInfo model = upload.smallAttachUpload(entity);
		
		return Result.ok("文件上传成功").setDatas("model", model);
	}

解决方案:

去掉 @RequestBody 注解就行了

你可能感兴趣的:(java,Web,异常篇)