Spring MissingServletRequestParameterException

Spring MissingServletRequestParameterException,spring,angular,Spring,Angular,我正在将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.

我正在将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”:“请求错误”,“异常”:“org.springframework.web.bind.MissingServletRequestParameterException”,“message”:“所需字符串参数‘name’不存在”,“path”:“/beheerback/rest/upload/localeventbanner”}


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

尝试为name和file指定required=false,类似于:

@RequestParam(value = "name", required = false, defaultValue = "defaultName") String name, @RequestParam(value = "file" , required = false, defaultValue = "defaultFile") MultipartFile file
然后检查您真正得到的值。另外,在浏览器的开发模式下,使用网络检查器检查发送的表单