注释作为另一个注释的快捷方式? [英] Annotation as shortcut for another annotation?

查看:28
本文介绍了注释作为另一个注释的快捷方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建一个注解,例如@DateOutput 等价于另一个Jackson注解,例如:

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)

推荐答案

您可以使用 @JacksonAnnotationsInside 作为注释容器,如下所示:

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 {
}

这篇关于注释作为另一个注释的快捷方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