JSON parse error: Cannot construct instance of `java.sql.Time` : cannot deserialize from Object value

Viewed 578

I have an API call which takes java.sql.Time object from request body. Following is the structure of request body:

{
  "age": 0,
  "height":0,
  "startMealTime": {
    "date": 0,
    "hours": 0,
    "minutes": 0,
    "month": 0,
    "seconds": 0,
    "time": 0,
    "year": 0
  }
}

Following is the POJO:

public class DietScheduleRequest {
    @NotBlank
    private Double height;
    @NotBlank
    private int age;
    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss.SSS")
    @NotBlank
    private Time startMealTime;
}

My pom.xml contains fasterxml.jackson dependency:

        <dependency>
            <groupId>com.fasterxml.jackson.datatype</groupId>
            <artifactId>jackson-datatype-jsr310</artifactId>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.module</groupId>
            <artifactId>jackson-module-parameter-names</artifactId>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.datatype</groupId>
            <artifactId>jackson-datatype-jdk8</artifactId>
        </dependency>

The error I get when executing the request is:

WARN 31832 --- [nio-8080-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : 
Resolved [org.springframework.http.converter.HttpMessageNotReadableException: 
JSON parse error: Cannot construct instance of `java.sql.Time` (although at least one Creator exists): 
cannot deserialize from Object value (no delegate- or property-based Creator); 
nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: 
Cannot construct instance of `java.sql.Time` (although at least one Creator exists): 
cannot deserialize from Object value (no delegate- or property-based Creator)<EOL> at 
[Source: (org.springframework.util.StreamUtils$NonClosingInputStream); line: 1, column: 58] 
(through reference chain: com.webapp.ExpenseTracker.payload.DietScheduleRequest["startMealTime"])]

I looked for similar problems and tried solutions mentioned which were to add

@JsonFormat(pattern="yyyy-MM-dd HH:mm:ss.SSS")

or

@JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")

But in my case, the problem persists.

0 Answers
Related