【问题标题】:REST API with Kotlin and Spring BootPost return empty objects带有 Kotlin 和 Spring BootPost 的 REST API 返回空对象
【发布时间】:2018-11-08 10:03:18
【问题描述】:

我是 Kotlin 语言的新手,我尝试使用 Kotlin 和 Spring Boot 的简单 RESTFUL API。我的查询方法没有返回预期的结果。我希望在退还申请时收到这个:

[
    {
        "id": 0,
        "name": "string 1",
        "color": "string 1"
    },
    {
        "id": 1,
        "name": "string 2",
        "color": "string 2"     
    }
]

但我收到了这个(HTTP 状态 200):

[
  {},
  {}
]

我的数据库有类别,我的服务和存储库执行正常。这是我的控制器的代码:

@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)
        }

    }

}

我做错了什么?感谢您的帮助!

【问题讨论】:

  • 您确定调用categoryService.findAll() 会返回有效结果吗?
  • 是的,感谢您的宝贵时间,但我发现了问题!

标签: rest spring-boot kotlin


【解决方案1】:

我发现了问题。我的数据类变量是私有的,我删除了私有声明,现在内容显示在响应正文中

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-18
    • 1970-01-01
    • 1970-01-01
    • 2018-10-23
    • 2021-12-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多