我正在使用Spring Boot创建一个Web应用程序。端点之一期望json对象具有一个属性,即studentId
。我正在像其他功能一样使用DTO来捕获有效负载。
@PostMapping("/courses/{id}/students")
public SuccessResponse<Void> addEnrolls(@PathVariable Long id, @RequestBody StudentIdPayload payload) throws HandledException {
courseService.addEnrolls(id, payload.getStudentId());
return success(HttpStatus.OK);
}
@Data
@AllArgsConstructor
public class StudentIdPayload {
private Long studentId;
}
但是,当我尝试使用json body发布终结点时{"studentId":1}
,出现以下错误:
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `org.bimoadityar.univms.dto.input.StudentIdPayload` (although at least one Creator exists): cannot deserialize from Object value (no delegate- or property-based Creator)
如果我仅使用值发布,则可以使用1
。
如何使它与对象有效负载一起使用?
有趣的是,当我向中添加另一个属性StudentIdPayload
(例如)时String placeholder
,它可以按预期工作,尽管此解决方案让人觉得有些棘手。
默认情况下,反序列化需要no-args构造函数,因此添加@NoArgsConstructor
:
@Data
@AllArgsConstructor
@NoArgsConstructor
public class StudentIdPayload {
private Long studentId;
}
也可以看看:
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句