0

I am trying to serialize a joda LocalDate to a string.

@JsonProperty("date")
@JsonSerialize(using = LocalDateSerializer.class)
LocalDate date;

However, that is getting three values like [2020, 1, 9] rather than a string "2020-01-09".

The LocateDateSerializer class documents multiple constructors, one with no arguments, and two like

LocalDateSerializer(java.time.format.DateTimeFormatter formatter)

But I cannot figure out how to specify the constructor argument in the annotation so that I can invoke the LocalDateSerializer with a DateTimeFormatter argument.

Does someone know how to do that?

Fred Andrews
  • 648
  • 9
  • 18
  • Did you look at the `setDateFormat` method of the `ObjectMapper` instance? – w08r Jan 10 '20 at 06:22
  • The serializer you're asking for is for the Java 8 Time API. Is this a legacy project or a new one? – chrylis -cautiouslyoptimistic- Jan 10 '20 at 07:00
  • To use serialiser with non-default constructor you need to use `com.fasterxml.jackson.databind.module.SimpleModule`. See some examples: [How to write boolean value...](https://stackoverflow.com/questions/16564639/how-to-write-boolean-value-as-string-in-a-json-array), [How do i use without any annotations...](https://stackoverflow.com/questions/20104435/how-do-i-use-jackson-json-parsing-in-a-spring-project-without-any-annotations),[Convert java object with Element...](https://stackoverflow.com/questions/55217671/convert-java-object-with-org-w3c-dom-element-to-string-and-get-error-when-conver) – Michał Ziober Jan 10 '20 at 10:40

2 Answers2

0

You can specify format with @JsonFormat

@JsonFormat(pattern = "yyyy-MM-dd")
@JsonProperty("date")
@JsonSerialize(using = LocalDateSerializer.class)
LocalDate date;
Vikas
  • 6,868
  • 4
  • 27
  • 41
  • Not quite sure what was different about my environment but the @JsonFormat annotation also did not help. The only way I could get this working was to go back to where the ObjectMapper is used and add a call to mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd")); Then the dates came through as desired. And fortunately there is only one such usage in this project. – Fred Andrews Jan 13 '20 at 22:09
-2

Try to put annotations on getter instead:

@JsonSerialize(using = LocalDateSerializer.class)   
public LocalDate getDate() {
            return date;
        }
Alex Chernyshev
  • 1,719
  • 9
  • 11
  • Putting the annotation on the getter is probably cleaner, but doing that does not change the results. I am pretty sure that I need to specify LocalDateSerializer with a formatter argument, but that is what I cannot figure out how to do. – Fred Andrews Jan 10 '20 at 06:27
  • 2
    Has you tried by self? Jackson annotations on getters can produce different results than on fields: https://github.com/FasterXML/jackson-databind/issues/2345 – Alex Chernyshev Jan 10 '20 at 08:39
  • Yes, I tried putting it on the getter. Same results. – Fred Andrews Jan 10 '20 at 17:34
  • Don't know how you tested, but i just generated fresh spring boot project from https://start.spring.io/ then added simple code: @RestController public class TestController { @GetMapping(value = "/test") public TestDTO test() { return new TestDTO(); } public static class TestDTO { private LocalDate date = LocalDate.now(); @JsonSerialize(using = LocalDateSerializer.class) public LocalDate getDate() { return date; } } } and got expected: {"date":"2020-01-10"} – Alex Chernyshev Jan 10 '20 at 19:39