0

我正在尝试创建预订,但java.time.LocalDate出现如下错误。

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"
}

这是我的控制器:

@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);     
    }

这是我的 ReservationRequest 类:

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;
    }
}

最后是我要配置的 ApiConfig 类:

@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(); 
    }
}

我已经尝试了有关此主题的所有解决方案,但仍然出现错误。我错过了什么?

任何帮助都会非常有用。

4

0 回答 0