1

I have the filter class bellow with 3 attributes to search for available books. I have two search cases: search through a list of book codes or search by date.

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class BookFilter {

    Set<Long> bookCodes;
    LocalDate fromDate;
    LocalDate toDate;

}

This is my Controller

    @GetMapping("/books")
    public ResponseEntity<Page<BookView>> findAvailableBooks(BookFilter bookFilter, Pageable pageable) {

        Page<Book> page = service.findAvailableBooks(bookFilter);
        Page<BookView> map = page.map(book-> modelMapper.map(book, BookView.class));

        return ResponseEntity.status(HttpStatus.OK).body(map);
    }

And I'm trying to send request in Postman like:

http://localhost:8887/v1/api/library/books?fromDate=01-01-2019&toDate=01-31-2019

But I'm stuck in typeMismatch error: Failed to convert from type String to LocalDate. What am I missing?

Liam Park
  • 414
  • 1
  • 9
  • 26

4 Answers4

2
    /* 1. Get as String */
    @GetMapping("/books")
    ResponseEntity<Page<BookView>> findAvailableBooks(@RequestParam(value = "fromDate", required = false) String fromDate, @RequestParam(value = "toDate", required = false) String toDate, Pageable pageable) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");

        LocalDate fromLocalDate = LocalDate.parse(fromDate, formatter);
        LocalDate toLocalDate = LocalDate.parse(toDate, formatter);

        // other code
    }

    /* 2. Using @DateTimeFormat  */
    @GetMapping("/books")
    ResponseEntity<Page<BookView>> findAvailableBooks(@RequestParam(value = "fromDate", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime fromDate, @RequestParam(value = "toDate", required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)  LocalDateTime toDate, Pageable pageable) {
        // other code
    }
  1. Send LocalDate as a String with @RequestParam and then convert it to the desired date format

  2. Use @DateTimeFormat (iso = DateTimeFormat.ISO.DATE_TIME) with the desired format in the date parameter

Also, a very similar question was asked before on this topic here:

How to use LocalDateTime RequestParam in Spring? I get "Failed to convert String to LocalDateTime"

S.Daineko
  • 1,790
  • 1
  • 20
  • 29
1

Add @JsonFormat(pattern = "dd-MM-yyyy") before LocalDate So your BookFilter class will look like this

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class BookFilter {

    Set<Long> bookCodes;
    @JsonFormat(pattern = "dd-MM-yyyy")
    LocalDate fromDate;
    @JsonFormat(pattern = "dd-MM-yyyy")
    LocalDate toDate;

}
  • 1
    Thanks for your answer. I tried how you said but still sutck in typeMismatch error: Failed to convert from type String to LocalDate – Liam Park May 14 '20 at 15:20
  • could you please share your stack trace. Also please let me know what kind of application you are using? I tested this in spring boot applications and it worked for me. – Raja Bhowmick May 15 '20 at 07:04
1

You can use custom serializer/deserializer for string to LocalDate conversion.

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "MM-dd-yyyy")
@JsonDeserialize(using = LocalDateDeserializer.class)
@JsonSerialize(using = LocalDateSerializer.class)
private LocalDate toDate;

after adding the below dependencies

<dependency>
   <groupId>com.fasterxml.jackson.core</groupId>
   <artifactId>jackson-databind</artifactId>
   <version>2.9.7</version>
</dependency>
<dependency>
   <groupId>com.fasterxml.jackson.datatype</groupId>
   <artifactId>jackson-datatype-jsr310</artifactId>
   <version>2.9.7</version>
</dependency>
Sudheendra
  • 29
  • 5
1

You need to add @JsonFormat annotation to your LocalDate members

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@ToString
public class BookFilter {

    Set<Long> bookCodes;
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy")
    LocalDate fromDate;
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy")
    LocalDate toDate;
}

See this question: Spring Data JPA - ZonedDateTime format for json serialization

Michael Gantman
  • 7,315
  • 2
  • 19
  • 36
  • Thanks for your answer. I tried how you said but still sutck in typeMismatch error: Failed to convert from type String to LocalDate. I used the same URL that is in my question – Liam Park May 14 '20 at 15:50