問題描述
大約一年后,我讀了一篇文章,解釋了如何創建一個基本上是其他注釋容器的注釋.這樣,如果我總是在特定用例中使用相同的 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.
很遺憾,我再也找不到這篇文章了,我很想現在就為我的 jackson 配置做這篇文章.
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();
)
我的場景是我有一堆序列化用例,可以由具有不同配置的相同序列化程序處理.為了讓一切更容易使用和更透明,我想將 jackson 配置和序列化器配置包裝到一個注釋中.
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.
推薦答案
對于 Jackson,這可以通過 @JacksonAnnotationsInside
元注釋來完成.有關更多信息,請參閱這篇文章,但其中的代碼片段是:
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;
}
這篇關于如何創建作為一組杰克遜注釋的注釋?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!