简体   繁体   English

如何使用 Spring 引导传入请求将多部分文件映射到 DTO

[英]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.

相关问题 使用Spring Boot使用自定义DTO进行多部分请求 - Multipart Request with Custom DTO using Spring Boot Spring 引导 controller - 上传多部分和 JSON 到 DTO - Spring Boot controller - Upload Multipart and JSON to DTO Spring 启动:使用 ModelMapper 在 DTO 和 Entity 之间进行自定义映射 - Spring boot: Custom mapping between DTO and Entity using ModelMapper Spring 启动 kafka 消息传递。 如何简化处理程序的 dto 映射? - Spring boot kafka messaging. How to simplify dto mapping for handlers? React Spring 使用多部分表单数据启动应用程序 - 所需的请求部分“文件”不存在 - React Spring Boot App Using Multipart Form Data - Required request part 'file' is not present 在Spring Boot Rest中将GET请求参数与DTO对象映射时出现的问题 - Issue when mapping GET request parameters with DTO object in Spring Boot Rest 在请求spring boot java中识别作为多部分文件出现的相同图像 - Identifying the same image in coming as multipart file in request spring boot java 在Spring Boot中将传入的JSON映射到类 - Mapping incoming JSON to a class in spring boot 如何在Spring Boot中指定回退请求映射 - How to specify a fallback request mapping in Spring Boot 使用AngularJS的Java Spring Boot多部分POST请求 - Java Spring Boot multipart POST request using AngularJS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM