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

Spring Boot资源无法接收请求排查及正确Curl请求示例求助

问题分析与解决方案

一、Spring接口参数绑定错误(HttpMediaType相关异常)

你的接口混合使用了@RequestParam和@RequestPart,并且对布尔值、整数这类基本类型参数用了@RequestPart,这是触发HttpMediaTypeNotSupportedException和HttpMediaTypeNotAcceptableException的核心原因:

  • @RequestPart主要用于处理multipart请求中的复杂对象或文件类型,对于普通键值对形式的基本类型参数,应该用@RequestParam来接收。
  • 当用@RequestPart接收基本类型时,Spring会尝试将该part的内容按媒体类型解析,若请求中这些参数未指定正确的Content-Type(默认应为text/plain),就会触发类型不支持的异常。

修正后的接口代码

@PostMapping(
    name = "sign_one_doc",
    produces = MediaType.APPLICATION_OCTET_STREAM_VALUE,
    consumes = MediaType.MULTIPART_FORM_DATA_VALUE
)
public ResponseEntity<InputStreamResource> signDocument(
    @RequestParam("file") MultipartFile data,
    @RequestParam("isTimestamping") boolean isTimestamping,
    @RequestParam("isMakeCheck") boolean isMakeCheck,
    @RequestParam("signMode") int signMode,
    @RequestParam("certClientID") int certClientID,
    @RequestParam("isCertLocal") boolean isCertLocal
) throws IOException, DocSignException, InputStreamReadException {
    RequestData requestData = signCtrl.signFile(
        data.getInputStream(), 
        data.getOriginalFilename(), 
        signMode, 
        certClientID, 
        isTimestamping, 
        isMakeCheck, 
        isCertLocal
    );
    HttpHeaders headers = new HttpHeaders();
    InputStreamResource inputStreamResource = new InputStreamResource(requestData.currentCtxFile());
    headers.setContentLength(requestData.currentCtxFileBA().length);
    headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
    // 建议添加下载文件名头,方便客户端直接识别文件
    headers.setContentDisposition(ContentDisposition.attachment()
        .filename(requestData.currentCtxFile().getName()) // 替换为实际生成的文件名
        .build());
    return new ResponseEntity<>(inputStreamResource, headers, HttpStatus.OK);
}

二、Curl请求报错(EOFException)

你的curl命令缺失了多个必填参数(isMakeCheck、signMode、certClientID、isCertLocal),且手动设置了Content-Type: multipart/form-data——curl会自动处理multipart请求的边界和Content-Type,手动设置会覆盖自动生成的边界,导致请求解析失败。

正确的Curl命令

curl -v --include \
  -F "file=@test.pdf" \
  -F "isTimestamping=false" \
  -F "isMakeCheck=true" \ # 替换为你的实际业务值
  -F "signMode=1" \        # 替换为你的实际业务值
  -F "certClientID=1001" \ # 替换为你的实际业务值
  -F "isCertLocal=true" \  # 替换为你的实际业务值
  -H "Accept: application/octet-stream" \
  http://localhost:8080/sign_one_doc

三、Insomnia请求的注意事项

在Insomnia中调试时:

  • 所有基本类型参数通过Form标签添加,类型选择Text;文件参数选择File类型。
  • 不要手动设置Content-Type为multipart/form-data,Insomnia会自动生成包含正确边界的请求头,手动设置会破坏请求结构。
  • 确认Accept头设置为application/octet-stream,若需要直接下载文件,可开启Insomnia的"Download response"选项。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.11 09:24:42