Swagger Java Spring Data Mock API

0

Making a data mock for an API I have found that having the example in the Swagger editor, after executing the project spring generated by Swagger I find that the input of the endpoint is not acceptable. It is a Json and the example autogenerates it as a string, so you must use escape characters to include the quotes. But then the service hits it as it comes from there, I explain:

This is the swagger with the tags example

swagger: '2.0'
info:
  description: Test
  version: 0.0.1
  title: Test
tags:
- name: Test
  description: ''
schemes:
- http
paths:
/testOperation:
   get:
     tags:
     - Test
     summary: Test
     description: Test
     operationId: testOperation
     produces:
       - application/json
     parameters:
       - name: testInput
         in: body
         schema:
           $ref: '#/definitions/testInput'
         required: true
     responses:
      '200':
        description: Successful operation
        schema:
          $ref: '#/definitions/testOutput'
      '405':
        description: Error operation
definitions:
  testInput:
    type: object
    properties:
      mainId:
        type: integer
        example: 0001
      additional:
        type: array
        items:
          type: object
          properties:
            id:
              type: integer
              example: 1
            property1:
              type: string
              example: property1
            property2:
              type: string
              example: property2
        example:
          - id: 1
            property1: property1
            property2: property2
          - id: 2
            property1: property11
            property2: property22
  testOutput:
    type: object
    properties:
       id:
         type: integer
         example: 1

And there's the Java line that handles the example the service

   @ApiModelProperty(example = "
[{\"id\":1,\"property1\":\"property1\",\"property2\":\"property2\"},
{\"id\":2,\"property1\":\"property11\",\"property2\":\"property22\"}]", 
value = "")

When I upload it and click on the input example I get this

And when I execute it I get an error because it does not read the escape characters correctly, my question is:

How can I write the string in Java so that it receives the Swagger example? I would have to return something like that when I clicked on the input example

{
  "mainId": 1,
  "additional": [
    {
      "id": 1,
      "property1": "property1",
      "property2": "property2"
    },
    {
      "id": 2,
      "property1": "property11",
      "property2": "property22"
    }
  ]
}

Thanks in advance.

    
asked by supra 06.10.2017 в 16:40
source

2 answers

0

Try this way:

@ApiModelProperty(example="[{'id':1,'property1':'property1','property2':'property2'},{'id':2,'property1':'property11','property2':'property22'}]",value="")
    
answered by 08.10.2017 в 07:48
0

Your POJO should be as follows

@ApiModel(value = "persona")
public class Persona {

    @ApiModelProperty(name = "id", example = "1")
    private Integer id;

    @ApiModelProperty(name = "nombre", example = "nombre")
    private String nombre;

    @ApiModelProperty(name = "edad", example = "99")
    private Integer edad;   

}
    
answered by 14.10.2017 в 07:32