Annotation as shortcut for another annotation?

拈花ヽ惹草 提交于 2020-01-04 05:30:46

问题


How can I create an annotation, such as @DateOutput that is equivalent with another Jackson annotation, such as:

@JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd'T'HH:mm:ss.SSSZ", timezone="UTC")

Or ideally, to a set of annotations, such as

@JsonSerialize(using = XSerializer.class)
@JsonDeserialize(using = XDeserializer.class)

回答1:


You can use @JacksonAnnotationsInside as a annotation container like follows:

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD})
@JacksonAnnotationsInside
@JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd'T'HH:mm:ss.SSSZ", timezone="UTC")
public @interface DateOutput {
}

and use it to annotate the field in your class like so:

public class Foo {

   @DateOutput
   private Date date;

}

You can also bundle a set of annotations under a single annotation:

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD, ElementType.METHOD})
@JacksonAnnotationsInside
@JsonSerialize(using = XSerializer.class)
@JsonDeserialize(using = XDeserializer.class)
public @interface MyAnnotation {
}


来源:https://stackoverflow.com/questions/38643475/annotation-as-shortcut-for-another-annotation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!