简体   繁体   中英

swagger io consume json on post

Using swagger editor I created a post call to consume a json object that I want to simply load into a db but when I run the call I get an empty json object.

This is the parameters portion of my json swagger for the post

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

It then creates a "Body" model, but I am not able to see the json that was part of the post:

 @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    ");
  }
}

If I remove the text/json from consume and generated my code again and I still see an issue with the body model and being able to pull in json. If you look at the toString method it shows hard coded values, so I dont see how I can pull the json from the post with the post method only taking in the Body and securitycontext.

Before you send your data you should check HTTP ACCEPT method which you set in swagger when you send your data.

There should be Several Accept method which their behaviour distinct from each other when sending data to server.

Thus for application/JSON : Data are part of body.

form-data and x-www-form-urlencoded : Data are part of header.

I haven't adaquate Java experience to give correct code to obtain json into related jSON Object but How to convert HTTP Request Body into JSON Object in Java this answer could help.

Please Check following RFCs for further information

form-data related RFC https://www.rfc-editor.org/rfc/rfc7578

x-www-form-urlencoded related RFC https://datatracker.ietf.org/doc/html/draft-hoehrmann-urlencoded-01

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

UPDATED

Related curl command: I get the command from swagger live demo http://petstore.swagger.io/#/pet paste your json to it and change url, secret key give it a try!

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'

I am a little confused about the http accept, when using this swagger snippet:

            "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
        }

The method generated is the following:

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

So based on all this I am not sure how to pull in the post body info, normally I would grab what I need from the http request but with swagger I cant seem to figure out how that gets used.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM