I'm new in Kotlin language and I try a simple RESTFUL API with Kotlin and Spring Boot. My query methods are not returning the expected result. I was hoping to receive this upon return of the requisition:
[
{
"id": 0,
"name": "string 1",
"color": "string 1"
},
{
"id": 1,
"name": "string 2",
"color": "string 2"
}
]
But I receive this (HTTP status 200):
[
{},
{}
]
My data base have categories and my service and repository perform OK.This is the code of my controller:
@RestController
@RequestMapping("/category")
class CategoryController (val categoryService: CategoryService) {
@GetMapping
fun findAll(): ResponseEntity<Any> {
try {
return ResponseEntity.ok(categoryService.findAll())
} catch (e: Exception) {
val msg = "Something went wrong: " + e.message
return ResponseEntity(msg, HttpStatus.INTERNAL_SERVER_ERROR)
}
}
}
What I'm doing wrong? I thank you for your help!
ResponseEntity example to return empty response ResponseEntity can be used to return an empty response, you can leave the response body null or you can use noContent() method to mention that there is no body associated. In the following example, We have just returned one ResponseEntity object with noContent option.
Spring Boot Gradle plugin automatically uses the Kotlin version declared via the Kotlin Gradle plugin. You can now take a deeper look at the generated application.
I found the problem. My data class variables was private, i remove the private declaration and now the content is showed at the response body
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With