I want to display the response result in the following format:
[
"author":"Author",
"status_code":"200"
"message":"GET",
"description":"Description..."
"data":{
"id":"123",
"name":""My Name",
... : ...
}
]
But I only display each data result as follows:
[
{
"id":"123",
"name":""My Name",
... : ...
}
]
This is the code snippet that displays the response result
@GetMapping(value = "categorie")
fun getAllFoodCategories(@Param(value = "key") key:String): ResponseEntity<List<Category>> {
if (key==AppUtils.APY_KEY){
var foodCategories: List<Category> = foodCategoryRespository.findAll()
return if(!foodCategories.isEmpty()){
foodCategories.forEach { v ->
run {
logger.info(v.toString())
}
}
ResponseEntity(foodCategories, HttpStatus.OK)
}else{
ResponseEntity(HttpStatus.NO_CONTENT)
}
}else{
return ResponseEntity(HttpStatus.UNAUTHORIZED)
}
}
@Entity(name = "categories")
data class Category(
@Id
@Column(name = "id")
val id: Int,
@get: NotBlank
@Column(name = "name")
val name: String
)
Can someone help me with the answer, Thanks you.
You can't return different elements as a list, but you can return as an object, which contains a list of category. Like this:
{
"author":"Author",
"status_code":"200"
"message":"GET",
"description":"Description..."
"data":[
{
"id":"123",
"name":""My Name",
... : ...
}
]
}
For this, create a model like:
data class MyResponse(
val author: String,
val statusCode: String,
val message: String,
val description: String,
val data: List<Category>
)
Then return with:
fun getAllFoodCategories(@Param(value = "key") key:String): ResponseEntity<MyResponse> {...}