swagger io在帖子上消耗json

时间:2016-01-25 01:49:37

标签: swagger

使用swagger编辑器我创建了一个post调用来使用我想简单地加载到db中的json对象,但是当我运行调用时,我得到一个空的json对象。

这是我的json swagger的帖子

的参数部分
                "parameters": [
                {
                    "in": "body",
                    "name": "body",
                    "description": "Add question to collection",
                    "required": true,
                    "schema": { "type" : "object", "additionalProperties" : {}
                    }
                }
            ],

然后创建一个“Body”模型,但我无法看到作为帖子一部分的json:

 @javax.annotation.Generated(value = "class io.swagger.codegen.languages.JaxRSServerCodegen", date = "2016-01-22T20:49:03.229Z")
public class Body   {




  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }
    Body body = (Body) o;
    return true;
  }

  @Override
  public int hashCode() {
    return Objects.hash();
  }

  @Override
  public String toString() {
    StringBuilder sb = new StringBuilder();
    sb.append("class Body {\n");

    sb.append("}");
    return sb.toString();
  }

  /**
   * Convert the given object to string with each line indented by 4 spaces
   * (except the first line).
   */
  private String toIndentedString(Object o) {
    if (o == null) {
      return "null";
    }
    return o.toString().replace("\n", "\n    ");
  }
}

如果我从consume中删除text / json并再次生成我的代码,我仍然看到身体模型的问题并且能够拉入json。  如果你看一下toString方法,它会显示硬编码值,所以我不知道如何用post方法从帖子中拉出json只接受Body和securitycontext。

2 个答案:

答案 0 :(得分:0)

在发送数据之前,您应该检查在发送数据时以骄傲的方式设置的HTTP ACCEPT方法。

在向服务器发送数据时,应该有几种Accept方法,它们的行为彼此不同。

因此application/JSON:数据是正文的一部分。

form-datax-www-form-urlencoded:数据是标题的一部分。

我没有足够的Java经验来提供正确的代码来获取json到相关的jSON对象,但How to convert HTTP Request Body into JSON Object in Java这个答案可能有所帮助。

请查看以下RFC以获取更多信息

form-data相关RFC https://tools.ietf.org/html/rfc7578

x-www-form-urlencoded相关RFC https://tools.ietf.org/html/draft-hoehrmann-urlencoded-01

application/JSON相关rfc https://www.ietf.org/rfc/rfc4627.txt

<强>已更新

相关的curl命令:我从swagger live demo http://petstore.swagger.io/#/pet获取命令将json粘贴到它并更改url, secret key尝试一下!

curl -X POST --header 'Content-Type: application/json' --header 'Accept: application/json' -d '{
  "id": 0,
  "category": {
    "id": 0,
    "name": "string"
  },
  "name": "doggie",
  "photoUrls": [
    "string"
  ],
  "tags": [
    {
      "id": 0,
      "name": "string"
    }
  ],
  "status": "available"
}' 'http://petstore.swagger.io/v2/pet?api_key=special-key'

答案 1 :(得分:0)

当使用这个swagger片段时,我对http接受有点困惑:

            "post": {
            "tags": [
                "AskGrey"
            ],
            "summary": "Create new askgrey.com question",
            "operationId": "postAskGrey",
            "consumes": [
                "application/json",
                "text/json"
            ],
            "produces": [
                "application/json"
            ],
            "parameters": [
                {
                    "in": "body",
                    "name": "body",
                    "description": "Add question to collection",
                    "required": true,
                    "schema": { "type" : "object", "additionalProperties" : {}
                    }
                }
            ],
            "responses": {
                "200": {
                    "description": "OK",
                    "schema": {
                        "type": "object"

                    }
                }
            },
            "deprecated": false
        }

生成的方法如下:

      @Override
  @POST
  @Consumes("application/json")
  public Response postAskGrey(Body body,SecurityContext securityContext)
  throws NotFoundException {

所以基于这一切,我不知道如何提取帖子的身体信息,通常我会从http请求中获取我需要的东西,但是我很想知道如何使用它。