java 我们可以在 spring.. 中同时使用 multipart 和 @RequestBody 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40924649/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can we use multipart and @RequestBody together in spring..?
提问by Mayur
I want to create a API which can have parameter as multipart file and JSON object (@RequestBody). Please find following snippet while calling this API I am getting 415 HTTP error. If I remove "@RequestBody LabPatientInfo reportData" then it works fine.
我想创建一个 API,它可以将参数作为多部分文件和 JSON 对象(@RequestBody)。请在调用此 API 时找到以下代码段我收到 415 HTTP 错误。如果我删除“@RequestBody LabPatientInfo reportData”,那么它工作正常。
@RequestMapping(value={"/lab/saveReport"}, method={RequestMethod.POST},
consumes={"multipart/form-data"}, headers={"Accept=application/json"})
@ResponseBody
public ResponseEntity<String>
saveReport(@RequestParam(value="reportFile") MultipartFile reportFile,
@RequestBody LabPatientInfo reportData) throws IOException {
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type", "application/json; charset=utf-8");
logger.info("in Lab Save Report");
logger.info("Report Data {} ", reportData);
//logger.info("Request BODY {} ", request.getAttribute("data"));
return new ResponseEntity<String>(HttpStatus.OK);
}
following is LabPatientInfo class.
以下是 LabPatientInfo 类。
@RooJson(deepSerialize = true)
@RooToString
public class LabPatientInfo {
private String firstName;
private String phoneNumber;
private String DateOfBirth;
private Integer age;
private String gender;
private String refferedBy;
private String reportfile;
private String reportType;
private String reportDate;
private String purpose;
private String followUpDate;
private List<ReportDataInfo> analytes;
while hitting API I am passing following JSON object with uploaded file..
在点击 API 时,我正在传递带有上传文件的 JSON 对象..
{
"firstName":"abc",
"phoneNumber":"898989",
"DateOfBirth":"asas",
"age":"asas",
"gender":"asas",
"refferedBy":"asas",
"reportfile":"asas",
"reportType":"asas",
"reportDate":"asas",
"purpose":"asas",
"followUpDate":"asas",
"analytes":null
}
回答by abaghel
You can use @RequestPart
like below. This will support both json object and multipart file.
你可以@RequestPart
像下面这样使用。这将支持 json 对象和多部分文件。
@ResponseBody
public ResponseEntity<String>
saveReport(@RequestPart (value="reportFile") MultipartFile reportFile,
@RequestPart LabPatientInfo reportData) throws IOException {
In order to test it using curlyou 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 reportFileis "report.txt". Now you can send request using curl like below.
为了使用curl对其进行测试,您可以为 json 部分创建一个文件 ( reportData)。例如,假设您创建了“mydata.json”文件并将您的 json 负载粘贴到其中。并说您的reportFile是“report.txt”。现在您可以使用 curl 发送请求,如下所示。
curl -v -H "Content-Type:multipart/form-data" -F "[email protected];type=application/json" -F "[email protected];type=text/plain" http://localhost:8080/MyApp/lab/saveReport
回答by eruiz
Spring Roo 2.0.0.M3 includes support for automatic scaffolding of a REST API.
Spring Roo 2.0.0.M3 包括对 REST API 自动脚手架的支持。
For complete information, see the REST APIin the reference manual.
有关完整信息,请参阅参考手册中的REST API。
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.
请注意,M3 版本生成的工件可能会在较新版本中发生变化,因此如果您使用 RC1 或更高版本打开它,您的项目可能不会自动升级。
May the Force be with you.
愿原力与你同在。
回答by WGSSAMINTHA
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.
当参数用@RequestPart 注释时,该部分的内容将通过 HttpMessageConverter 传递以解决方法参数,并记住请求部分的“Content-Type”。这类似于@RequestBody 根据常规请求的内容解析参数所做的工作。
so, we can parse @Requestbody as @RequestPart as "abaghel" and reportData need to be a json file.
因此,我们可以将@Requestbody 解析为@RequestPart 作为“abaghel”,并且reportData 需要是一个json 文件。