gpt4 book ai didi

spring - MissingServletRequestParameterException

转载 作者:行者123 更新时间:2023-12-03 21:26:29 24 4
gpt4 key购买 nike

我将一个 formData 对象传递给我的 Spring 后端:

imageBanner(banner: File, bannerPath: string, id: number, callback: (response) => void){
var formData = new FormData();
formData.append('name', banner.name);
console.log(formData.get('name'));
formData.append('file', banner);
console.log(formData.get('file'));

this.sendPost("/upload/" + bannerPath, formData, response => {
callback(response);
});

}

控制台日志显示:
1.jpg 
File {name: "1.jpg", lastModified: 1496737372408, lastModifiedDate: Tue Jun 06 2017 10:22:52 GMT+0200 (W. Europe Daylight Time), webkitRelativePath: "", size: 38983…}

所以看起来 formData 有一些值。

在后端我有这个:
@RequestMapping(value="/rest/upload/localeventbanner", method=RequestMethod.POST, headers = "content-type!=multipart/form-data")
public @ResponseBody String uploadFileHandler(@RequestParam("name") String name, @RequestParam("file") MultipartFile file) {

if (!file.isEmpty()) {
try {
log("success");

return "You successfully uploaded file=" + name;
} catch (Exception e) {
log("fail");
return "You failed to upload";
}
} else {
log("nope");
return "You failed to upload " + name
+ " because the file was empty.";
}
}

我在控制台中得到的返回是:

"{"timestamp":1502745177167,"status":400,"error":"Bad Request","exception":"org.springframework.web.bind.MissingServletRequestParameterException","message":"Required String parameter 'name' is not present","path":"/beheerback/rest/upload/localeventbanner"}"



它说 name 不存在,但是当我在前端记录它时,它显示 name 存在于 formData 对象中。

最佳答案

尝试为名称和文件提供 required = false ,例如:

@RequestParam(value = "name", required = false, defaultValue = "defaultName") String name, @RequestParam(value = "file" , required = false, defaultValue = "defaultFile") MultipartFile file

然后检查您真正获得的值。此外,请在浏览器的开发模式下使用网络检查器检查发送的表单。

关于spring - MissingServletRequestParameterException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45683153/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com