0

I would like to format my LocalDateTime on Javalin.

I know the library uses Jackson to generate JSON and I also know how to configure it but where can I set the Jackson configuration?

I would like to format my dates in this format, "dd-MM-yyyy hh:mm:ss".

Thank you

Ryan Godlonton-Shaw
  • 584
  • 1
  • 5
  • 18
Jose Luiz Junior
  • 145
  • 1
  • 11
  • 3
    This is a terrible idea. Use a standard ISO format, and format the date the way you want to in the presentation layer. – JB Nizet Mar 25 '19 at 12:57
  • I try to mark my fields with "@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "dd-MM-yyyy hh:mm:ss")" but not work, what I need to do? – Jose Luiz Junior Mar 25 '19 at 13:20
  • 4
    Again, don't do anything like that. Keep using the standard, ISO format. Format dates in the presentation layer, not in the JSON. – JB Nizet Mar 25 '19 at 13:23
  • Ok, I will create a DTO with String field to set my LocalDateTime formated, thank you :), because the project is a test and I need to format output date to send – Jose Luiz Junior Mar 25 '19 at 13:32
  • 1
    So, you'll do exactly what I advised not to do. And in an even worse way: now not only your JSON will be awful, but you will also corrupt your model. – JB Nizet Mar 25 '19 at 13:35
  • I understand you about advice, but the project is a test to job and I need to format date to return, because this I'm asking, to try do the less worst way – Jose Luiz Junior Mar 25 '19 at 13:38
  • I not setted this in my model, I did a DTO to return in my endpoint, is that a bad idea anyway? – Jose Luiz Junior Mar 25 '19 at 13:46
  • Creating a DTO is not a bad idea. Using a field of type String when it should be of type LocalDateTime, and formatting in a non-standard, ambiguous format is. – JB Nizet Mar 25 '19 at 13:47
  • What could I do in this DTO to solve my formatting problem? – Jose Luiz Junior Mar 25 '19 at 13:52

3 Answers3

2

In Javalin, Jackson is configurable via JavalinJackson#configure ().

In my specific case, I would do

JavalinJackson.configure(jacksonObjectMapper().findAndRegisterModules())

and then add the appropriate dependency:

compile "com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.9.8"
Alexander Leithner
  • 3,169
  • 22
  • 33
Jose Luiz Junior
  • 145
  • 1
  • 11
  • Configure method from JavalinJackson is now deprecated. How to do this with Javalin version >= 4.0.0? – lubrum Oct 21 '21 at 20:34
  • Well, it seems that we need to pass a object (that implement the JsonMapper interface from Javalin) to config.jsonMapper() method. This information can be found here https://javalin.io/documentation#faq – lubrum Oct 22 '21 at 15:07
1

You can format your LocalDateTime like this:

LocalDateTime.now().format(DateTimeFormatter.ofPattern("dd-MM-yyyy hh:mm:ss"))
0

If you're fine with using Gson (it handles LocalDateTime well):

Gson gson = new GsonBuilder().create();
JsonMapper gsonMapper = new JsonMapper() {
    @Override
    public String toJsonString(@NotNull Object obj) {
        return gson.toJson(obj);
    }
    @Override
    public <T> T fromJsonString(@NotNull String json, @NotNull Class<T> targetClass) {
        return gson.fromJson(json, targetClass);
    }
};
Javalin app = Javalin.create(config -> config.jsonMapper(gsonMapper)).start(7070);

Above code snippet is from the official documentation: javalin.io/documentation#faq

Art
  • 1,302
  • 13
  • 25