英文:
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: "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?
答案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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论