0

I have a spring rest api (actually apache wicket rest api on top of spring project), that returns an object with a property which is a joda LocalDate.

Everything is fine except that the joda LocalDate/LocalTime/DateTime... they are returned in this format:

  "createdDate": {
    "year": 2019,
    "dayOfMonth": 15,
    "dayOfWeek": 1,
    "era": 1,
    "weekOfWeekyear": 16,
    "secondOfMinute": 21,
    "millisOfSecond": 455,
    "dayOfYear": 105,
    "millisOfDay": 72861455,
    "yearOfCentury": 19,
    "weekyear": 2019,
    "minuteOfHour": 14,
    "secondOfDay": 72861,
    "yearOfEra": 2019,
    "centuryOfEra": 20,
    "minuteOfDay": 1214,
    "monthOfYear": 4,
    "hourOfDay": 20,
    "zone": {
      "fixed": false,
      "uncachedZone": {
        "cachable": true,
        "fixed": false,
        "id": "Australia/Perth"
      },
      "id": "Australia/Perth"
    },
    "millis": 1555330461455,
    "chronology": {
      "zone": {
        "fixed": false,
        "uncachedZone": {
          "cachable": true,
          "fixed": false,
          "id": "Australia/Perth"
        },
        "id": "Australia/Perth"
      }
    },
    "equalNow": false,
    "afterNow": false,
    "beforeNow": true
  },

I am expecting the value to be in this format yyyy-MM-dd'T'HH:mm:ss.

I tried using JsonFormat annotation to the said field but with no luck.

...
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss")
@Field("createdDateS")
private DateTime createdDate;
...

Thanks!

Borgy Manotoy
  • 1,960
  • 5
  • 27
  • 42

1 Answers1

1
<dependency>
  <groupId>com.fasterxml.jackson.datatype</groupId>
  <artifactId>jackson-datatype-joda</artifactId>
  <version>2.8.7</version>
</dependency>

Add above maven dependency.

Anant Goswami
  • 318
  • 3
  • 14
  • just adding the above dependency will make it return the joda objects to formatted string? – Borgy Manotoy Apr 16 '19 at 02:56
  • @BorgyManotoy Yes, I should, because Jackson registers the Joda module automatically when the JodaModule is in the classpath. – Anant Goswami Apr 16 '19 at 02:58
  • I tried adding/remove this: `@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss")` but still no luck. It is still not displayed as formatted string. – Borgy Manotoy Apr 16 '19 at 03:15
  • you just need to add the dependency in pom, don't remove @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd'T'HH:mm:ss") – Anant Goswami Apr 16 '19 at 03:17
  • I used version `2.9.8` and JsonFormat annotation not removed. Still it is displaying as object instead of formatted string. No need for other configurations for this? – Borgy Manotoy Apr 16 '19 at 03:21