Can we use multipart and @RequestBody together in spring..?

安稳与你 提交于 2019-12-05 04:10:19

You can use @RequestPart like below. This will support both json object and multipart file.

@ResponseBody
public ResponseEntity<String>
saveReport(@RequestPart (value="reportFile") MultipartFile reportFile,
           @RequestPart LabPatientInfo reportData) throws IOException {

In order to test it using curl you can create one file for your json part (reportData). Say for example you create "mydata.json" file and paste your json payload in it. And say your reportFile is "report.txt". Now you can send request using curl like below.

curl -v -H "Content-Type:multipart/form-data" -F "reportData=@mydata.json;type=application/json" -F "reportFile=@report.txt;type=text/plain"  http://localhost:8080/MyApp/lab/saveReport

Spring Roo 2.0.0.M3 includes support for automatic scaffolding of a REST API.

For complete information, see the REST API in the reference manual.

Note the M3 version generate artifacts that could change in newer versions, so your project might not upgrade automatically if you open it with RC1 or above.

May the Force be with you.

When a parameter is annotated with @RequestPart the content of the part is passed through an HttpMessageConverter to resolve the method argument with the 'Content-Type' of the request part in mind. This is analogous to what @RequestBody does to resolve an argument based on the content of a regular request.

so, we can parse @Requestbody as @RequestPart as "abaghel" and reportData need to be a json file.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!