包含在注释中时,@ JsonSerialize和@JsonDeserialize无法正常工作 [英] @JsonSerialize and @JsonDeserialize not working when included in an annotation
问题描述
我试图创建一个自定义批注,以始终在给定属性添加注记的情况下将其标记化,因此我具有以下结构:
I am trying to create a custom annotation to tokenize a given property always when it is annotated with, so I have the following structure:
@JsonComponent
public class TokenSerializer {
@JsonSerialize(using = IdToTokenSerializer.class) // This does not work
@JsonDeserialize(using = TokenToIdDeserializer.class) // This does not work
@Retention(RetentionPolicy.RUNTIME)
public static @interface TokenizedId {
Class<?> value();
}
public static class IdToTokenSerializer extends JsonSerializer<Long> implements ContextualSerializer {
...
}
public static class TokenToIdDeserializer extends JsonDeserializer<Long> implements ContextualDeserializer {
...
}
}
我为什么要那样使用?因为@TokenizedId
将提供一个类,有条件地将在序列化器/反序列化器上考虑该类以执行某项操作.使用ContextualDeserializer
配置此值,然后从@TokenizedId
获取类.
Why am I using like that? Because the @TokenizedId
will provide a class, which conditionally will be considered on the serializer/deserializer to do something. This value is configured using the ContextualDeserializer
, fetching the class from @TokenizedId
.
问题是,当我这样注释时,序列化程序和反序列化程序都无法工作:
The issue is, both serializer and deserializer do not work when I annotate just like this:
@TokenizedId(MyClass.class)
private Long id;
但是当我这样使用它们时(从@TokenizedId
中删除@JsonSerialize
和@JsonDeserialize
),它们会起作用:
But they work when I use like this (removing the @JsonSerialize
and @JsonDeserialize
from @TokenizedId
):
@JsonSerialize(using = IdToTokenSerializer.class)
@JsonDeserialize(using = TokenToIdDeserializer.class)
@TokenizedId(MyClass.class)
private Long id;
我个人不喜欢这种方法,因为开发人员在想要对某些ID进行标记时,必须始终记住使用这三个注释,而且我希望@TokenizedId
始终与这些序列化程序相关.
Personally I did not like this approach, since developers will need to always remember to use these three annotations when they would like to tokenize some id, and also I would like the @TokenizedId
always be related to these serializers.
在另一个注释上注释时,有没有办法使序列化器/反序列化器正常工作?
Is there a way to make the serializer/deserializer work when annotated on another annotation?
推荐答案
我能够按照我想要的方式使注释起作用,在查看Jackson图书馆的一些线索后,我发现了@JacksonAnnotationsInside
注释:
I was able to make the annotation works the way I wanted, looking after some clue on the Jackson lib, I found the @JacksonAnnotationsInside
annotation:
/**
* 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'.
*
* @since 2.0
*/
@Target({ElementType.ANNOTATION_TYPE})
@Retention(RetentionPolicy.RUNTIME)
@JacksonAnnotation
public @interface JacksonAnnotationsInside
{
}
在我的注释中包含此内容可解决此问题:
Including this on my annotation solved the issue:
@JacksonAnnotationsInside
@JsonSerialize(using = IdToTokenSerializer.class)
@JsonDeserialize(using = TokenToIdDeserializer.class)
@Retention(RetentionPolicy.RUNTIME)
public static @interface TokenizedId {
Class<?> value();
}
这篇关于包含在注释中时,@ JsonSerialize和@JsonDeserialize无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!