Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

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?

EDIT

What i want is something like:

@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();
)

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.

like image 300
Laures Avatar asked Nov 15 '12 16:11

Laures


People also ask

What does @JsonProperty annotation do?

The @JsonProperty annotation is used to map property names with JSON keys during serialization and deserialization. By default, if you try to serialize a POJO, the generated JSON will have keys mapped to the fields of the POJO.

What is the use of Jackson annotations?

Jackson Annotations - @JacksonInject @JacksonInject is used when a property value is to be injected instead of being parsed from Json input. In the example below, we are inserting a value into object instead of parsing from the Json.

Which of the following is an example of multi value annotation?

An annotation that has more than one method, is called Multi-Value annotation. For example: @interface MyAnnotation{ int value1();


2 Answers

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; } 
like image 79
StaxMan Avatar answered Sep 19 '22 14:09

StaxMan


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) } ) 
like image 44
Thor84no Avatar answered Sep 21 '22 14:09

Thor84no