Well I found a somewhat elegant solution. Actually, two somewhat elegant
solutions I'd like to share... Along the way, I've learned more about JAXB
Internals than I ever wanted to know unfortunately... Both involve
overriding the default AnnotationReader in the
org.apache.cxf.jaxb.JAXBDataBinding. If someone with wiki access could
format these in a comprehensible manner, it would be much appreciated.



First Solution is to use JAXBIntroductions from JBoss
(http://community.jboss.org/wiki/JAXBIntroductions). This is basically a
replacement for Aegis. You create POJOs, then use the introductions XSD to
map them to XML. It works fairly well, the schema is easy to understand for
anyone that has used JAXB. The only downside the current release does not
let you override existing JAXB Annotations. This is a good solution if you
need to map classes in a third party jar that do not already have existing
JAXB annotations.  Attached are an example config file and spring snippets. 
http://cxf.547215.n5.nabble.com/file/n3335130/spring-config-snippet.xml
spring-config-snippet.xml 
http://cxf.547215.n5.nabble.com/file/n3335130/intro-config.xml
intro-config.xml 



The second solution is to implement your own custom annotation reader.
Create a new annotation called "ExternallyTransient" and annotate your pojos
fields with it. Next create your custom annotation reader that will
substitue XMLTransient when it encounters ExternallyTransient. I did this by
wrapping the default annotation reader: RuntimeInlineAnnotationReader.
First, create a new class that implements RuntimeAnnotationReader. Declare a
field of RuntimeInlineAnnotationReader and generate all delegate methods to
it. Finally, decorate the "hasFieldAnnotation" and "hasMethodAnnotations"
category of methods with code something like this:

        /**
         * {...@inheritdoc}
         */
        @Override
        public boolean hasFieldAnnotation(Class<? extends Annotation>
annotationType, Field field) {
                if (field.getAnnotation(ExternallyTransient.class) != null) {
                        return false;
                }
                return annotationReader.hasFieldAnnotation(annotationType, 
field);
        }

        /**
         * {...@inheritdoc}
         */
        @Override
        public Annotation[] getAllFieldAnnotations(Field field, Locatable 
srcPos) {
                Annotation[] allFieldAnnotations =
annotationReader.getAllFieldAnnotations(field, srcPos);
                if (field.getAnnotation(ExternallyTransient.class) != null) {
                        return new Annotation[] { new XmlTransient() {
                                @Override
                                public Class<? extends Annotation> 
annotationType() {
                                        return XmlTransient.class;
                                }
                        } };
                }
                return allFieldAnnotations;
        }


This is working awesome for us, CXF never sees the fields and they're not
listed in the WSDL. Good luck!
-- 
View this message in context: 
http://cxf.547215.n5.nabble.com/Overriding-JAXB-annotations-tp3320779p3335130.html
Sent from the cxf-user mailing list archive at Nabble.com.

Reply via email to