In my code, I'm going to have lots of getters like this with the same set of annotations (one for Hibernate and others for Jackson):
在我的代码中,我将使用相同的一组注释(一个用于Hibernate,另一个用于Jackson)有很多这样的getter:
@Transient
@JsonSerialize(using = LocalDateTimeSerializer.class)
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = BaseEntity.JSON_DATETIME_FORMAT)
public LocalDateTime getCreatedDateDT() {
return DateTimeUtils.klabTimestampToLocalDateTime(getCreatedDate(), createdDateDT);
}
I want to minimize repetion by implementing my own annotation, which will mean all these four together, like this:
我希望通过实现我自己的注释来最小化重复,这将意味着所有这四个一起,如下所示:
@TransientLocalDateTime
public LocalDateTime getCreatedDateDT() {
return DateTimeUtils.klabTimestampToLocalDateTime(getCreatedDate(), createdDateDT);
}
How can I do it? Is this even possible?
我该怎么做?这有可能吗?
UPDATE Thanks to Konstantin Yovkov, now I know, that I can combine all Jackson annotations in one, but that is because Jackson's annotation processor recognizes such a trick. I wonder if it is possible to make any annotation processor do that? It seems to me that it's not.
更新感谢Konstantin Yovkov,现在我知道,我可以将所有杰克逊注释合二为一,但这是因为杰克逊的注释处理器能够识别出这样的技巧。我想知道是否有可能让任何注释处理器这样做?在我看来,事实并非如此。
1 个解决方案
#1
6
Jackson provides a meta-annotation (annotation used for annotating another annotation), called @JacksonAnnotationsInside
, which is a:
Jackson提供了一个元注释(用于注释另一个注释的注释),称为@JacksonAnnotationsInside,它是:
Meta-annotation (annotations used on other annotations) used for indicating that instead of using target annotation (annotation annotated with this annotation), Jackson should use meta-annotations it has.
元注释(在其他注释上使用的注释)用于指示不使用目标注释(使用此注释注释的注释),杰克逊应该使用它具有的元注释。
This can be useful in creating "combo-annotations" by having a container annotation, which needs to be annotated with this annotation as well as all annotations it 'contains'.
这可以通过使用容器注释来创建“组合注释”,这需要使用此注释以及它“包含”的所有注释进行注释。
So, you should create an annotation like this one:
所以,你应该创建一个像这样的注释:
@Target(value = { ElementType.TYPE, ElementType.METHOD,
ElementType.PARAMETER, ElementType.FIELD })
@Retention(RetentionPolicy.RUNTIME)
@JacksonAnnotationsInside // <-- this one is mandatory
@Transient
@JsonSerialize(using = LocalDateTimeSerializer.class)
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = BaseEntity.JSON_DATETIME_FORMAT)
public @interface TransientLocalDateTime {
}
and use is like:
并且使用就像:
@TransientLocalDateTime
public LocalDateTime getCreatedDateDT() {
return DateTimeUtils.klabTimestampToLocalDateTime(getCreatedDate(), createdDateDT);
}
#1
6
Jackson provides a meta-annotation (annotation used for annotating another annotation), called @JacksonAnnotationsInside
, which is a:
Jackson提供了一个元注释(用于注释另一个注释的注释),称为@JacksonAnnotationsInside,它是:
Meta-annotation (annotations used on other annotations) used for indicating that instead of using target annotation (annotation annotated with this annotation), Jackson should use meta-annotations it has.
元注释(在其他注释上使用的注释)用于指示不使用目标注释(使用此注释注释的注释),杰克逊应该使用它具有的元注释。
This can be useful in creating "combo-annotations" by having a container annotation, which needs to be annotated with this annotation as well as all annotations it 'contains'.
这可以通过使用容器注释来创建“组合注释”,这需要使用此注释以及它“包含”的所有注释进行注释。
So, you should create an annotation like this one:
所以,你应该创建一个像这样的注释:
@Target(value = { ElementType.TYPE, ElementType.METHOD,
ElementType.PARAMETER, ElementType.FIELD })
@Retention(RetentionPolicy.RUNTIME)
@JacksonAnnotationsInside // <-- this one is mandatory
@Transient
@JsonSerialize(using = LocalDateTimeSerializer.class)
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = BaseEntity.JSON_DATETIME_FORMAT)
public @interface TransientLocalDateTime {
}
and use is like:
并且使用就像:
@TransientLocalDateTime
public LocalDateTime getCreatedDateDT() {
return DateTimeUtils.klabTimestampToLocalDateTime(getCreatedDate(), createdDateDT);
}