【问题标题】:Problem reading requestparam in controller在控制器中读取请求参数时出现问题
【发布时间】:2019-11-29 22:56:43
【问题描述】:

请求参数显示在网络中,但第二个参数 (fileId) 在控制器中接收为 null。请求返回 400 bad request。

@PostMapping(path = "/uploadFile")
public ResponseEntity<ServiceResponse> upload(
        @RequestParam(value = "id") Long userId,
        @RequestParam(value = "fileId") Long fileId,
        @RequestPart(value = "file") MultipartFile... file) {

    // 
        return ResponseEntity.ok(serviceResponse);
}

请求URL:(文件在body中发送:formData) http://localhost:8080/api/upload?id=9999&fileId=101

【问题讨论】:

  • 您是否还在表单数据中发送fileId 密钥?即file 参数是否包含fileId 键?因为您的 fileId RequestParam 不应该是 null 与您提供的请求,除非 fileId 键也是 MultipartFile 的一部分。
  • @Chaimae Aklibous 在 Spring-MVC 中可能会发生一些原因,您的控制器必须在 Model 类中有 fileID 才能知道它可以使用。

标签: java spring spring-boot spring-mvc


【解决方案1】:

好吧,您能至少显示您的请求或 URL 吗?

【讨论】:

  • 我现在确实添加了!
  • 当您将方法映射到api/uploadFile 时,为什么您的请求使用api/upload
  • api 在项目中定义。实际上,当 fileId 的 required 设置为 false 时,一切正常。它接收 id 的值,但不接收 fileId。
【解决方案2】:

您的网址应该是 http://localhost:8080/api/uploadFile?id=9999&amp;fileId=101 而不是http://localhost:8080/api/upload?id=9999&amp;fileId=101 因为你提到了上传文件的路径

【讨论】:

    猜你喜欢
    • 2018-07-30
    • 2021-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-20
    • 2016-02-21
    • 1970-01-01
    • 2013-03-04
    相关资源
    最近更新 更多