java 如何创建一组 Jackson 注释的注释?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13401581/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create an annotation that is a group of Hymanson annotations?
提问by Laures
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.
大约一年左右,我阅读了一篇文章,其中解释了如何创建一个注释,该注释基本上是其他注释的容器。这样,如果我总是在特定用例中使用相同的 5 个注释,我会创建一个包含它们的注释并使用它。
Unfortunately, I can't find the article anymore and would really like to do that right now for my Hymanson configuration.
不幸的是,我再也找不到这篇文章了,现在真的很想为我的 Hymanson 配置做这件事。
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?
由于我自己找不到任何相关信息,我开始质疑我的记忆力。这是可能的还是我错了?
EDIT
编辑
What i want is something like:
我想要的是这样的:
@Target(ElementType.METHOD)
@com.fasterxml.Hymanson.databind.annotation.JsonSerialize(using=MySerializerThatIsUsedEverywhere.class
@javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter(MyCustomXmlAdapter.class)
@SomeOtherEvaluatedByTheSerializer
public @interface SerializerUseCase01 {
public String a();
public int b();
)
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 Hymanson config and the serializer config into one annotation.
我的场景是我有一堆序列化用例,可以由具有不同配置的同一个序列化器处理。为了使一切更易于使用和更透明,我想将 Hymanson 配置和序列化器配置包装到一个注释中。
采纳答案by StaxMan
For Hymanson, this can be done with @HymansonAnnotationsInside
meta-annotation. See this articlefor more, but code snippet from there is:
对于Hyman逊来说,这可以通过@HymansonAnnotationsInside
元注释来完成。有关更多信息,请参阅本文,但那里的代码片段是:
@Retention(RetentionPolicy.RUNTIME) // IMPORTANT
@HymansonAnnotationsInside
@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;
}
回答by Thor84no
There are some examples hereon how to make various combinations of annotations containing other annotations. Is this what you're looking for?
有一些例子在这里就如何使其他含有注解注释的各种组合。这是你要找的吗?
Example from the source:
来源示例:
@Target(ElementType.METHOD)
public @interface SimpleAnnotation {
public String a();
public int b();
)
@Target(ElementType.METHOD)
public @interface ReallyComplexAnnotation {
public SimpleAnnotation[] value();
)
Used like this:
像这样使用:
@ReallyComplexAnnotation(
{ @SimpleAnnotation(a="...", b=3), @SimpleAnnotation(a="...", b=4) }
)