0

I am trying to create reservation and I am getting a java.time.LocalDate error like below.

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `java.time.LocalDate` (no Creators, like default constructor, exist): no String-argument constructor/factory method to deserialize from String value ('2017-03-01')
 at [Source: (PushbackInputStream); line: 3, column: 17] (through reference chain: com.spring.angular.webproject.model.request.ReservationRequest["checkin"])

JSON :

{
    "id" : 12345,
    "checkin" : "2017-03-01",
    "checkout" : "2017-03-05"
}

Here is my controller :

@RestController
@RequestMapping(ResourceConstants.ROOM_RESERVATION_V1)
public class ReservationResource {
    
    @RequestMapping(path = "", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<ReservationResponse> getAvailableRooms(
            @RequestParam(value = "checkin")
            @DateTimeFormat(iso = DateTimeFormat.ISO.DATE )
            LocalDate checkin,
            @RequestParam(value = "checkout")
            @DateTimeFormat(iso = DateTimeFormat.ISO.DATE )
            LocalDate checkout){
        
        return new ResponseEntity<>(new ReservationResponse(), HttpStatus.OK);
    }
    
    @RequestMapping(path = "", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE,
            consumes = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<ReservationResponse> createReservation(
            @RequestBody
            ReservationRequest reservationRequest){
        
        return new ResponseEntity<>(new ReservationResponse(), HttpStatus.CREATED);     
    }

here is my ReservationRequest class :

public class ReservationRequest {
    
    private Long id;
    @DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
    private LocalDate checkin;
    @DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
    private LocalDate checkout;
    
    
    public ReservationRequest() {
        super();
    }
    public ReservationRequest(Long id, LocalDate checkin, LocalDate checkout) {
        super();
        this.id = id;
        this.checkin = checkin;
        this.checkout = checkout;
    }
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
    public LocalDate getCheckin() {
        return checkin;
    }
    public void setCheckin(LocalDate checkin) {
        this.checkin = checkin;
    }
    public LocalDate getCheckout() {
        return checkout;
    }
    public void setCheckout(LocalDate checkout) {
        this.checkout = checkout;
    }
}

and last here is my ApiConfig class to configuration :

@Configuration
public class ApiConfig {
    
    @Bean 
    public ObjectMapper objectMapper() {
        
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.registerModule(new JavaTimeModule());
        return new ObjectMapper();
        
    }
    
    @Bean
    public ObjectWriter objectWriter (ObjectMapper objectMapper ) {
        return objectMapper.writerWithDefaultPrettyPrinter(); 
    }
}

I have tried all solutions about this topic but I am still getting an error. What am I missing?

Any help will be greatful.

vahdet
  • 6,357
  • 9
  • 51
  • 106
  • Looks quite similar to this: https://stackoverflow.com/q/45863678/4636715 have you checked it? – vahdet Jul 07 '20 at 09:23
  • I think you use the wrong annotation `@DateTimeFormat`. Have you tried `@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")`? – KunLun Jul 07 '20 at 09:26
  • @vahdet yes ı haved check and try but I am still getting this error. – TolgaKucuk Jul 07 '20 at 09:28
  • @KunLun I have just tried your solution but did not work – TolgaKucuk Jul 07 '20 at 09:32
  • What version of Spring Boot you use? I test your code with Spring Boot 2.3.1 without problem, even without `ApiConfig` and `@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)`. A working project is in [Github](https://github.com/yejianfengblue/so62771911/blob/master/src/test/java/com/example/so62771911/So62771911ApplicationTests.java). – yejianfengblue Jul 08 '20 at 03:34

0 Answers0