【问题标题】:swagger-ui doesn't display the response model for a 400swagger-ui 不显示 400 的响应模型
【发布时间】:2017-01-24 20:40:02
【问题描述】:

这是我生成的 swagger 文档的摘录。

    "responses": {
      "200": {
        "description": "successful operation",
        "schema": {
          "$ref": "#/definitions/ActionJsValue"
        }
      },
      "400": {
        "description": "list of validation errors",
        "schema": {
          "type": "array",
          "items": {
            "$ref": "#/definitions/Error"
          }
        }
      }
    }

在定义部分:

"Error": {
  "type": "object",
  "properties": {
    "path": {
      "type": "string"
    },
    "messages": {
      "type": "array",
      "items": {
        "type": "string"
      }
    }
  }
},

而且 swagger-ui 不显示响应模型。

有人遇到过这个问题吗? 有谁知道如何解决这个问题?

【问题讨论】:

    标签: swagger swagger-ui


    【解决方案1】:

    我遇到了完全相同的问题并找到了下一个解决方法。

    方法说明:

    "responses": {
      "200": {
        "description": "successful operation",
        "schema": {
          "$ref": "#/definitions/ActionJsValue"
        }
      },
      "400": {
        "description": "list of validation errors",
        "schema": {
          "$ref": "#/definitions/ErrorsArray"
        }
      }
    }
    

    定义部分:

    "ErrorsArray": {
      "type": "array",
      "items": {
        "type": "object",
        "properties": {
          "path": {
            "type": "string"
          },
          "messages": {
            "type": "array",
            "items": {
              "type": "string"
            }
          }
        }
      }
    }
    

    所以,只需将您的数组定义为单独的类型。

    【讨论】:

      【解决方案2】:

      无论您用于生成 swagger doc 的 url,您需要在 swagger-ui index.html 页面中使用的相同 url。 EX:如果您使用 "http://{servername}:{port}/{App-Context}/{url-pattern}/swagger.json" 生成 swagger doc,那么在 index.html 中也必须使用下面。

      <script type="text/javascript">
      $(function () {
        var url = window.location.search.match(/url=([^&]+)/);
        if (url && url.length > 1) {
          url = decodeURIComponent(url[1]);
        } else {
          url = "http://petstore.swagger.io/v2/swagger.json";// you need to change this url to your's url http://{servername}:{port}/{App-Context}/{url-pattern}/swagger.json
        }
      

      如果对您有帮助,请告诉我,谢谢。

      【讨论】:

      • swagger-ui 有效。我的问题是为什么响应模型列是空的(cf screenshot),我期待看到Error
      • 确保在你的rest资源类和模型类的swagger注解中使用@ApiOperation(value = "...", notes = "...", response = ResponseObj.class ) @ApiModel(value = "LeaflicenseInfoResponse") 公共类 ResponseObj { 私有字符串代码; @ApiModelProperty(value = "code", notes = "... }
      • 对于生成的文档的提取,注释似乎很好
      • 您能从生成的文档的摘录中看到定义吗?如下图 "definitions": { "ResponseObj": { "type": "object", "properties": { "code": { "type": "string", "description": "code" }},
      • 我已经用定义部分编辑了我的问题。对我来说似乎很好。
      猜你喜欢
      • 1970-01-01
      • 2021-03-28
      • 1970-01-01
      • 2016-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-10
      相关资源
      最近更新 更多