0

I have the following date field in my DTO class:

@JsonFormat(pattern="dd.MM.yyyy")
private LocalDateTime date;

I define the date format in my application.yml as shown below:

spring:
  jackson:
    date-format: "dd.MM.yyyy"

I am trying to use this format in my DTO field directly like below:

@JsonFormat(pattern="${spring.jackson.date-format}")
private LocalDateTime date;

Is it possible? Or do ı have to add a @Value field like below? I tried but cannot use this dateFormat in my date field.

@Value("${spring.jackson.date-format}")
private String dateFormat;
Jack
  • 1
  • 21
  • 118
  • 236
  • Take a look at this question: https://stackoverflow.com/questions/65103102/set-external-date-format-pattern-to-jsonformat-from-a-properties-file – Unmitigated Feb 19 '23 at 21:09
  • 1
    I don't know the answer to your question, but when faced with questions like this one, I tend to try it both ways, and see which one works. A lot of software development consists of trying something, testing it out, failing, then trying something else. – Dawood ibn Kareem Feb 19 '23 at 21:11
  • TL;DR: Java annotation do not accept variable values. More detailed - see https://stackoverflow.com/q/10636201/2886891, https://stackoverflow.com/q/62798011/2886891, https://stackoverflow.com/q/54403433/2886891, https://stackoverflow.com/q/14268981/2886891, https://stackoverflow.com/q/12568385/2886891 – Honza Zidek Feb 19 '23 at 21:12
  • @Unmitigated Thanks for help, it seems to suggest to use Value as I tried, but it is also not working. – Jack Feb 19 '23 at 21:12
  • @HonzaZidek So, do you have any suggestion for using date format in DTO? Should I use Value annotation or anything? I also tried to use it but could not make it work. – Jack Feb 19 '23 at 21:14
  • The way to achieve your goal would be probably something like modifying the JSON serializer/deserializer, which *can* be done in the code, so dynamically. – Honza Zidek Feb 19 '23 at 21:14

2 Answers2

2
  1. JsonFormat is not a spring annotation, therefore you can't use spring expression language.
  2. spring.jackson.date-format defines default date format for some types of date classes, you don't have to use this variable inside JsonFormat, just define the value. For details see e.g. https://www.baeldung.com/spring-boot-formatting-json-dates
  3. If you would like to be more flexible in spring you can define converters for any type, see the same article.
Boris
  • 1,054
  • 1
  • 13
  • 23
1

You have good tips here: https://www.baeldung.com/spring-boot-formatting-json-dates

Either configure the default format, like

spring.jackson.date-format=dd.MM.yyyy

or configure the serializer:

@Configuration
public class MyJsonConfig {
    @Value("${spring.jackson.date-format}")
    private String dateFormat;

    @Bean
    public Jackson2ObjectMapperBuilderCustomizer jsonCustomizer() {
        return builder -> {
            builder.simpleDateFormat(dateTimeFormat);
            builder.serializers(new LocalDateSerializer(DateTimeFormatter.ofPattern(dateFormat)));
            builder.serializers(new LocalDateTimeSerializer(DateTimeFormatter.ofPattern(dateFormat)));
        };
    }
}
Honza Zidek
  • 9,204
  • 4
  • 72
  • 118
  • Thanks a lot, but it says "Although setting the default format like this is quite handy and straightforward, there's a drawback to this approach. Unfortunately, it doesn't work with the Java 8 date types, such as LocalDate and LocalDateTime. We can only use it to format fields of the type java.util.Date or the java.util.Calendar." I use `LocalDateTime`, then should I configure serializer? – Jack Feb 19 '23 at 21:25