【问题标题】:form-data file upload + array of object表单数据文件上传 + 对象数组
【发布时间】:2021-01-21 09:09:09
【问题描述】:

我想将表单内的数据推送到我的 Spring REST api。处理程序方法如下所示:

@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 中塑造我的请求以满足我的控制器?问题是访问属性。 Spring 一直抱怨访问参数不存在或访问参数不是 List 类型。 在 Json 中,我的请求应如下所示:

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

...

/*Content of uarttest.py*/

我需要表单数据来上传我想的文件。还是我在这里做错了什么? 提前致谢。

【问题讨论】:

    标签: java spring rest file-upload postman


    【解决方案1】:

    您只需在 Postman 请求中使用一个 access 变量,并将所需的 JSON 作为值:

    当然,你需要将Content-Type指定为access字段的application/json

    【讨论】:

    • 感谢您的回答,这对我不起作用。我得到以下信息:i.stack.imgur.com/qkT4J.png 我认为邮递员将字段的有效负载作为字符串转义。
    • 删除请求映射中 List 之前的有效注释。 List 不是 JavBean,所以 Valid 在那里没用。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-09-26
    • 2021-02-26
    • 1970-01-01
    • 1970-01-01
    • 2017-04-18
    • 1970-01-01
    • 2013-07-04
    相关资源
    最近更新 更多