Quarkus: resteasy-jackson serialize LocalDate to array

Created on 16 Apr 2020  路  5Comments  路  Source: quarkusio/quarkus

Describe the bug
(Describe the problem clearly and concisely.)
below endpoint returns an array like [2020, 4, 16]

  @GET
  @Path("datetime")
  fun datetime(@QueryParam("i") i: LocalDateTime): LocalDate {
    println(i)
    return LocalDate.now()
  }

Expected behavior
(Describe the expected behavior clearly and concisely.)
should return "2020-04-16"

Actual behavior
(Describe the actual behavior clearly and concisely.)
returns [2020, 4, 16]

To Reproduce
Steps to reproduce the behavior:



    1. 2.
  1. 3.

Configuration

# Add your application.properties here, if applicable.

Screenshots
(If applicable, add screenshots to help explain your problem.)

Environment (please complete the following information):

  • Output of uname -a or ver:
  • Output of java -version:
  • GraalVM version (if different from Java):
  • Quarkus version or git rev:
  • Build tool (ie. output of mvnw --version or gradlew --version):

Additional context
(Add any other context about the problem here.)

kinquestion

Most helpful comment

@rainmanhhh you can customize the Jackson ObjectMapper via an ObjectMapperCustomizer, see
https://quarkus.io/guides/rest-json#configuring-json-support

We automatically register the JavaTimeModule so Java 8 DateTime are supported but by default Jackson serialize dates as timestamps. If you want to serialize them as String you need to disable timestamp serialization : mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);

All 5 comments

Hello @rainmanhhh,

I believe this is the default behavior of jackson. Is there a specific reason you are using quarkus-resteasy-jackson?

Becuase I tested it with quarkus-resteasy-jsonb as the following

    @GET
    @Path("test")
    public LocalDate test() {
        return LocalDate.now();
    }

and I retrieved a response

"2020-04-16"

The reason I believe this is the jackson default is because I also used the following dependency

    <dependency>
      <groupId>io.quarkus</groupId>
      <artifactId>quarkus-resteasy-jackson</artifactId>
    </dependency>

and created a object such as;

import com.fasterxml.jackson.annotation.JsonFormat;
import java.time.LocalDate;

public class Fruit {

    public String name;
    public String description;
    @JsonFormat(pattern = "yyyy-MM-dd")
    public LocalDate localDate = LocalDate.now();
}

and was able to get a response with the expected format

{"name":"Apple","description":"Winter fruit","localDate":"2020-04-16"}

I yet was not able to format the LocalDate returned directly from the method as you showed in your example.

is there a way to set a global default date/date-time format for jackson? if so maybe quarkus-resteasy-jackson should set it to avoid too many @JsonFormat

@rainmanhhh you can customize the Jackson ObjectMapper via an ObjectMapperCustomizer, see
https://quarkus.io/guides/rest-json#configuring-json-support

We automatically register the JavaTimeModule so Java 8 DateTime are supported but by default Jackson serialize dates as timestamps. If you want to serialize them as String you need to disable timestamp serialization : mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);

@loicmathieu I tried out customizing the Jackson ObjectMapper as you recommended and it works fine. This is a jackson related configuration for sure.

Ok so I close the issue.

Was this page helpful?
0 / 5 - 0 ratings