Content type 'multipart/form-data;boundary=----WebKitF;charset=UTF-8' not supported
在vue给springboot发请求时,传了两个参数:(1)pojo类数据(2)MultipartFile数据
前端传输格式:imgobj是一个object,默认传值为null
this.$refs['ruleForm'].validate((flag) => {
//如果通过验证,则进行表单数据提交
if(flag) {
if(this.editId) {
this.myPost(this.url.edit, {
id: this.forms.id,
loginName: this.forms.loginName,
name: this.forms.name,
email: this.forms.email,
phone: this.forms.phone,
imgobj: this.forms.imgobj
}, (response) => {
this.$emit('getTable')
this.$emit('update:show', false)
})
} else {
this.myPost(this.url.edit, this.forms, (response) => {
this.$emit('getTable')
this.$emit('update:show', false)
})
}
}
})
后端报错时写法:
public CommonResult addUmsAdmin(@RequestBody UmsAdmin umsAdmin,
MultipartFile imgobj) throws xxxExp {
// do somethings
}
此时发现后端接收到的图片为null,而验证后发现前端上传的文件没问题。
排查问题:在上传文件的时候,spring框架会自动装配文件类型, 使用@RequestBody接收对象,所对应的content-type :application/json。所以当使用@RequestBody同时进行文件上传的时候,会报错。
解决方案:
去掉pojo类参数前的@RequestBody注解,对后续的MultipartFile类参数使用@RequestParams("imgobj"),注意imgobj与前端传输的文件对象名,与后端MultipartFile参数名命名需一致。
public CommonResult addUmsAdmin(UmsAdmin umsAdmin,
@RequestParam("imgobj") MultipartFile imgobj) throws xxxExp {
// do somethings
}