在我的游戏框架应用程序中,我已经在路由文件中注册了以下API:
POST /api/rmt-create-request controllers.Api.CreateRMTRequestForm
在控制器的操作上,我正在使用以下代码访问以form提交为的形式提交的formData:
public Result CreateRMTRequestForm()
{
Map<String, String[]> params = request().body().asMultipartFormData().asFormUrlEncoded();
当我通过forntend提交表单时,它可以很好地用作API。
我正在尝试使用swagger.ui创建API文档,其中我在swagger.json文件中编写了以下JSON数据。
"paths": {"/api/rmt-create-request": {
"post": {
"tags": [
"RMT APIs"
],
"description" : "Return newly created request data",
"operationId": "create-new-rmt-request",
"consumes": ["application/x-www-form-urlencoded"],
"parameters": [
{
"name": "rootNodeName",
"in": "formData",
"description": "Root node class name for item",
"schema": {
"type": "string"
}
}
],
"responses": {
"200": {
"description": "OK",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/rmt-request-data"
}
}
}
},
"default": {
"$ref": "#/components/responses/default"
}
}
}
},
在检查RequestHeader数据时,未附加其值为'multipart / form-data'的content-Type属性以及formData均未附加,这导致控制器抛出空异常。
任何人都可以帮助swagger.json文件中缺少什么吗?
答案 0 :(得分:1)
您正在混合使用OpenAPI 2.0和3.0语法。
在OpenAPI 3.0中,使用requestBody
关键字而不是in: formData
参数来定义请求正文(包括表单数据)。
此外,OAS3不使用consumes
。该操作消耗的媒体类型在requestBody
中指定。
"paths": {
"/api/rmt-create-request": {
"post": {
"tags": [
"RMT APIs"
],
"description": "Return newly created request data",
"operationId": "create-new-rmt-request",
"requestBody": {
"content": {
"multipart/form-data": { // or "application/x-www-form-urlencoded" - depending on what you need
"schema": {
"type": "object",
"properties": {
"rootNodeName": {
"type": "string",
"description": "Root node class name for item"
}
}
}
}
}
}
}
}
}