Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

REST API with Kotlin and Spring BootPost return empty objects

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!

like image 815
Gabriel A. Garcia Avatar asked May 29 '18 17:05

Gabriel A. Garcia


People also ask

How do I return an empty response entity?

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.

Can I use spring boot with Kotlin?

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.


1 Answers

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

like image 131
Gabriel A. Garcia Avatar answered Nov 13 '22 04:11

Gabriel A. Garcia