How to create an annotation that is a group of Jackson annotations?

前端 未结 2 1758
别跟我提以往
别跟我提以往 2020-12-13 06:17

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 annot

相关标签:
2条回答
  • 2020-12-13 06:37

    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;
    }
    
    0 讨论(0)
  • 2020-12-13 06:53

    There are some examples here on 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) }
    )
    
    0 讨论(0)
提交回复
热议问题