You need to use the body parameter:

    "parameters": [
      {
        "in": "body",
        "name": "body",
        "description": "Pet object that needs to be added to the store",
        "required": false,
        "schema": {
          "$ref": "#/definitions/Pet"
        }
      }
    ],

and #/definitions/Pet is defined as a model:

"Pet": {
  "required": [
    "name",
    "photoUrls"
  ],
  "properties": {
    "id": {
      "type": "integer",
      "format": "int64"
    },
    "category": {
      "$ref": "#/definitions/Category"
    },
    "name": {
      "type": "string",
      "example": "doggie"
    },
    "photoUrls": {
      "type": "array",
      "xml": {
        "name": "photoUrl",
        "wrapped": true
      },
      "items": {
        "type": "string"
      }
    },
    "tags": {
      "type": "array",
      "xml": {
        "name": "tag",
        "wrapped": true
      },
      "items": {
        "$ref": "#/definitions/Tag"
      }
    },
    "status": {
      "type": "string",
      "description": "pet status in the store",
      "enum": [
        "available",
        "pending",
        "sold"
      ]
    }
  },
  "xml": {
    "name": "Pet"
  }
},

Ref: https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/test/resources/2_0/petstore.json#L35-L43

OpenAPI/Swagger v2 spec: https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md#parameter-object

For OpenAPI v3 spec, body parameter has been deprecated. To define the HTTP payload, one needs to use the requestBody instead, e.g. https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/test/resources/3_0/petstore.json#L39-L41

OpenAPI v3 spec: https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md#requestBodyObject

Answer from William Cheng on Stack Overflow
🌐
Swagger
swagger.io › docs › specification › v3_0 › describing-request-body › describing-request-body
Describing Request Body | Swagger Docs
The requestBody is more flexible in that it lets you consume different media types, such as JSON, XML, form data, plain text, and others, and use different schemas for different media types. requestBody consists of the content object, an optional Markdown-formatted description, and an optional required flag (false by default). content lists the media types consumed by the operation (such as application/json) and specifies the schema for each media type. Request bodies are optional by default. To mark the body as required, use required: true. ... content allows wildcard media types. For example, image/* represents all image types; */* represents all types and is functionally equivalent to application/octet-stream.
Discussions

How to describe this POST JSON request body in OpenAPI (Swagger)? - Stack Overflow
I have a POST request that uses the following JSON request body. How can I describe this request body using OpenAPI (Swagger)? More on stackoverflow.com
🌐 stackoverflow.com
Request body with multiple object in parameters in JSON format
Q&A (please complete the following information) OS: Windows10 Browser: Firefox Version: 66 Method of installation: dist assets Swagger-UI version: 3.10 Swagger/OpenAPI version: Swagger 2.0 Cont... More on github.com
🌐 github.com
5
May 10, 2019
java - Swagger example post body - how to show JSON Body- Swagger-annotations - Stack Overflow
You must define a model if you want to show json schema in swagger-ui I believe. ... It's an old question but since I haven't found a solution online here how I to customized the example value in the swagger documentation produce automatically by the java annotations. I use swagger 2.0 and springfox.version 2.10.5. The Idea is documenting the class of the request ... More on stackoverflow.com
🌐 stackoverflow.com
No Response when consuming a swagger.json with body object parameter | OutSystems
No Response when consuming a swagger.json with body object parameter More on outsystems.com
🌐 outsystems.com
🌐
Swagger-autogen
swagger-autogen.github.io › openapi 3.x › request body
Request Body | Swagger Autogen
Example #1 · app.post('/path', (req, res) => { ... /* #swagger.requestBody = { required: true, content: { "application/json": { schema: { $ref: "#/components/schemas/someSchema" } } } } */ ... }); Example #2 (without declaring the content) ...
Top answer
1 of 3
51

I made it work with:

    post:
      consumes:
        - application/json
      produces:
        - application/json
        - text/xml
        - text/html
      parameters:
        - name: body
          in: body
          required: true
          schema:
            # Body schema with atomic property examples
            type: object
            properties:
              testapi:
                type: object
                properties:
                  messageId:
                    type: string
                    example: kkkk8
                  messageDateTime:
                    type: string
                    example: '2014-08-17T14:07:30+0530'
              testapiBody:
                type: object
                properties:
                  cameraServiceRq:
                    type: object
                    properties:
                      osType:
                        type: string
                        example: android
                      deviceType:
                        type: string
                        example: samsung555
            # Alternatively, we can use a schema-level example
            example:
              testapi:
                testapiContext:
                  messageId: kkkk8
                  messageDateTime: '2014-08-17T14:07:30+0530'
                testapiBody:
                  cameraServiceRq:
                    osType: android
                    deviceType: samsung555
2 of 3
10

openapi version >= 3.0.0 allows for the use of a requestBody which would allow for request body definitions outside of parameters.

In your case it would look something like this:

