您的位置:首页 > Web前端

Difference of Maven JAXB plugins

2016-05-23 12:20 489 查看
http://stackoverflow.com/questions/2432859/difference-of-maven-jaxb-plugins
http://kalssworld.blogspot.com/2015/02/jaxb2-annotate-plugin.html


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

<plugin>
<groupid>org.jvnet.jaxb2.maven2</groupid>
<artifactid>maven-jaxb2-plugin</artifactid>
<version>0.8.2</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
<configuration>
<schemadirectory>src/main/resources/xsd</schemadirectory>
<bindingdirectory>src/main/resources/xsd</bindingdirectory>
<generatedirectory>${project.build.directory}/generated-sources/xjc</generatedirectory>
<bindingfiles>binding.xjb</bindingfiles>
<extension>true</extension>
<args>
<arg>-XtoString</arg>
<arg>-Xequals</arg>
<arg>-XhashCode</arg>
<arg>-Xannotate</arg>
</args>
<plugins>
<plugin>
<groupid>org.jvnet.jaxb2_commons</groupid>
<artifactid>jaxb2-basics</artifactid>
<version>0.6.4</version>
</plugin>
<plugin>
<groupid>org.jvnet.jaxb2_commons</groupid>
<artifactid>jaxb2-basics-annotate</artifactid>
<version>0.6.4</version>
</plugin>
</plugins>
</configuration>
<dependencies>
<dependency>
<groupid>org.codehaus.jackson</groupid>
<artifactid>jackson-jaxrs</artifactid>
<version>1.9.13</version>
</dependency>
</dependencies>
</plugin>


In binding.xjb

<!--?xml version="1.0" encoding="UTF-8"?-->

<jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:annox="http://annox.dev.java.net" xsi:schemalocation="http://java.sun.com/xml/ns/jaxb http://java.sun.com/xml/ns/jaxb/bindingschema_2_0.xsd" jaxb:extensionbindingprefixes="xjc annox" version="2.1">

<jaxb:bindings schemalocation="OMA-SUP-XSD_rest_netapi_messaging-V1_0-20130709-C.xsd" node="/xs:schema">
<jaxb:bindings node="xs:complexType[@name='DeliveryInfoNotification']">
<annox:annotate target="class">
<annox:annotate annox:class="org.codehaus.jackson.annotate.JsonIgnoreProperties" ignoreunknown="true">
</annox:annotate>
</annox:annotate></jaxb:bindings>
</jaxb:bindings>

</jaxb:bindings>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: