首页 > 解决方案 > form-data file upload + array of object

问题描述

I want to push data inside a form to my Spring REST api. The handler method looks as follows:

@PostMapping
public RepresentationModel<?> handleFileUpload(
    @RequestParam(value = "file") MultipartFile file,
    @RequestParam(value = "description", required = false) String description,
    @RequestParam(value = "public") Boolean publicAccessible,
    @RequestParam(value = "access") @Valid List<SomeObject> access,
    Authentication authentication) {

        ....
}

postman

this gave me:

response postman

But I need a way to wrap a complex Object into my array.

This is the only thing I came up with so far. How do I have to shape my request in Postman to satisfy my controller? The problem is the access attribute. Spring complains all the time access parameter is not present or access parameter is not of type List. In Json my request should look like this:

"public": "true",
"description": "Bachelor Dokument",
"access": [
  {
    "someAttribute": "something",
    "someAttribute2": "something"
  },
  {
    "someAttribute": "something2",
    "someAttribute2": "something2"
  }
]

...

/*Content of uarttest.py*/

I need the form-data for uploading files I figured. Or do I do something wrong here? Thanks in advance.

标签: javaspringrestfile-uploadpostman

解决方案


您只需access在 Postman 请求中使用一个具有所需JSON值的变量:

在此处输入图像描述

当然,您需要指定字段的Content-Typeas 。application/jsonaccess


推荐阅读