如何创建一个注释,是一组注解? [英] How to create an annotation that is a group of annotations?

查看:148
本文介绍了如何创建一个注释,是一组注解?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个一年左右的时间,我读了解释,我怎么能创建一个注释,基本上是其他注释的容器中的文章。这样,如果我总是在特定用例使用相同的5注释创建包含这些注释并使用它。

A year or so I read an article that explained how I could create an annotation that basically is a container for other annotations. This way if I always use the same 5 annotations in a specific use-case I create an annotation that contains them and use that instead.

不幸的是,我再也找不到那篇文章真的想这样做,现在我的杰克逊配置。

Unfortunately, I can't find the article anymore and would really like to do that right now for my jackson configuration.

因为我找不到上对我自己,我开始怀疑我的记忆中的任何信息。这是可能的,或者我只是错了?

Since I can't find any information on that on my own I'm beginning to question my memory. Is this possible or I am just wrong?

修改

我想是这样的:

@Target(ElementType.METHOD)
@com.fasterxml.jackson.databind.annotation.JsonSerialize(using=MySerializerThatIsUsedEverywhere.class
@javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter(MyCustomXmlAdapter.class)
@SomeOtherEvaluatedByTheSerializer
public @interface SerializerUseCase01 {
    public String a();
    public int b();
)

我的情况是,我有一堆的,可以通过不同的CONFIGS相同的串行处理序列化的用例。为了使一切更容易使用和更透明的我想换杰克逊配置和串行配置成一个注解。

my scenario is that i have a bunch of serialization use cases that can be handled by the same serializer with different configs. To make everything easier to use and more transparent i want to wrap the jackson config and the serializer config into one annotation.

推荐答案

有关杰克逊,这可以用 @JacksonAnnotationsInside 做过元注解。请参见更多的这篇文章,但是从那里code片段是:

For Jackson, this can be done with @JacksonAnnotationsInside meta-annotation. See this article for more, but code snippet from there is:

@Retention(RetentionPolicy.RUNTIME) // IMPORTANT
@JacksonAnnotationsInside
@JsonInclude(Include.NON_NULL)
@JsonPropertyOrder({ "id", "name" }) 
public @interface MyStdAnnotations

和从上面您可以使用此类型自己的类,像这样:

and from thereon you can use this type for your own classes like so:

@MyStdAnnotations
public class MyBean {
   public String name, id;
}

这篇关于如何创建一个注释,是一组注解?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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