You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot响应式上传Azure Blob遇Postman 400错误:缺失file参数

Spring Boot响应式上传接口Postman报400:Required query parameter 'file' is not present.

问题描述

使用Spring Boot响应式开发CRUD功能,采用Azure Blob Container存储数据,读取功能正常,但上传接口通过Postman测试时始终返回400错误:

400 BAD_REQUEST "Required query parameter 'file' is not present."

最初怀疑是代码问题,将接口返回类型改为void并清空请求体后,错误依旧,因此推测问题出在Postman配置或Spring的请求解析逻辑中。

已尝试操作

  • 启用“Read files outside working directory”选项
  • 在form-data区域添加“file”键
  • 移除form-data区域的“file”键
  • 添加默认值为multipart/form-data; boundary=的“Content-Type”请求头
  • 移除“Content-Type”请求头
  • 移除原有“Content-Type”请求头并手动设置

以上操作均无效,点击发送后立即返回相同400错误。

相关代码

Service实现类

@Override
public Mono<Void> uploadBlob(MultipartFile file) throws IOException {
    String blobName = file.getOriginalFilename();

    if (blobName == null || blobName.isEmpty()) {
        return Mono.error(new IllegalArgumentException("Invalid file name"));
    }

    logger.info("Uploading blob: {}", blobName);
    return blobContainerAsyncClient.getBlobAsyncClient(blobName)
            .uploadFromFile(file.getResource().getFile().getPath(), true) // true para sobrescribir si ya existe
            .then();
}

Controller类

@PostMapping("/upload")
public Mono<ResponseEntity<Void>> uploadBlob(@RequestParam("file") MultipartFile file) throws IOException {
    return azureBlobService.uploadBlob(file)
            .thenReturn(ResponseEntity.ok().<Void>build());
}

解决思路

  1. 确认Postman请求参数配置

    • 确保请求为POST方法,参数放在Body标签下的form-data中,键名file的类型选择File而非Text,并正确选择本地文件。错误提示提到“query parameter”,说明Spring误将file识别为URL参数,需确认Postman未在Params标签下添加file参数。
  2. 修正Spring参数注解
    WebFlux环境下,处理multipart文件更推荐使用@RequestPart替代@RequestParam,后者更适用于URL参数或简单表单字段:

    @PostMapping("/upload")
    public Mono<ResponseEntity<Void>> uploadBlob(@RequestPart("file") MultipartFile file) throws IOException {
        return azureBlobService.uploadBlob(file)
                .thenReturn(ResponseEntity.ok().<Void>build());
    }
    
  3. 检查Multipart配置与依赖

    • 确保项目仅引入spring-boot-starter-webflux,避免同时引入spring-boot-starter-web(Servlet依赖会干扰WebFlux的请求解析)。
    • 在application.yml中添加Multipart配置:
      spring:
        servlet:
          multipart:
            enabled: true
            max-file-size: 10MB
            max-request-size: 10MB
      
  4. 避免手动设置Content-Type
    Postman在选择form-data并添加文件后,会自动生成带正确boundary的Content-Type请求头,手动设置会覆盖该值,导致Spring无法解析请求体,建议完全移除手动添加的Content-Type头。

  5. 替换Service层文件上传逻辑
    file.getResource().getFile()在WebFlux环境下可能无法获取到有效文件路径(文件可能存储在内存中),改用InputStream上传更稳妥:

    return blobContainerAsyncClient.getBlobAsyncClient(blobName)
            .upload(file.getInputStream(), file.getSize(), true)
            .then();
    
  6. 用curl排除Postman问题
    执行以下curl命令测试接口,若能成功则说明是Postman配置问题,可尝试重置Postman设置或重新创建请求:

    curl -X POST -F "file=@/本地文件路径/文件名" http://localhost:8080/upload
    

内容的提问来源于stack exchange,提问作者Fabricio

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 01:21:15