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

OpenAPI Generator生成multipart/form-data客户端MIME类型异常问题

解决OpenAPI Generator生成byte[]类型multipart字段并保留text/csv Content-Type的问题

直接给你修正后的OpenAPI规范配置,以及Gradle插件的必要配置,解决你遇到的问题:

修正后的OpenAPI规范

/api/SomeEndpoint:
  post:
    requestBody:
      content:
        multipart/form-data:
          schema:
            type: object
            properties:
              id:
                type: string
              csv:
                type: string
                format: byte  # 让生成器生成byte[]类型参数
          encoding:
            csv:
              contentType: text/csv  # 强制指定该字段的Content-Type为text/csv
              style: form

Gradle插件配置调整

在你的build.gradle的openApiGenerate任务中添加useByteArrayForBinary配置,确保生成器优先用byte[]处理二进制字段:

openApiGenerate {
    generatorName = "java"
    inputSpec = "$rootDir/src/main/resources/openapi.yaml".toString()
    outputDir = "$buildDir/generated/openapi".toString()
    configOptions = [
        useByteArrayForBinary: "true",
        // 根据你实际使用的HTTP客户端调整library,比如resttemplate、okhttp-gson等
        library: "resttemplate"
    ]
}

关键要点

  1. format: byte:OpenAPI Generator的Java客户端会将这个配置的字段生成为byte[],替代原来的File类型。
  2. encoding中的contentType:必须明确设置为text/csv,否则生成器会默认给byte[]类型字段设置application/json的Content-Type。
  3. useByteArrayForBinary配置:确保生成器严格按照byte格式生成byte[],不会 fallback 到File。

重新生成客户端代码后,检查接口方法的csv参数应为byte[]类型,发送请求时该字段的Content-Type会正确显示为text/csv。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 20:55:04