[英]How To Mapping Multipart file to DTO using Spring Boot for incoming request
I have this DTO Request Class我有这个 DTO 请求 Class
Public Class MyRequestDTO(){
private String name;
private MultipartFile docPic;
}
i want to retrieve in in controller我想在 controller 中检索
@PostMapping("/test")
public String test(@RequestBody MyRequestDTO dto){
system.out.print(dto.getdocPic.getOriginalFileName());
return "success;
}
but always get javaNullexception when i try to display this newly requested file, is it correct to mapping multipart file inside DTO?但是当我尝试显示这个新请求的文件时总是得到 javaNullexception,在 DTO 中映射多部分文件是否正确?
With the latest spring boot we have a running example without the @RequestBody annotation, so try this:使用最新的 spring 启动,我们有一个没有 @RequestBody 注释的运行示例,所以试试这个:
@PostMapping("/test")
public String test(MyRequestDTO dto){
If that doesn't work, the next spot you should double check is your request.如果这不起作用,下一个你应该仔细检查的地方是你的请求。 Specifically the
Content-type
.特别是
Content-type
。 The above signature works for us with a request that has a content type of: multipart/form-data
上面的签名适用于内容类型为:
multipart/form-data
的请求
You're getting javaNullexception because you're receiving MyRequestDTO dto as null or some property of dto object is pointing to null.您收到 javaNullexception 是因为您收到 MyRequestDTO dto 作为 null 或 dto object 的某些属性指向 null。
So please be sure before calling the getter method on a null object.所以在调用null object上的getter方法之前请务必确定。
Firstly get that dto.getdocPic is not pointing to null.首先得到 dto.getdocPic 没有指向 null。
@PostMapping("/test")
public String test(@RequestBody MyRequestDTO dto){
if(dto.getdocPic() != null){
system.out.print(dto.getdocPic().getOriginalFileName());
}
return "success;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.