Search code examples
jacksonmicronautgraalvmgraalvm-native-image

Native image doesn't respect date format with @JsonFormat


With the following field configuration
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
When I run the project with java the result of printing the date is:
{"name":"test","date":"2021-11-20 23:12:12"}

However with native image:
mvn -T 1C package -Dpackaging=native-image -Pgraalvm
execute the binary:
./target/jackson-string-error
Sending post:
curl -X POST --location "http://localhost:8080/main" -H "accept: application/json" -H "Content-Type: application/json" -d "{\"name\":\"test\",\"date\":\"2021-11-20 23:12:12\"}"
the result is:
{"name":"test","date":[2021,11,20,23,12,12]}

For some reason the date is not parsed.

Sample: https://github.com/j1cs/jackson-string-error
OS: Macos Big Sur
Micronaut version: 3.1.4
Graalvm version: 21.2.0.r11-grl
Issue github: https://github.com/micronaut-projects/micronaut-core/issues/6546

Update: I made mi own serializer and still doesn't work.
I added one log to see if it goes through that class.
When I run the app with java it print the log fine:

02:31:46.585 [main] INFO  io.micronaut.runtime.Micronaut - Startup completed in 1171ms. Server Running: http://localhost:8080
02:31:50.729 [default-nioEventLoopGroup-1-2] INFO  me.jics.CustomDateSerializer - the formatter: yyyy-MM-dd HH:mm:ss
02:31:50.730 [default-nioEventLoopGroup-1-2] INFO  me.jics.MainController - {"date":"2021-11-20 23:12:12","name":"test"}

But with the native image the log "the formatter" doesn't print:

02:30:23.601 [main] INFO  io.micronaut.runtime.Micronaut - Startup completed in 29ms. Server Running: http://localhost:8080
02:30:28.561 [default-nioEventLoopGroup-1-2] INFO  me.jics.MainController - {"name":"test","date":[2021,11,20,23,12,12]}

Solution

  • Solved by the micronaut framework team: https://github.com/micronaut-projects/micronaut-core/issues/6546