我有以下类,我将其用作请求负载:
public class SampleRequest {
private String fromDate;
private String toDate;
// Getters and setters removed for brevity.
}
我正在尝试将它与下面的资源一起使用(只是试图将它打印到屏幕上以查看发生的事情):
@PostMapping("/getBySignatureOne")
public ResponseEntity<?> getRequestInfo(@Valid @RequestBody SampleRequest signatureOneRequest) {
System.out.println(signatureOneRequest.getToDate);
System.out.println(signatureOneRequest.getFromDate);
}
这是我发送的 JSON 请求:
{
"fromDate":"2019-03-09",
"toDate":"2019-03-10"
}
这是我得到的错误:
Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot construct instance of `com.test.app.payload.SampleRequest` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('fromDate'); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `com.test.app.payload.SampleRequest` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('fromDate')
at [Source: (PushbackInputStream); line: 1, column: 2]]
我很想知道这里出了什么问题,我怀疑这是构造函数的问题,或者我在某处遗漏了一些注释,但老实说我不确定哪里出了问题。
原文由 Slippy 发布,翻译遵循 CC BY-SA 4.0 许可协议
您需要一个包含所有参数的构造函数:
或者使用来自 lombok 的
@AllArgsConstructor
或@Data
。