Using application property in Spring Boot annotation?

rosy

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;
Honza Zidek

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)));
        };
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

@Autowired annotation is not recognized for repository in spring boot application

NotNull annotation preventing builds in spring boot application

Using Java and Spring Boot and my @Value annotation for my application-dev.properties is not being resolved?

Spring boot. The profit of using @ConfigurationProperties annotation

What is advantage of using @value annotation in Spring Boot

Configure mongodb property maxWaitQueueSize in Spring boot application?

how to set system property in spring boot application

Building Spring standalone application using Autowired annotation

Pass a YAML-based property value to @Scheduled annotation in Spring Boot

Spring Boot : Load property file in constructor and use as autowire annotation

Is there any way to enable/disable a annotation by property in Spring Boot?

Spring boot application not using jemalloc

How to assign annotation value from application.properties in Spring Boot?

Why Spring Boot Application class needs to have @Configuration annotation?

Implementing plugin architecture in annotation based Spring Boot Application

Spring boot application custom validation annotation doesn't fire

How to check if an spring boot application has a certain annotation in autoconfiguration class

@Value annotation unable to read properties file in spring boot camel application

Unable to read property from application.yml using @Value in Spring boot

Spring boot :Hibernate annotation

SPRING BOOT annotation: are they required

Spring boot @RequestMapping annotation

Annotation Transactional in spring boot

Cacheable Annotation in Spring Boot

Spring @Value annotation not using defaults when property is not present

Is it possible to read YAML property into Map using Spring and @Value annotation

How to configure for Spring Boot Configuration Annotation Processor using @ConfigurationProperties on IntelliJ?

How to schedule a cron job in spring boot without using @Scheduled() annotation

Spring Boot ObjectMapper for @RequestBody annotation using Jackson Framework