Home > other >  Java - @ DateTimeFormat couldn't identify
Java - @ DateTimeFormat couldn't identify

Time:11-26

I tried to use the @ DateTimeFormat comments LocalDateTime object why don't you admit?
My main idea is that once in the controller receives a string, it will transform it into LocalDateTime object

Now I got:

{
"Timestamp" : 1493708443198,
"Status" : 400,
"Error" : "Bad Request,"
"The exception" : "org. Springframework. HTTP. Converter. HttpMessageNotReadableException",
"Message" : "Could not read the JSON document: Can not construct the instance of the Java. Time. LocalDateTime: no String - argument constructor/factory method to deserialize the from String value (' 2015-09-26 T01:30:00. 000 ') \ n at [Source: Java. IO. PushbackInputStream @ 3233297 a; line: 5, the column: 23] (through the reference chain: net. Petrikainulainen. Spring. Trenches. Model. The Topic [" LocalDateTime \] "); Nested exception is com. Fasterxml. Jackson. Databind. JsonMappingException: Can not construct the instance of the Java. Time. LocalDateTime: no String - argument constructor/factory method to deserialize the from String value (' 2015-09-26 T01:30:00. 000 ') \ n at [Source: Java. IO. PushbackInputStream @ 3233297 a; line: 5, the column: 23] (through the reference chain: net. Petrikainulainen. Spring. Trenches. Model. The Topic [\ "LocalDateTime \]"), "
"Path" : "/API/switchable viewer"
}

When trying to publish

{
"Id" : "javaw2,"
"Name" : "the Java code,"
"Descript2ion" : "Java description,"
"LocalDateTime" : "the 2015-09-26 T01:30:00. 000"
}

This is my controller:

@ RequestMapping (method=RequestMethod. POST, value="https://bbs.csdn.net/topics")
Public void addTopic (@ RequestBody Topic Topic) {
TopicService. AddTopic (topic);
}

CodePudding user response:



Can not construct the instance of the Java. Time. LocalDateTime: no String - argument constructor/factory method to deserialize the from String value (' 2015-09-26 T01:30:00. 000 ')

This error indicates that LocalDateTime classes without String parameter constructor/factory methods, so you must write your own deserializer to Date String representation deserialization for LocalDateTime Object.
Like:

@ JsonDeserialize (using=MyDateDeserializer. Class)
Private LocalDateTime LocalDateTime;

Then MyDateDeserializer implementation

Public class MyDateDeserializer extends JsonDeserializer@ Override
Public LocalDateTime deserialize (JsonParser jp, DeserializationContext CTXT) throws the Exception {

DateTimeFormatter formatter.=DateTimeFormatter ofPattern (" your pattern ");

String date=jp. GetValueAsString ();

LocalDateTime LocalDateTime=LocalDateTime. Parse (the date, the formatter);
Return localDateTime;
}
}

CodePudding user response:

SSM framework:
Use the Date instead of LocalDateTime and in combination with @ JsonFormat (pattern="MM - dd yyyy - HH: MM: ss"), can receive the time string of @ RequestBody,
And transition to Date type MM - dd yyyy - HH: MM: ss time format,
SpringBoot framework:
Can be used normally LocalDateTime.
  • Related