I want to pass json and file together in the controller using curl. I have following method in controller.
@PostMapping(value = /api/campaign, headers = {"content-type=multipart/mixed","content-type=multipart/form-data"})
@ResponseBody
@ResponseStatus(HttpStatus.CREATED)
public @Valid ResponseDTO campaignCreator (@Valid @RequestBody CampaignCreatorDTO campaignCreatorDTO, @RequestPart("file") MultipartFile adGraphic){
}
Below is the curl command
curl -i -X POST -H "Content-Type: multipart/mixed" -d "campaignCreatorDTO={\"edipi\":123456789,\"firstName\":\"John\",\"lastName\":\"Smith\",\"email\":\"john.smith@gmail.com\"};type=application/json" -F "file=@newfile.png;type=image/png" http://localhost:8080/api/campaign
controller method is not getting called. when I put json in a file and use file in curl command in place of direct json it works. But I do not want to use file for json.
I tried to use @RequestPart for json object but same issue.
is there any way to pass multipart file inside json I mean CampaignCreatorDTO object?
Update:: Now I am able to pass RequestPart for both type but image size is coming 0 bytes. Though iamge is present in the filesystem.
Updated Code :: now using below code, and getting filesize as 0 bytes.
@PostMapping(value = /api/campaign, consumes = {"multipart/form-data","multipart/mixed"})
@ResponseBody
@ResponseStatus(HttpStatus.CREATED)
public @Valid ResponseDTO campaignCreator (@Valid @RequestPart("json") CampaignCreatorDTO campaignCreatorDTO, @RequestPart("file") MultipartFile adGraphic) {
}
I tried solution given in this link but still same issue
Spring MVC Multipart Request with JSON
this is how client is paasing the data to server
let formData = new FormData()
const blob = new Blob([json], {
type: 'application/json'
});
formData.append("json", blob)
formData.append("file", values.adCreativeImageCover)
let authToken = sessionStorage.getItem("authToken")
fetch(/api/campaign, {
method: "POST",
headers: {
'Accept': 'application/json',
'X-Auth-Token': authToken,
},
mode: 'cors',
body: formData
})
I solved the issue by implementing multipartresolver in configuration.
@Bean
public MultipartResolver multipartResolver() {
return new StandardServletMultipartResolver();
}
First way: If you will use postman/javaScript framework -> you have to define media-type of every RequestPart. example in POSTMAN:
Second way is implement maintaining of octet-stream, because spring set it by default in AbstractMessageConverterMethodArgumentResolver.class
if there is no content-type header in request part. (I'm using spring-boot-starter-parent 2.4.1 -> spring-webmvc 5.3.2
)
So here is 2 cases I found (to map response JSON to DTO):
1 case (not recommended): Set supporting to JSON converter:
2 case: Define new Converter class which will work definitely for needed classes (in my case it is MDVersionDTO.class
) and definitely when it comes like octet-stream , to implement it, ovveride canRead()
method.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.