kotlin数据类HttpMessageNotReadableException
我正在尝试这个邮递员的代码,但没有任何作品,为什么?
我发送的是:
{ "name":"front_msel", "gitlabId": "83", "fichierVersion":"VERSION" }
我的弹簧控制器:
@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?)
我加倍检查参数,这不是一个措辞错误,这是什么东西不起作用?
编辑:
感谢托德,问题是通过添加空值到我的参数,以生成一个零参数的构造函数。 谢谢 !
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" }