问题
In a spring project, I'd like to create a LocalDate
from an @Autowire
d constructor parameter whose value is in a .properties
file. Two things I'd like to do:
- 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.
- 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