简体   繁体   中英

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

[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!

Solved

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM