Issue
I have the following class, which I am using as a request payload :
public class SampleRequest {
private String fromDate;
private String toDate;
// Getters and setters removed for brevity.
}
I am trying to use it with this resource below (just trying to print it to screen to see things happen) :
@PostMapping("/getBySignatureOne")
public ResponseEntity<?> getRequestInfo(@Valid @RequestBody SampleRequest signatureOneRequest) {
System.out.println(signatureOneRequest.getToDate);
System.out.println(signatureOneRequest.getFromDate);
}
This is the JSON request I send up :
{
"fromDate":"2019-03-09",
"toDate":"2019-03-10"
}
This is the error I get :
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]]
I'd love to know what's wrong here, I suspect its an issue with constructors, or that I am missing some annotation somewhere, but I am honestly unsure of where I have gone wrong.
Solution
You need a constructor with all parameters:
public SampleRequest(String fromDate, String toDate) {
this.fromDate = fromDate;
this.toDate = toDate;
}
Or using @AllArgsConstructor
or @Data
from lombok.
Answered By - Andronicus
Answer Checked By - Cary Denson (JavaFixing Admin)