简体   繁体   English

用一个自定义注释代替复杂的注释组合

[英]Substitute complicated combo of annotations with one custom annotation

In my current project I have this group of big and complicated annotations I have to put on a lots of fields around my class. 在我当前的项目中,我需要这组庞大而又复杂的注释,我必须在课堂上放置很多字段。 They are Jackson json annotations. 它们是Jackson的json注释。

@JsonSerialize(using = IViewableToReferenceSerializer.class, contentUsing = IViewableToReferenceSerializer.class)
private ComplexeObject myObject;

and I would like to substitute it to something like: 我想将其替换为:

@JsonAsReference 
private ComplexeObject myObject;

I already tested the system without the custom annotation and it works fine. 我已经在没有自定义注释的情况下测试了系统,并且工作正常。 I though I could define my annotation as (for example): 我虽然可以将注释定义为(例如):

@Target({ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@JsonSerialize(using = IViewableToReferenceSerializer.class, contentUsing = IViewableToReferenceSerializer.class)
public @interface JsonAsReference {
}

So my question is: Is it even possible? 所以我的问题是:有可能吗? Or am I just doing something wrong? 还是我做错了什么?

UPDATE : I found an answer for the particuliar Jackson case in this thread Create a custom Jackson annotation 更新 :我在此线程中找到了特殊杰克逊案例的答案创建自定义杰克逊注解

but I am open to any general case solution. 但我愿意接受任何一般情况的解决方案。

With the default jackson library you will have no luck. 使用默认的杰克逊库,您将没有运气。

Here a code snippet from JacksonAnnotationIntrospector (from jackson 1.9.13) 这是JacksonAnnotationIntrospector的代码片段(来自杰克逊1.9.13)

public Object findSerializer(Annotated a) 
{
    /* 21-May-2009, tatu: Slight change; primary annotation is now
     *    @JsonSerialize; @JsonUseSerializer is deprecated
     */
    JsonSerialize ann = a.getAnnotation(JsonSerialize.class);
    if (ann != null) {
        Class<? extends JsonSerializer<?>> serClass = ann.using();
        if (serClass != JsonSerializer.None.class) {
            return serClass;
        }
    }

   ....
}

As you can see only the annotation JsonSerialize will be taken into account. 如您所见,将仅考虑注释JsonSerialize Jackson will not search for a JsonSerialize annotation on other annotation like you wanted. Jackson不会像您想要的那样在其他注释上搜索JsonSerialize注释。

You can implement an own AnnotationIntrospector and provide it to the SerializationConfig and DeserializationConfig , eg 您可以实现自己的AnnotationIntrospector并将其提供给SerializationConfigDeserializationConfig ,例如

AnnotationIntrospector annIntr = ....; // your implementation

ClassIntrospector<? extends BeanDescription> intr = new BasicClassIntrospector();
VisibilityChecker<?> vc = VisibilityChecker.Std.defaultInstance();
TypeFactory typeFactory= TypeFactory.defaultInstance();

SerializationConfig serConfig = new SerializationConfig(intr, annIntr, vc, null,
            null, typeFactory, null);
DeserializationConfig deserConfig = new DeserializationConfig(intr, annIntr, vc, null,
            null, typeFactory, null);

// null means use a default implementation
ObjectMapper objectMapper = new ObjectMapper(null, null, null, serConfig, deserConfig);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM