gpt4 book ai didi

json - kotlin 数据类 HttpMessageNotReadableException

转载 作者:行者123 更新时间:2023-12-02 13:24:18 24 4
gpt4 key购买 nike

我正在用 postman 尝试这段代码,但没有任何效果,为什么?

我寄什么:

{
"name":"front_msel",
"gitlabId": "83",
"fichierVersion":"VERSION"
}

我的 Spring Controller :
@RestController
@RequestMapping("/projects")
class ProjectController(val projectRepository: ProjectRepository) {
private val log = LoggerFactory.getLogger(ProjectController::class.java)

@PostMapping()
fun saveProject(@RequestBody payload: Project): String {
log.info("project: '{}'", payload.toString())
return projectRepository.save(payload).gitlabId?:"-1"
}

}

我得到什么:
{
"timestamp": 1505917417221,
"status": 400,
"error": "Bad Request",
"exception": "org.springframework.http.converter.HttpMessageNotReadableException",
"message": "JSON parse error: Can not construct instance of com.......model.Project: no suitable constructor found, can not deserialize from Object value (missing default constructor or creator, or perhaps need to add/enable type information?); nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of com.......model.Project: no suitable constructor found, can not deserialize from Object value (missing default constructor or creator, or perhaps need to add/enable type information?)\n at [Source: java.io.PushbackInputStream@66ba61d9; line: 2, column: 2]",
"path": "/projects"

}

我的项目数据类:
data class Project(val name:String?, val gitlabId: String?, val fichierVersion: String?)

我加倍检查参数,这不是一个措辞错误,这东西不起作用是什么?

编辑:

感谢 Todd,通过向我的参数添加 null 值来生成零参数构造函数来解决问题。谢谢 !
data class Project(val name:String? = null, val gitlabId: String? = null, val fichierVersion: String? = null)

最佳答案

我通过做了两个细微的改变来实现这一点。

首先,因为你所有的Project字段可以为空,提供默认值以便编译器生成一个零参数构造函数:

data class Project(val name:String? = null, val gitlabId: String? = null, val fichierVersion: String? = null)

其次,Spring 似乎希望您的字段位于 snakey_case 中。而不是 camelCase ,因此将您的有效负载更改为:
{
"name":"front_msel",
"gitlab_id": "83",
"fichier_version":"VERSION"
}

关于json - kotlin 数据类 HttpMessageNotReadableException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46324776/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com