OpenApi – 使用 X-WWW-FORM-URLEncoded 作为 requestBody。

huangapple go评论65阅读模式
英文:

OpenApi -API with X-WWW-FORM-URLEncoded as requestBody

问题

I am using openapi 3.0.3 and I am trying to write an API which accepts data in the form of 'application/x-www-form-urlencoded' by api specification looks something like this:

/p路径/到/api/v1/testApi:
post:
tags:
- 提取数据
operationId: "testApi"
requestBody:
content:
application/x-www-form-urlencoded:
schema:
type: object
properties:
effectiveDate:
type: string
required: true

using the above specification when an API is generated automatically, the request is accepted by the API as @RequestParam.

ResponseEntity<String> testAPI(
@Parameter(name = "effectiveDate", description = "") @Valid @RequestParam(value = "effectiveDate", required = false) String effectiveDate
) {
return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
}

Is there a possibility to make it accept @RequestBody instead of @RequestParam with the same x-www-form-urlencoded data?

英文:

I am using openapi 3.0.3 and I am trying to write an API which accepts data in the form of
'application/x-www-form-urlencoded' by api specification looks something like this:

/path/to/api/v1/testApi:
    post:
      tags:
        - Extract data
      operationId: &quot;testApi&quot;   
    requestBody:
     content:
       application/x-www-form-urlencoded:
         schema:
          type: object
          properties:
            effectiveDate:
              type: string
     required: true

using the above specification when an api is generated automatically, the request is accepted by the api as @RequestParam.

ResponseEntity&lt;String&gt; testAPi(
        @Parameter(name = &quot;effectiveDate&quot;, description = &quot;&quot;) @Valid @RequestParam(value = &quot;effectiveDate&quot;, required = false) String effectiveDate
    ) {
        return new ResponseEntity&lt;&gt;(HttpStatus.NOT_IMPLEMENTED);

    }

Is there a possibility to make it accept @RequestBody instead of @RequestParam with the same x-www-form-urlencoded data?

答案1

得分: 2

不可能强制一个使用application/x-www-form-urlencoded内容的open-api POST请求体生成一个Java Controller方法,该方法接受@RequestBody而不是@RequestParam。

原因是Servlet API要求ServletRequest.getParameter()方法仅支持HTTP POST的表单字段访问。- 来自spring-framework/docs

另请参阅ServletRequest.html#getParameter(java.lang.String)

英文:

It is not possible to force an open-api POST requestBody with application/x-www-form-urlencoded content to generate a Java Controller method that accepts a @RequestBody instead of @RequestParam.

The reason is that the The Servlet API requires ServletRequest.getParameter() methods to support form field access only for HTTP POST.- from spring-framework/docs

Also ServletRequest.html#getParameter(java.lang.String)

huangapple
  • 本文由 发表于 2023年4月10日 23:00:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75978209.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定