Friday, February 13, 2015

JAXB2 Annotate Plugin

I was using maven-jaxb2-plugin plugin to generate objects from a standard xsd and created a web service that accept from xml and json types of DeliveryInfoNotification object. In my cxf-servlet there was org.codehaus.jackson.jaxrs.JacksonJsonProvider that was used for binding JSON content to and from POJOs.

Web service was working fine with xml requests but for json requests it was giving the following error.

Unrecognized field "deliveryInfoNotification" (Class oma.xml.rest.netapi.messaging._1.DeliveryInfoNotification), not marked as ignorable

org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "deliveryInfoNotification" (Class oma.xml.rest.netapi.messaging._1.DeliveryInfoNotification), not marked as ignorable

 at [Source: org.apache.cxf.transport.http.AbstractHTTPDestination$1@c85f54d; line: 2, column: 33] (through reference chain: oma.xml.rest.netapi.messaging._1.DeliveryInfoNotification["deliveryInfoNotification"])

In order to get rid of that I had to annotate the DeliveryInfoNotification with @JsonIgnoreProperties

How do I get annotations in generated POJOs?

Answer was to use JAXB2 Annotate Plugin.

After several trial and errors it worked. With some configurations it was not giving any errors but was not generating the annotations which was a little frustrating. There were 2 options to configure annotations, add them in xsd or an external bindings file. I used the second option since it was not a great idea to change the standard xsd.

I got the following and had to add a dependency to the plugin to get rid of it.

Caused by: org.jvnet.annox.annotation.AnnotationClassNotFoundException: Annotation class [org.codehaus.jackson.annotate.JsonIgnoreProperties] could not be found.
    ... 32 more
Caused by: java.lang.ClassNotFoundException: org.codehaus.jackson.annotate.JsonIgnoreProperties

Anyway here is the final configuration that worked.

In pom.xml build->plugins->

                org.jvnet.jaxb2.maven2
                maven-jaxb2-plugin
                0.8.2
                
                    
                        
                            generate
                        
                    
                
                
                    src/main/resources/xsd
                    src/main/resources/xsd
                    ${project.build.directory}/generated-sources/xjc
                    binding.xjb
                    true
                    
                        -XtoString
                        -Xequals
                        -XhashCode
                        -Xannotate
                    
                    
                        
                            org.jvnet.jaxb2_commons
                            jaxb2-basics
                            0.6.4
                        
                        
                            org.jvnet.jaxb2_commons
                            jaxb2-basics-annotate
                            0.6.4
                        
                    
                
                
                    
                        org.codehaus.jackson
                        jackson-jaxrs
                        1.9.13
                    
                
            

In binding.xjb