I want to group some of my annotation to use them in more simple way. I read this document about @Stereotype annotation. I want to achive something like this:
@Stereotype
@AnnotationA
@AnnotationB
@Annotationc
@Target(...)
@Retention(...)
public @interface GroupAnnotation {
}
@GroupAnnotation
class X {}
@GroupAnnotation
class Y {}
But whenever I tried to use this annotation I got the following error message in IntelliJ:
Cannot resolve symbol 'Stereotype'
So is there any way to use this annotation in spring? Or do u have any alternatives of grouping annotations?
I'm using spring 2.7.1 and Java 11.
--- UPDATE ---
My real life scenario related to use @JsonFormat annotation. I wanted to setup it in one place and use it in many cases.
CodePudding user response:
Combining Spring's annotations
Annotation @Stereotype
is a part of Java EE (Jakarta EE) world, and it's not available in Spring. More over, it's not needed in Spring.
Assuming that you need to combine several annotations provided by Spring, here's how it can be done:
@SpringAnnotationA // something like @RestController, @RequestMapping("/foo"), etc.
@SpringAnnotationB
@SpringAnnotationC
@Retention(RetentionPolicy.RUNTIME) // mandatory
@Target(ElementType.TYPE) // limit the types of elements on which annotation can be applied
@Documented // to reflect the annotation in the documentation
public @interface MyAnnotation {}
A more concrete example:
@RestController
@RequestMapping("/foo")
@Validated
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented
public @interface MyController {}
By the way, here's the right place to go when you need Spring documentation:
Combining Jackson's annotations
In order to create a custom multi-annotation from several Jacksons annotations, you need to apply a special meta-annotation @JacksonAnnotationsInside
provided by Jackson:
Meta-annotation (annotations used on other annotations) used for indicating that instead of using target annotation (annotation annotated with this annotation), Jackson should use meta-annotations it has. This can be useful in creating "combo-annotations" by having a container annotation, which needs to be annotated with this annotation as well as all annotations it 'contains'.
Example:
@JacksonAnnotationsInside
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface MyJsonAnnotation {}