我想在spring实体中创建一个json字段,如下所示
@Type(type = "json")
@Column(name = "LIFE_CYCLE_RULES")
private List<S3LifeCycleRule> lifeCycleRules;
但是类s3lifecyclerule具有关联的类
private S3LifeCycleExpiration expiration;
private String id;
private String prefix;
private S3LifeCycleFilter filter;
private Boolean status;
private List<S3LifeCycleTransition> transitions;
S3LifecycleExption.java:
@JsonSerialize
public record S3LifeCycleExpiration(
Instant Date,
int Days,
Boolean ExpiredObjectDeleteMarker
) {
}
s3lifecycletransition.java:
@JsonSerialize
public record S3LifeCycleTransition(
Instant date,
int days,
S3ObjectStorageClass storageClass
) {
}
(s3objectstorageclass是一个枚举器)
s3lifecyclefilter.java:
@JsonSerialize
public record S3LifeCycleFilter (
String prefix,
S3LifeCycleTag tag,
S3LifeCycleAnd and
){
}
s3lifecycletag.java:
@JsonSerialize
public record S3LifeCycleTag(
String key,
String value
) {}
s3lifecycleand.java:
@JsonSerialize
public record S3LifeCycleAnd(
String prefix,
List<S3LifeCycleTag> tags
) {
}
在我添加jsonserialize注解之前,应用程序不能完全工作,但是在我添加它之后,字段:expiration、filter和transitions保持为空:
输出json:
[
{
"id": "Life_Cycle_Rule_For_5_Days",
"filter": {},
"prefix": null,
"status": true,
"expiration": {},
"transitions": []
}
]
谢谢
2条答案
按热度按时间wecizke31#
您可以尝试向类中添加getter,这将很容易
Lombok
@Getter
注解。如果它不起作用,那么您需要为所有类创建自定义序列化程序,并将它们的注解更改为
@JsonSerialize(using = SomeCustomSerializer.class)
序列化程序演示(参见第2.7段)。jackson–自定义序列化程序
l2osamch2#
似乎问题出在记录上,在我将它们转换为普通类并添加默认构造函数之后,它就起作用了。
谢谢