问题
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