0

In Json return the field date time:

"date": "2023-09-22 01:07:50.062477"

In class Json:

@JsonDeserialize(using = LocalDateTimeDeserializer.class)
@JsonSerialize(using = LocalDateTimeSerializer.class)
public LocalDateTime date;

Return error:

Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of java.time.LocalDateTime (no Creators, like default construct, exist): no String-argument constructor/factory method to deserialize from String value ('2023-09-22 02:11:13.855618')

How to solve?

@JsonDeserialize(using = LocalDateTimeDeserializer.class)
@JsonSerialize(using = LocalDateTimeSerializer.class)
public LocalDateTime date;
1
  • The java.time classes use the ISO 8601 standard formats when parsing/generating text. That means using a T in the middle rather than the SPACE seen in your text: 2023-09-22T02:11:13.855618. I guess you either need to correct your inputs to comply with ISO 8601 or you need to configure Jackson with a formatter to parse your particular inputs. en.wikipedia.org/wiki/ISO_8601 Commented Sep 22, 2023 at 2:57

2 Answers 2

1

you are getting that error since Json is trying to deserialize the date sting into instance of LocalDateTime but it not being able to do that due no constructor or such method on that class.

You can get to solution by creating a custom deserializer class by extending JsonDeserializer and override the deserializer method and return to required by Parsing.

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;

public class CustomLocalDateTimeDeserializer extends JsonDeserializer<LocalDateTime> {
    private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSSSSS");

    @Override
    public LocalDateTime deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
        String dateStr = jsonParser.getValueAsString();
        return LocalDateTime.parse(dateStr, formatter);
    }
}

Then annotate the field with custom deserializer instead.

@JsonDeserialize(using = CustomLocalDateTimeDeserializer.class)
@JsonSerialize(using = LocalDateTimeSerializer.class)
public LocalDateTime date;

I am sure that you have this dependency.

implementation("com.fasterxml.jackson.datatype:jackson-datatype-jsr310")

Hope this works for you. I did have get some shortcut just before 3-4 months. Sorry I forgot that.

Sign up to request clarification or add additional context in comments.

Comments

0

Am no sure how you interact from front end, passing json data from the front end or when you call the end point just put this to the @PathVariable or @RequestParam like this in controller class:

@RequestMapping(method = RequestMethod.GET, path = "/v{version}", produces = "application/json")
ResponseEntity<List<Test>> findByDate(@RequestParam(required = false) @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime date)}

Use this standard ISO method.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.