在Spring中,我们能否同时使用multipart和@RequestBody?

16

我想创建一个API,可以将参数设置为多部分文件和JSON对象(@RequestBody)。请查看以下片段,在调用此API时遇到HTTP 415 Unsupported Media Type error。如果我删除@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);
}

以下是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;

在调用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
}
4个回答

16
你可以像下面这样使用@RequestPart。这将支持JSON对象和多部分文件。
@ResponseBody
public ResponseEntity<String>
saveReport(@RequestPart (value="reportFile") MultipartFile reportFile,
           @RequestPart LabPatientInfo reportData) throws IOException {
为了测试它,您可以使用curl,创建一个文件来存储您的 JSON 部分(reportData)。例如,您可以创建一个名为“mydata.json”的文件,并在其中粘贴您的 JSON 负载。假设您的reportFile为“report.txt”。现在您可以使用以下命令通过 curl 发送请求。
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

1
嘿,我使用了上述代码,现在出现HTTP状态400-所需请求部分“reportData”不存在。 - Mayur
@mayur 你是如何发送请求的?你可以尝试使用curl。我已经在上面更新了我的答案,附有详细信息。请查看。 - abaghel
1
你需要将 reportData 转储到 file.json 文件中,如果你试图直接发送 dto,则会出现此类错误。 - Nasir Shah

3

一个接收JSON对象和通用文件的POST方法示例:

public ResponseEntity<Resource> postGenerateReport(@RequestPart GenerateReportDTO, generateReportDTO, @RequestPart MultipartFile jxhtmlReport)

对于 PostMan 设置(或 curl 或任何其他 REST 测试工具),您只需添加包含 2 个元素的表单数据请求:

  1. Key:generateReportDTOValue: 含有 .json 扩展名的文件(内容与对象兼容)
  2. Key:jxhtmlReportValue: 任意文件。

祝好。


1
当一个参数被标注为@RequestPart时,该部分的内容会通过HttpMessageConverter传递,以解决方法参数,并考虑请求部分的'Content-Type'。这类似于@RequestBody根据常规请求的内容解决参数的方式。
因此,我们可以将@RequestBody解析为@RequestPart,如"abaghel"和reportData需要是一个json文件。

0

Spring Roo 2.0.0.M3 包含自动脚手架 REST API 的支持。

有关完整信息,请参阅参考手册中的 REST API

请注意,M3 版本生成的工件可能会在新版本中更改,因此如果您使用 RC1 或以上版本打开项目,则可能无法自动升级。

愿原力与你同在。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接