Reputation: 45
For my project I am converting classes to json with jackson fasterXml. Now I have to change in one place the way the objects are converted so that @JsonIgnore is ignored and the property is serialised.
I found out that I can do this with extending a JacksonAnnotationIntrospector and a custom annotation. The problem is that the custom annotation is only visible if it is defined in the same package/project/osgi bundle as the implementing class while I would like to define it in the service package which exposes the json convertion. If the annotation is in the implementing package, I cannot reference it in the JacksonAnnotationIntrospector and I would need to check on the name of the annotation.
I have two identical annotations with only difference the package:
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.ANNOTATION_TYPE, ElementType.METHOD, ElementType.CONSTRUCTOR, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface AnnotationName {
}
When I set both on the implementation, I only see the annotation defined from within the same package/project/osgi bundle as the implementation class. Is there a constraint regarding this or has anyone an idea what could be the problem?
Upvotes: 0
Views: 1931
Reputation: 19606
Put your custom annotation in a unique package ideally using a reverse domain name you control. Put this into a separate API bundle (at least for now).
In your user code add the API bundle to the maven dependencies and use the new annotation. The maven-bundle-plugin (or what you use) should create an Import-Package statement in your Manifest. This will make sure the annotation can be wired at runtime.
In the code where you parse the annotations make sure you use the classloader of the user bundle. This classloader should be able to see the standard annotations as well as the custom one.
Upvotes: 1