2

[Spring + Kotlin]

These are the dependencies:

implementation("org.springframework.boot:spring-boot-starter-data-jpa")
implementation("org.springframework.boot:spring-boot-starter-data-rest")
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.springframework.boot:spring-boot-starter-web-services")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.4.0")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")

This is the Entity:

@Entity
class MatchEntity(
        @Id @GeneratedValue val id: Long,
        @NotBlank val matchDateTime: Date,
        @NotBlank @ManyToOne @JoinColumn val tournamentInvolved: TournamentEntity
)

Whenever I try to run the following query:

interface MatchRepository : JpaRepository<MatchEntity, Long> {
    fun findMatchesByMatchDateTimeIsAfter(matchDateTime: Date)
}

with a test string like so 1985-04-12T23:20, I get the error:

QueryMethodParameterConversionException: Failed to convert 1985-04-12T23:20 into java.util.Date!

I tried, as suggested here, with patterns like @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) and @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm") in the signature of the query method, without solving.

Also, as suggested here, I tried adding

  1. compile("com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.4.0") to the dependencies

  2. spring.jackson.serialization.write_dates_as_timestamps=false to the application.properties.

Didn't work.

UPDATE: I also tried with LocalDateTime and Instant classes. Still getting the same Exceptions:

QueryMethodParameterConversionException: Failed to convert 1985-04-12T23:20 into java.time.LocalDateTime!

QueryMethodParameterConversionException: Failed to convert 1985-04-12T23:20 into java.time.Instant!

ciurlaro
  • 742
  • 10
  • 22
  • 1
    I recommend you don’t use `Date`. That class is poorly designed and long outdated. Instead use `LocalDateTime` or `Instant`; both classes are from [java.time, the modern Java date and time API](https://docs.oracle.com/javase/tutorial/datetime/). – Ole V.V. Oct 03 '19 at 07:26
  • 1
    I just tried, i got these messages: `QueryMethodParameterConversionException: Failed to convert 1985-04-12T23:20 into java.time.LocalDateTime!` `QueryMethodParameterConversionException: Failed to convert 1985-04-12T23:20 into java.time.Instant!` – ciurlaro Oct 03 '19 at 08:18
  • Try adding seconds to the date you are submitting. (ISO-8601 the default includes seconds). – M. Deinum Oct 03 '19 at 08:48
  • 1
    Turned out the problem was the @DateTimeFormat pattern – ciurlaro Oct 03 '19 at 09:19

1 Answers1

3

Solved

Using @DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm") worked.

ciurlaro
  • 742
  • 10
  • 22