Spring: create LocalDate or LocalDateTime from @Value parameter

限于喜欢 提交于 2020-01-14 13:13:44

问题


In a spring project, I'd like to create a LocalDate from an @Autowired constructor parameter whose value is in a .properties file. Two things I'd like to do:

  1. If the property file contains the property my.date, the parameter should be created by parsing the property value

When the property is set, and when I use the following:

@DateTimeFormat(pattern = "yyyy-MM-dd") @Value("${my.date}") LocalDate myDate,
...

I get this error: java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'java.time.LocalDate': no matching editors or conversion strategy found

I have also used the iso = ... to use an ISO date with the same result.

  1. If the property is not in the properties file, the parameter should be created using LocalDate.now()

I tried using a default value as such:

@Value("${my.date:#{T(java.time.LocalDate).now()}}") LocalDate myDate,
...

But I get the same error.

Forgive my ignorance with Spring, but how can I achieve the two objectives here?


回答1:


I know two ways. One is generic for any object - to use @Value annotation on custom setter

@Component
public class Example {

    private LocalDate localDate;

    @Value("${property.name}")
    private void setLocalDate(String localDateStr) {
        if (localDateStr != null && !localDateStr.isEmpty()) {
            localDate = LocalDate.parse(localDateStr);
        }
    }
}

The second is for LocalDate/LocalDateTime

public class Example {
    @Value("#{T(java.time.LocalDate).parse('${property.name}')}")
    private LocalDate localDate;
}

Sample property:

property.name=2018-06-20



回答2:


If you want to specify the date format as well then use following on the field:

@Value("#{T(java.time.LocalDate).parse('${date.from.properties.file}', T(java.time.format.DateTimeFormatter).ofPattern('${date.format.from.properties.file}'))}")



回答3:


Try to add this into your properties file:

spring.jackson.date-format=com.fasterxml.jackson.databind.util.ISO8601DateFormat
spring.jackson.time-zone=UTC

and remove @DateTimeFormat annotation

Concerning LocalDate.now() initialization. Try to use field injection this way:

@Value("${my.date}") LocalDate myDate = LocalDate.now();



回答4:


As mentioned in other answer by Pavel there are two ways.

I am providing similar two ways with modification to handle 2nd Point by OP.

If the property is not in the properties file, the parameter should be created using LocalDate.now()

@Component
public class Example {

private LocalDate localDate;

@Value("${property.name}")
private void setLocalDate(String localDateStr) {
    if (localDateStr != null && !localDateStr.isEmpty()) {
        localDate = LocalDate.parse(localDateStr, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
    }else{
        localDate = LocalDate.now();
    }
}
}

I Prefere 2nd way though...

public class Example {
    @Value("#{T(java.time.LocalDate).parse('${property.name}', T(java.time.format.DateTimeFormatter).ofPattern('yyyy-MM-dd')) ?: T(java.time.LocalDate).now()}")
    private LocalDate localDate;
}

Edit:- Fixed 2nd Way

@Value("#{ !('${date:}'.equals('')) ? T(java.time.LocalDate).parse('${date:}', T(java.time.format.DateTimeFormatter).ofPattern('MM-dd-yyyy')) " +
        ":T(java.time.LocalDate).now()}")
private LocalDate asOfDate;


来源:https://stackoverflow.com/questions/48575426/spring-create-localdate-or-localdatetime-from-value-parameter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!