Im trying to make a website using Spring + Angular JS and my current problem is i used POSTMAN to post a JSON statement as below:
{
"id": 12345,
"checkin" : "2017-03-01",
"checkout" : "2017-03-05"
}
and then this error pops up: enter image description here
"Type definition error: [simple type, class java.time.LocalDate]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of java.time.LocalDate
(no Creators, like default construct, exist): no String-argument constructor/factory method to deserialize from String value ('2017-03-01')"
Resource Code:
@RequestMapping(path = "", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE,
consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
public ResponseEntity<ReservationResponse> createReservation(
@RequestBody
ReservationRequest reservationRequest){
return new ResponseEntity<>(new ReservationResponse(), HttpStatus.CREATED);
}
Model Code:
public class ReservationRequest {
private Long id;
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
private LocalDate checkin;
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
private LocalDate checkout;}
APIConfig Code:
@Bean
@Primary
public ObjectMapper objectMapper() {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.registerModules(new JavaTimeModule());
return new ObjectMapper();
}
Application Properties:
spring.jackson.serialization.write-dates-as-timestamps=false
Build Gradle:
compile group: 'com.fasterxml.jackson.datatype', name: 'jackson-datatype-jsr310', version: '2.8.7'
We also faced the same issue and after defining a converter it worked fine. You can add below code in the application and it should work fine. You can change the date format as per your need.
I was working through the same tutorial and encountered the same problem. What made my code work was to change the return value of the
objectMapper()
method in API Config Code fromnew ObjectMapper()
toobjectMapper
.