Question

I am using swagger for documenting my Play-Scala application.

My controller is as below:

@Api(value = "/people")
class PersonController {
  import JsonFormats._

  @ApiOperation(response = ?????, responseContainer = "Array", httpMethod = "GET")
  def showPeople(...) = Action {
    // Code here..to get persons of type List[Person]

    val json: JsObject = Json.obj("people" -> persons)
    Ok(json)
  }
}

My JsonFormats.scala is as below:

object JsonFormats {
  implicit val personFormat = new Format[Person] {
    def writes(person: Person): JsValue = {
      Json.obj(
        "fName" -> person.name.fName,
        "lName" -> person.name.lName,
        "age" -> person.age)

  }
}

My model classes are as below

case class Person(name: Name, age: String) {
  gender: String
}

case class Name {
  fName: String,
  lName: String,
}

Question: What to specify at @ApiOperation's response param in my controller, so it displays the json response structure as expected format (as below), in swagger-ui.

{ 
    "people":[
                {
                    "fName":"",
                    "lName":"",
                    "age":""
                }
              ]
}
Was it helpful?

Solution

This a sample to do that :

   @ApiOperation(
    value = "Get a content type by its name",
    notes = "Returns a content type",
    response = classOf[models.ContentType],
    httpMethod = "GET"
  )
  @ApiResponses(Array(
    new ApiResponse(code = 404, message = "Content Type not found")
  ))

In this case, models.ContentType is case class.

Cheers

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top