...
      requestBody:
        description: The pet JSON you want to post
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                testapi:
                  type: object
                  properties:
                    messageId:
                      type: string
                      example: kkkk8
                    messageDateTime:
                      type: string
                      example: '2014-08-17T14:07:30+0530'
                testapiBody:
                  type: object
                  properties:
                    cameraServiceRq:
                      type: object
                      properties:
                        osType:
                          type: string
                          example: android
                        deviceType:
                          type: string
                          example: samsung555
              example:
                testapi:
                  testapiContext:
                    messageId: kkkk8
                    messageDateTime: '2014-08-17T14:07:30+0530'
                  testapiBody:
                    cameraServiceRq:
                      osType: android
                      deviceType: samsung555
...
🌐
Swagger
swagger.io › docs › specification › v3_0 › adding-examples
Adding Examples | Swagger Docs
If a sample value cannot be inserted ... nor JSON-conformant, you can use the externalValue keyword to specify the URL of the example value. The URL should point to the resource that contains the literal example contents (an object, file or image, for example): ... You can define common examples in the components/examples section of your specification and then re-use them in various parameter descriptions, request and response body descriptions, ...
🌐
Google Groups
groups.google.com › g › swagger-swaggersocket › c › YxovC7Uoikg › m › -Ud6uPjCAwAJ
Swagger UI for REST Request body example
test(@RequestBody(required = true, content = @Content(mediaType = APPLICATION_JSON,schema = @Schema(implementation = Foo.class, example ="fooex1" ) ,examples ={@ExampleObject(name="fooExternalEx1",externalValue="http://localhost:8080/receiver/fooExternalEx1.json")})) Foo f) { ... As per Open ...
🌐
GitHub
github.com › swagger-api › swagger-editor › issues › 1992
Request body with multiple object in parameters in JSON format · Issue #1992 · swagger-api/swagger-editor
May 10, 2019 - https://petstore.swagger.io/?_ga=2.210242835.708059440.1555410030-810955978.1555410030#/ How can I pass multiple objects like in above screenshot User Object, Task Object, Some primitive data types parameters in same request body in the form of JSON.
Published   May 10, 2019
Author   vaibhavn
Find elsewhere
🌐
Medium
inside.caratlane.com › post-api-with-request-body-as-json-in-swagger-876fe626048c
POST API with request body as JSON in Swagger | by CaratLane Insider | CaratLane Insider
March 13, 2023 - So param type should be set as body within swagger documentation for each parameter. ... Here the parameter content type is now set as application/json and parameters are added in JSON format. Now the request is executed successfully.
Top answer
1 of 4
4

It's an old question but since I haven't found a solution online here how I to customized the example value in the swagger documentation produce automatically by the java annotations. I use swagger 2.0 and springfox.version 2.10.5.

The Idea is documenting the class of the request parameter that has the @RequestBody annotation. for example my method is

    @ApiOperation(
              value = "Start ListBuilder extraction",
              response = ExtractionLogEntity.class,
              produces = "application/json"
            )
    @PostMapping("/extraction/start")
    public ExtractionLogEntity startTask(
        @RequestBody(required = true) ExtractionRequest request,

In order to expose request json object example I added a @ApiModelProperty(example = "...") annotation to the properties of ExtractionRequest .

   @ApiModelProperty(example = "[{ 'field':'value'}]")
    @NotNull
    private  List<ListBuilderFieldEntity> fields;

   @ApiModelProperty(example = "1000")

    private  String ied;
   @ApiModelProperty(example = "US")
    private  String codebase;

And that's the result

2 of 4
3

If changing from String to a concrete object is not okay (although that's what I would recommend you to do since it's cleaner), you can try using @ApiImplicitParams (check out their documentation)

@ApiOperation(notes = "example" value = "/example", consumes = ".." , method= "..")
@ApiImplicitParams({
        @ApiImplicitParam(name = "Object", value = "Object to be created", required = true, dataType = "your.package.BodyClass", paramType = "body")
})
@RequestMapping(name = "xxx" value ="/hello" ..)
@ApiResponses(..)
    public @ResponseBody String getXXX (@Apiparam(name="JSONrequest", required = true) @RequestBody String JSONrequest){

}

(not sure if you still need the @Apiparam(name="JSONrequest", required = true) bit from the method parameter)

🌐
GitHub
gist.github.com › biggates › 4955d608379a8b1b3224e815c7dd0dc9
Example of OpenAPI Specification v3.0.0, containing multiple examples of requestBody · GitHub
value: - id: 1 category: id: 1 name: cat name: fluffy photoUrls: - http://example.com/path/to/cat/1.jpg - http://example.com/path/to/cat/2.jpg tags: - id: 1 name: cat status: available - id: 2 category: id: 2 name: dog name: puppy photoUrls: - http://example.com/path/to/dog/1.jpg tags: - id: 2 name: dog status: available '400': description: Invalid status value security: - petstore_auth: - write:pets - read:pets "/pet/findByStatus/singleExample": get: tags: - pet summary: Finds Pets by status description: Multiple status values can be provided with comma separated strings operationId: findPets
🌐
GitHub
github.com › zircote › swagger-php › issues › 1168
Cannot describe POST requests with JSON body · Issue #1168 · zircote/swagger-php
March 16, 2022 - Hi there, In my old project I used zircote/swagger-php in version 2.0.16 It was working fine. ... /** * @Route("/login", methods={"POST"}) * * @SWG\Tag(name="Authorization") * * @SWG\Parameter( * name="body", * in="body", * description="Login action, return JWT token", * @SWG\Schema (ref="#/definitions/credentials") * ) * * @SWG\Response( * response=200, * description="Returns JWT token", * @SWG\Schema (ref="#/definitions/authentication") * ) * * @SWG\Response( * response=400, * description="Bad Request", * @SWG\Schema (ref="#/definitions/error") * ) * * @SWG\Response( * response=401, * description="Bad credentials", * @SWG\Schema (ref="#/definitions/error") * ) */
Author   evolic
🌐
ServiceStack
forums.servicestack.net › swagger
Swagger PUT/POST body value issue - Swagger - ServiceStack Customer Forums
October 23, 2017 - However, when we PUT or POST data via the Swagger UI, we see issues where Swagger is missing an extra JSON wrapper around the body parameter data after we click on the Model Schema to autofill the request data. Without that wrapper (see Request JSON Schema examples below), the call fails.
🌐
Stack Overflow
stackoverflow.com › questions › 53266228 › json-request-body-example-swagger
JSON request body example swagger - Stack Overflow
Is there any way to set up example for request body in swagger 2.0? My scheme is the same like in https://petstore.swagger.io/ My json param: "parameters": [ { "in": "body", "name": "body", "description": "Pet object that needs to be added to the store", "required": true, "schema": { "$ref": "#/definitions/Pet" }, "x-example": "x-example": "{\r\n\"id\":123123123123123123,\r\n\"category\":{\r\n\"id\":44,\r\n\"name\":\"A12312312312312AA\"\r\n},\r\n\"name\":\"X123123123123XX\",\r\n\"photoUrls\":[\r\n\"st123123123ring\"\r\n],\r\n\"tags\":[\r\n{\r\n\"id\":0,\r\n\"name\":\"str123123ing\"\r\n}\r\n],\r\n\"status\":\"avai123123lable\"\r\n}"" } ], I tried everything to setup my own example.
🌐
GitHub
github.com › springfox › springfox › issues › 2352
How to customized request body "Example Value"? · Issue #2352 · springfox/springfox
April 10, 2018 - I using springfox.swagger2.version 2.8.1-SNAPSHOT today. @ExampleProperty(mediaType = "application/json", value = "{\n" +""aa": {\n" + ""type": "aa" \n" +" }") })) @NotNull @RequestBody @Valid DesktopSnapshot snapshot)
Author   JavaBingo
🌐
Dynamics Community
community.dynamics.com › blogs › post
How to – Post a JSON body in Swagger
The Azure Function expected the code query parameter basically the apiKey for the function and JSON Body with custom object named Lead having following properties as POST method. ... This is how we’d define our Lead Object and pass it as one of the parameters in the body. swagger: '2.0' info: title: mycrmfunctionapp.azurewebsites.net version: 1.0.0 host: mycrmfunctionapp.azurewebsites.net basePath: / schemes: - https - http paths: /api/MyLeadWebHook: post: operationId: /api/MyLeadWebHook/post produces: - application/json consumes: - application/json parameters: - name: Lead in: body descript
🌐
GitHub
github.com › swagger-api › swagger-core › issues › 391
What's the correct/swagger way to document the request body of a POST method that posts a complex object? · Issue #391 · swagger-api/swagger-core
December 3, 2013 - The demo project only shows GET requests and really doesn't include a good set of sample coverage of the different parameter types, etc. partial snippet: { "path": "/v1/customers", "operations": [ { "method": "POST", "summary": "Creates a new customer", "notes": "Creates a new customer using the given customer object.", "responseClass": "Customer", "nickname": "", "produces": [ "application/json", "application/xml" ], "parameters": [ { "name": "body", "description": "Customer object", "required": true, "allowMultiple": false, "dataType": "Customer", "paramType": "body" } ], "errorResponses": [ { "code": 400, "message": "Bad Request.
Author   mikemil
🌐
Nishantrana
nishantrana.me › 2017 › 04 › 27 › how-to-post-a-json-body-in-swagger
How to – Post a JSON body in Swagger | ...
April 27, 2017 - This is how we’d define our Lead Object and pass it as one of the parameters in the body. swagger: '2.0' info: title: mycrmfunctionapp.azurewebsites.net version: 1.0.0 host: mycrmfunctionapp.azurewebsites.net basePath: / schemes: - https - http paths: /api/MyLeadWebHook: post: operationId: /api/MyLeadWebHook/post produces: - application/json consumes: - application/json parameters: - name: Lead in: body description: lead object required: true schema: $ref: '#/definitions/Lead' description: >- Replace with Operation Object #http://swagger.io/specification/#operationObject responses: '200': de