Application.yml
datetime:
format: yyyy/MM/dd HH:mm:ss
transaction.java
@Entity
@Getter
@Setter
@NoArgsConstructor
public class Transaction{
@Id
@NotNull
@Enumerated(EnumType.STRING)
private TransactionSourceEnum transactionSource;
@Id
@NotNull
@Column(unique = true)
private String transactionId;
private String responseCode;
private String failureReason;
@NotNull
@JsonFormat(pattern = "yyyy/MM/dd HH:mm:ss")
private Date transactionCreSysDate;
}
I tried this
@JsonFormat(pattern = "${datetime.format}")
It won't work. I want to replace the JsonFormat pattern with datetime.format
The real solution would be to use the property during the configuration of ObjectMapper
.
In spring the simplest way to do that is to define value for the property
spring.jackson.date-format=${datetime.format}
The main drawback of this solution is that it only allows configuring one date format per application.
Because, as you noted, it is not possible to use SPEL inside @JsonFormat
annotation. And it is also impossible to use non-constant fields in the annotation parameters. I don't think there is a simple solution that would allow for configuring date format with configuration property.