如何在 dto class 的注释中导入 application.yml 属性
How to import application.yml property in an annotation in dto class
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;
}
我试过了
@JsonFormat(pattern = "${datetime.format}")
不行。我想用 datetime.format
替换 JsonFormat 模式
试试这个:
@Value("${datetime.format}")
private String dateFormat;
@JsonFormat(pattern = dateFormat)
当我遇到同样的问题时,我只是定义了一个全局变量
public static String date = "yyyy/MM/dd HH:mm:ss";
和
更改模式以使用全局变量
@JsonFormat(pattern = Classname.date)
它对我有用。
真正的解决方案是在配置 ObjectMapper
期间使用 属性。
在 spring 中,最简单的方法是为 属性
定义值
spring.jackson.date-format=${datetime.format}
此解决方案的主要缺点是它只允许为每个应用程序配置一种日期格式。
因为,正如您所指出的,不可能在 @JsonFormat
注释中使用 SPEL。而且注解参数中也不可能使用非常量字段。我不认为有一个简单的解决方案可以使用 属性.
配置日期格式
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;
}
我试过了
@JsonFormat(pattern = "${datetime.format}")
不行。我想用 datetime.format
替换 JsonFormat 模式试试这个:
@Value("${datetime.format}")
private String dateFormat;
@JsonFormat(pattern = dateFormat)
当我遇到同样的问题时,我只是定义了一个全局变量
public static String date = "yyyy/MM/dd HH:mm:ss";
和
更改模式以使用全局变量
@JsonFormat(pattern = Classname.date)
它对我有用。
真正的解决方案是在配置 ObjectMapper
期间使用 属性。
在 spring 中,最简单的方法是为 属性
定义值spring.jackson.date-format=${datetime.format}
此解决方案的主要缺点是它只允许为每个应用程序配置一种日期格式。
因为,正如您所指出的,不可能在 @JsonFormat
注释中使用 SPEL。而且注解参数中也不可能使用非常量字段。我不认为有一个简单的解决方案可以使用 属性.