[英]com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `org.springframework.web.multipart.MultipartFile`
I'm trying to access an uploaded file from the spring boot controller, but I'm getting error我正在尝试从 Spring Boot 控制器访问上传的文件,但出现错误
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of springframework.web.multipart.MultipartFile
FormDomain class FormDomain 类
public class FormDomain {
@JsonPropertyDescription
private MultipartFile fileInput;
public MultipartFile getFileInput() {
return fileInput;
}
public void setFileInput(MultipartFile fileInput) {
this.fileInput = fileInput;
}
}
Form Controller Class表单控制器类
@CrossOrigin(origins="*")
@RestController
public class FormController {
@PostMapping(path="/basicForm")
public String postResponseController(
@RequestBody FormDomain loginForm) throws IOException {
return "file transfer completed successfully";
}
Angular form角形
<form #basicForm = "ngForm" (ngSubmit) = "onClickSubmit(basicForm.value)" enctype="multipart/form-data" class="form-horizontal">
<input type="file" id="file-input" name="fileInput" ngModel (change)="selectFile($event)">
<button type="submit"> Submit</button>
</form>
Select File method选择文件方法
selectFile(event) {
this.selectedFiles = event.target.files[0];
console.log(this.selectedFiles);
}
Angular method that submits form提交表单的 Angular 方法
onClickSubmit(data) {
data.fileInput=this.selectedFiles;
this.http.post("http://localhost:8080/basicForm", data).subscribe( (ob)=>(console.log("subscribe method called")));
}
How to solve this issue?如何解决这个问题?
That is Not possible with RequestBody annotation.使用 RequestBody 注释是不可能的。
You can achive with using like below.您可以使用如下方法实现。
public String postResponseController(@RequestParam("multipartFile") MultipartFile multipartFile) throws IOException {
//do somthing
}
I hope this will helpful for you.我希望这对你有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.