0

In my spring boot app I have controller method, in kotlin:

@GetMapping("/testDate")
@ResponseBody
fun testDate(): ResponseEntity<Date> {
    return ResponseEntity.ok().body(Date())
}

Date - it is java.util.Date.

This method response is timestamp. For example, 1572869050518. I want to use custom format anywhere in my app, for example 'dd.MM.yyyy'. And I cannot find a way how to configure it.

I use classes with Date fields from other app, and API of other app to get it. So, I can't annotate fields with @JsonFormat or other annotations. I get ready objects and return it in controllers to show it to users.

I read this article, then add spring.jackson.date-format=yyyy-MM-dd HH:mm:ss - no effect in result.

In this SO questions I found that com.fasterxml.jackson.datatype:jackson-datatype-joda must be added. Ok, I add it, no effect in result.

I add spring.jackson.serialization.write-dates-as-timestamps=false, no effect.

And last, I found §3.6 in spring docs, add configuration with FormattingConversionService bean like in example, no effect.

Is there working example how to configure default date formatting in spring mvc app?

UPD1. Does not matter is date a part of another object. If I introduce class data class TestDateObj(var dt: Date), change method to

@GetMapping("/testDate")
@ResponseBody
fun testDate(): ResponseEntity<TestDateObj> {
    return ResponseEntity.ok().body(TestDateObj(dt = Date()))
}

I get {"dt":1572878129672} in result.

Alex T
  • 2,067
  • 4
  • 20
  • 27

0 Answers0