Dropwizard - java.time.LocalDateTime is parsed as Array

1,839 views
Skip to first unread message

Heinrich Wilhelm

unread,
Jul 11, 2018, 3:44:33 AM7/11/18
to dropwizard-user
Hi,

I use Dropwizard to build a RESTful Service for my MySQL Database.
Now I have the problem, that the LocalDateTime gets an Array in the JSON in response.

I have a class User.java with the attribute java.time.LocalDateTime lastLogin;
The Response-JSON looks like: ..."lastLogin":[2018,1,1,14,0]...

Why I dont get the milliseconds or the fomart like "yyyy-MM-dd HH:mm:ss"?

Where can I change this?

Thanks
Heiwil

Heinrich Wilhelm

unread,
Jul 11, 2018, 4:18:37 AM7/11/18
to dropwizard-user
Solution: I now use java.util.Date instead of java.time.LocalDateTime. This works fine.

Yantes

unread,
Jul 11, 2018, 4:30:39 AM7/11/18
to dropwizard-user
If you are using java 8 then I would recommend not to stick with Date, it is obsolete, go with LocalDateTime and LocalDate etc. See this to get started http://www.baeldung.com/java-8-date-time-intro 

You should create your own Serializer and Deserializer for handling LocalDateTime and LocalDate, the problem AFAIK is that either if your are using Jackson or Gson they they don't know how you want to De-Serialize the dates, therefore you have to tell them with a DateTimeFormatter something like fx.

DateTimeFormatter FORMATTER = DateTimeFormatter
.ofPattern("uuuu-MM-dd'T'HH:mm:ss.SSSX")
.withZone(ZoneOffset.UTC);

And then use that in your serializer

Hope this helps a bit.

Regards
Yantes

Jochen Schalanda

unread,
Jul 11, 2018, 4:32:40 AM7/11/18
to dropwiz...@googlegroups.com
Hi Heinrich,


Why I dont get the milliseconds or the fomart like "yyyy-MM-dd HH:mm:ss"?

Where can I change this?

You can configure the Jackson ObjectMapper accordingly or use the @JsonFormat annotation in your response objects.

Cheers,
Jochen
Reply all
Reply to author
Forward
0 new messages