Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java make a separate annotation that combines a others

Working with Play Framework 2.2, making a RESTfull API.

In a model I'm using, I wanted to output(Json with Jackson) only the Id of a related object, not the entire object. I found how to do that, as follows:

@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
@JsonIdentityReference(alwaysAsId = true)
@JsonIgnore
public Object myObject;

The Json output will contain a JsonNode "myObjectId: 1". For instance.

The IdentityInfo and IdentityReference take care of this.

The problem is, wherever I want this, I have to paste down these 3 lines, along with any other annotations that need to be present for a certain field. This get grow too large and I'm trying to create just 1 custom annotation that does all these things together.

Is this possible, and how? A link where I can read about it or an example would be appreciated.

like image 470
KdgDev Avatar asked May 09 '14 10:05

KdgDev


People also ask

Can we apply two annotations together?

It is also possible to use multiple annotations on the same declaration: @Author(name = "Jane Doe") @EBook class MyClass { ... } If the annotations have the same type, then this is called a repeating annotation: @Author(name = "Jane Doe") @Author(name = "John Smith") class MyClass { ... }

What is @interface annotation in Java?

Annotation is defined like a ordinary Java interface, but with an '@' preceding the interface keyword (i.e., @interface ). You can declare methods inside an annotation definition (just like declaring abstract method inside an interface). These methods are called elements instead.

Can you apply more than one annotation on any method?

You can repeat an annotation anywhere that you would use a standard annotation.

How do you inherit annotations?

Annotations, just like methods or fields, can be inherited between class hierarchies. If an annotation declaration is marked with @Inherited , then a class that extends another class with this annotation can inherit it.


1 Answers

To solve your problem you have to create an annotation annotated with JacksonAnnotationInside and annotations you want it to 'include'.

The Javadoc of that annotation says that it is:

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'.

For example for your case you'd have something like this:

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
@com.fasterxml.jackson.annotation.JacksonAnnotationsInside // this is important
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
@JsonIdentityReference(alwaysAsId = true)
public @interface MyCustomAnnotation {    
}

Then you can use it normally as you would Jackson annotations

@MyCustomAnnotation
public Object myObject;
like image 73
lpiepiora Avatar answered Sep 26 '22 15:09

lpiepiora