首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >OpenAPI规范说“application/x form-urlencoded”,但是生成的spring引导需要多部分/表单数据或多部分/混合流。

OpenAPI规范说“application/x form-urlencoded”,但是生成的spring引导需要多部分/表单数据或多部分/混合流。
EN

Stack Overflow用户
提问于 2022-04-21 10:39:05
回答 1查看 931关注 0票数 2

我试图使用现有OpenAPI演示程序中的configOptions规范在Java中创建类似的演示,并使用了带有openapi生成器-maven-插件的"spring“生成器和以下configOptions:

代码语言:javascript
运行
复制
                                <!-- generates single interface to implement -->
                                <useTags>true</useTags>

                                <withXml>true</withXml>
                                <title>${project.artifactId}</title>

                                <performBeanValidation>true</performBeanValidation>
                                <useBeanValidation>true</useBeanValidation>
                                <useOptional>true</useOptional>
                                <useSpringController>false</useSpringController>

                                <returnSuccessCode>false</returnSuccessCode>

                                <useAbstractionForFiles>true</useAbstractionForFiles>

                                <!-- Our own delegates can be autowired into the generated sources -->
                                <delegatePattern>true</delegatePattern>

这个YAML片段是:

代码语言:javascript
运行
复制
post:
  description: Add products to shopping cart.
  operationId: api.cart.add_product

  requestBody:
    required: true
    content:
      'application/x-www-form-urlencoded':
        schema:
          type: object
          properties:
            quantity:
              type: integer
              example: 1
            product_code:
              type: string
              example: elephant
          required:
            - quantity
            - product_code
        examples:
          add_an_elephant:
            summary: How to add a single elephant to the shopping cart
            value:
              product_code: elephant
              quantity: 1
  
  responses:
    '204':
      description: Product added to cart.
    '400':
      description: Cannot add product to cart.
    '422':
      description: Unknown product code.

用一点额外的胶水让Spring自动编译和启动请求变量。现在我的问题是,swagger页面生成一个application/x-www-form-urlencoded请求,就像OpenAPI文件中所说的那样,但是Tomcat在尝试调用时失败了:

代码语言:javascript
运行
复制
    @RequestMapping(
        method = RequestMethod.POST,
        value = "/cart/add",
        consumes = { "application/x-www-form-urlencoded" }
    )
    default ResponseEntity<Void> apiCartAddProduct(
        @Parameter(name = "quantity", description = "", required = true, schema = @Schema(description = "")) @Valid @RequestPart(value = "quantity", required = true) Integer quantity,
        @Parameter(name = "product_code", description = "", required = true, schema = @Schema(description = "")) @Valid @RequestPart(value = "product_code", required = true) String productCode
    ) {
        return getDelegate().apiCartAddProduct(quantity, productCode);
    }

然后调用

代码语言:javascript
运行
复制
    @Override
    public ResponseEntity<Void> apiCartAddProduct(Integer quantity, String productCode) {
        var product = products.stream().filter(p -> p.getCode().equals(productCode)).findFirst().get();
        CartEntry price = new CartEntry().product(product).price(product.getPrice()).quantity(quantity).price(product.getPrice() * quantity);
        cart.add(price);
        return new ResponseEntity<Void>((Void) null, HttpStatus.OK);
    }

除此外:

代码语言:javascript
运行
复制
org.apache.tomcat.util.http.fileupload.impl.InvalidContentTypeException: the request doesn't contain a multipart/form-data or multipart/mixed stream, content type header is application/x-www-form-urlencoded
    at org.apache.tomcat.util.http.fileupload.impl.FileItemIteratorImpl.init(FileItemIteratorImpl.java:151) ~[tomcat-embed-core-9.0.56.jar:9.0.56]
    at org.apache.tomcat.util.http.fileupload.impl.FileItemIteratorImpl.getMultiPartStream(FileItemIteratorImpl.java:205) ~[tomcat-embed-core-9.0.56.jar:9.0.56]

我的理解是,OpenAPI生成器应该生成能够接受YAML文件中声明的类型的代码,但是可能有很多原因不这样做。

我的问题是,我不明白为什么这首先是一个问题,是什么原因。有什么问题,我该怎么解决呢?如果有可能避免更改openapi文件,我更愿意这样做。

编辑:从实验上看,Tomcat所看到的将@RequestPart转换为@RequestParamapiCartAddProduct定义使其工作。随着自动生成代码的变化,我正在寻找一个更好的解决方案。有什么建议吗?

EN

回答 1

Stack Overflow用户

发布于 2022-04-21 12:31:06

这似乎是在https://github.com/OpenAPITools/openapi-generator/commit/a0eb149df5b722bfd43cf3587399c118850af76c (Version4.3)中故意破坏的,它的报告数倍于https://github.com/OpenAPITools/openapi-generator/issues/7794,然后在https://github.com/OpenAPITools/openapi-generator/commit/33b89148e562fc2d5acf60a56719c46ec4f631e8 (2022-02-27)中修复。

这意味着我使用的5.4.0版本不是固定的,而是6.0.0-beta版本。

升级到测试版解决了问题。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71953089

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档