简体   繁体   English

使用Spring Boot使用自定义DTO进行多部分请求

[英]Multipart Request with Custom DTO using Spring Boot

Below is my Rest api which accepts a multipart file and a custom dto 下面是我的Rest API,它接受多部分文件和自定义dto

@RequestMapping(path="/manual", method = RequestMethod.POST) 
public ResponseEntity<?> createObject(@RequestPart CustomDTO dto, @RequestPart(name = "file", required = true) MultipartFile file) { 

    //Some code here

}

Following is a CustomDTO 以下是CustomDTO

public class CustomDTO implements Serializable { 

// Few attributes and its respective getters and setter are there

}

Now on making a request it gives me an error as below 现在在发出请求时,它给我一个错误,如下所示 在此处输入图片说明

But if in rest api instead of CustomDTO if I make the data type as String then it works properly and then using objectmapper I am able to convert it to CustomDTO . 但是,如果我将数据类型设置为String,则使用rest api而不是CustomDTO ,则它可以正常工作,然后使用objectmapper可以将其转换为CustomDTO But please help me out on how to directly accept it as CustomDTO and not String. 但是,请帮助我了解如何直接将其作为CustomDTO而不是String接受。

Note: Am using Spring Boot 注意:我正在使用Spring Boot

try below one. 尝试以下一项。

@RequestMapping(value = "/manual", method = RequestMethod.POST)
public ResponseEntity<?> createObject( @Valid CustomDTO dto, @RequestParam("file") MultipartFile file ) {
        //
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM