org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'multipart/form-data;bounda分析

阿里 阅读:266 2021-03-31 16:58:10 评论:0

今天在编写SpringBoot 文件上传遇到这样的错误:org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'multipart/form-data;bounda

特此记录下:

错误的Controller 代码:

@ApiOperation(httpMethod = "POST", value = "文件块上传") 
	@RequestMapping(value = "/upload", method = { RequestMethod.POST }) 
	@ResponseBody 
	public Result upload( 
			@RequestBody @ApiParam(name = "文件块对象", value = "表单对象")ChunkInfoModel          
            entity, HttpServletRequest request, HttpServletResponse response) { 
 
			UploadFileCallback callback = new UploadFileCallback(entity, "upload", config.getLocation()); 
			Future<Result> result = this.uploadExecutor.submit(callback); 
			try { 
				return result.get(); 
			} catch (InterruptedException e) { 
				// TODO Auto-generated catch block 
				log.error(e.getMessage()); 
				e.printStackTrace(); 
			} catch (ExecutionException e) { 
				// TODO Auto-generated catch block 
				log.error(e.getMessage()); 
				e.printStackTrace(); 
			} 
		return Result.ok("文件块上传成功"); 
	}

正确的Controoler 代码:

@ApiOperation(httpMethod = "POST", value = "文件块上传") 
	@RequestMapping(value = "/upload", method = { RequestMethod.POST }) 
	@ResponseBody 
	public Result upload( 
			ChunkInfoModel entity, 
			HttpServletRequest request, HttpServletResponse response) { 
 
			UploadFileCallback callback = new UploadFileCallback(entity, "upload", config.getLocation()); 
			Future<Result> result = this.uploadExecutor.submit(callback); 
			try { 
				return result.get(); 
			} catch (InterruptedException e) { 
				// TODO Auto-generated catch block 
				log.error(e.getMessage()); 
				e.printStackTrace(); 
			} catch (ExecutionException e) { 
				// TODO Auto-generated catch block 
				log.error(e.getMessage()); 
				e.printStackTrace(); 
			} 
		return Result.ok("文件块上传成功"); 
	}

 原因:首先你在  multipart/form-data 就不能使用 @RequestBody

 

@RequestBody 的用法:

 1. 该注解用于读取Request请求的body部分数据,使用系统默认配置的HttpMessageConverter进行解析,然后把相应的数据绑定到要返回的对象上;

  2. 再把HttpMessageConverter返回的对象数据绑定到 controller中方法的参数上。

springboot  默认配置核心代码,涉及请求request数据转换配置:

 

总结:RequestBody 支持的请求方式和内容类型

请求方式 内容方式 支持/不支持
POST/GET application/x-www-form-urlencoded [email protected]@RequestBody
POST/GET  multipart/form-data [email protected]
POST/GET application/json、application/xml [email protected]
PUT application/x-www-form-urlencoded [email protected]@RequestBody
PUT  multipart/form-data [email protected]
PUT application/json、application/xml [email protected]
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

发表评论
搜索
KIKK导航

KIKK导航

排行榜
关注我们

一个IT知识分享的公众号