camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > JAXB
Date Mon, 01 Apr 2013 19:55:00 GMT
<html>
<head>
    <base href="https://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/2042/9/1/_/styles/combined.css?spaceKey=CAMEL&amp;forWysiwyg=true"
type="text/css">
    </head>
<body style="background: white;" bgcolor="white" class="email-body">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
    <h2><a href="https://cwiki.apache.org/confluence/display/CAMEL/JAXB">JAXB</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~muellerc">Christian
Mueller</a>
    </h4>
        <br/>
                         <h4>Changes (1)</h4>
                                 
    
<div id="page-diffs">
                    <table class="diff" cellpadding="0" cellspacing="0">
    
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" > <br> <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">h3.
Schema validation <br>*Available as of Camel 2.11* <br> <br>The JAXB [Data
Format] supports validation by marshalling and unmarshalling from/to XML. Your can use the
prefix *classpath:*, *file:* or *http:* to specify how the resource should by resolved. You
can separate multiple schema files by using the *&#39;,&#39;* character. <br>
<br>Using the Java DSL, you can configure it in the following way: <br>{code:java}
<br>JaxbDataFormat jaxbDataFormat = new JaxbDataFormat(); <br>jaxbDataFormat.setContextPath(Person.class.getPackage().getName());
<br>jaxbDataFormat.setSchema(&quot;classpath:person.xsd,classpath:address.xsd&quot;);
<br>{code} <br> <br>You can do the same using the XML DSL: <br>{code:xml}
<br>&lt;marshal&gt; <br>    &lt;jaxb id=&quot;jaxb&quot; schema=&quot;classpath:person.xsd,classpath:address.xsd&quot;/&gt;
<br>&lt;/marshal&gt; <br>{code} <br> <br> <br></td></tr>
            <tr><td class="diff-unchanged" >h3. Dependencies <br> <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h2><a name="JAXB-JAXB"></a>JAXB</h2>

<p>JAXB is a <a href="/confluence/display/CAMEL/Data+Format" title="Data Format">Data
Format</a> which uses the JAXB2 XML marshalling standard which is included in Java 6
to unmarshal an XML payload into Java objects or to marshal Java objects into an XML payload.</p>

<h3><a name="JAXB-UsingtheJavaDSL"></a>Using the Java DSL</h3>

<p>For example the following uses a named DataFormat of <em>jaxb</em> which
is configured with a number of Java package names to initialize the <a href="http://java.sun.com/javase/6/docs/api/javax/xml/bind/JAXBContext.html"
class="external-link" rel="nofollow">JAXBContext</a>. </p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
DataFormat jaxb = <span class="code-keyword">new</span> JaxbDataFormat(<span
class="code-quote">"com.acme.model"</span>);

from(<span class="code-quote">"activemq:My.Queue"</span>).
  unmarshal(jaxb).
  to(<span class="code-quote">"mqseries:Another.Queue"</span>);
</pre>
</div></div>

<p>You can if you prefer use a named reference to a data format which can then be defined
in your <a href="/confluence/display/CAMEL/Registry" title="Registry">Registry</a>
such as via your <a href="/confluence/display/CAMEL/Spring" title="Spring">Spring</a>
XML file. e.g. </p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"activemq:My.Queue"</span>).
  unmarshal(<span class="code-quote">"myJaxbDataType"</span>).
  to(<span class="code-quote">"mqseries:Another.Queue"</span>);
</pre>
</div></div>


<h3><a name="JAXB-UsingSpringXML"></a>Using Spring XML</h3>

<p>The following example shows how to use JAXB to unmarshal using <a href="/confluence/display/CAMEL/Spring"
title="Spring">Spring</a> configuring the jaxb data type</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml"><span class="code-tag">&lt;camelContext id=<span
class="code-quote">"camel"</span> xmlns=<span class="code-quote">"http://camel.apache.org/schema/spring"</span>&gt;</span>
  <span class="code-tag">&lt;route&gt;</span>
    <span class="code-tag">&lt;from uri=<span class="code-quote">"direct:start"</span>/&gt;</span>
    <span class="code-tag">&lt;unmarshal&gt;</span>
      <span class="code-tag">&lt;jaxb prettyPrint=<span class="code-quote">"true"</span>
contextPath=<span class="code-quote">"org.apache.camel.example"</span>/&gt;</span>
    <span class="code-tag">&lt;/unmarshal&gt;</span>
    <span class="code-tag">&lt;to uri=<span class="code-quote">"mock:result"</span>/&gt;</span>
  <span class="code-tag">&lt;/route&gt;</span>
<span class="code-tag">&lt;/camelContext&gt;</span>
</pre>
</div></div>

<p>This example shows how to configure the data type just once and reuse it on multiple
routes.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml"><span class="code-tag">&lt;camelContext id=<span
class="code-quote">"camel"</span> xmlns=<span class="code-quote">"http://camel.apache.org/schema/spring"</span>&gt;</span>
  <span class="code-tag">&lt;dataFormats&gt;</span>
    <span class="code-tag">&lt;jaxb id=<span class="code-quote">"myJaxb"</span>
prettyPrint=<span class="code-quote">"true"</span> contextPath=<span class="code-quote">"org.apache.camel.example"</span>/&gt;</span>
  <span class="code-tag">&lt;/dataFormats&gt;</span>

  <span class="code-tag">&lt;route&gt;</span>
    <span class="code-tag">&lt;from uri=<span class="code-quote">"direct:start"</span>/&gt;</span>
    <span class="code-tag">&lt;marshal ref=<span class="code-quote">"myJaxb"</span>/&gt;</span>
    <span class="code-tag">&lt;to uri=<span class="code-quote">"direct:marshalled"</span>/&gt;</span>
  <span class="code-tag">&lt;/route&gt;</span>
  <span class="code-tag">&lt;route&gt;</span>
    <span class="code-tag">&lt;from uri=<span class="code-quote">"direct:marshalled"</span>/&gt;</span>
    <span class="code-tag">&lt;unmarshal ref=<span class="code-quote">"myJaxb"</span>/&gt;</span>
    <span class="code-tag">&lt;to uri=<span class="code-quote">"mock:result"</span>/&gt;</span>
  <span class="code-tag">&lt;/route&gt;</span>

<span class="code-tag">&lt;/camelContext&gt;</span>
</pre>
</div></div>

<div class='panelMacro'><table class='tipMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/check.gif" width="16" height="16"
align="absmiddle" alt="" border="0"></td><td><b>Multiple context paths</b><br
/>It is possible to use this data format with more than one context path. You can specify
context path using <tt>:</tt> as separator, for example <tt>com.mycompany:com.mycompany2</tt>.
Note that this is handled by JAXB implementation and might change if you use different vendor
than RI.</td></tr></table></div>

<h3><a name="JAXB-Partialmarshalling%2Funmarshalling"></a>Partial marshalling/unmarshalling</h3>
<p><b>This feature is new to Camel 2.2.0.</b><br/>
JAXB 2 supports marshalling and unmarshalling XML tree fragments. By default JAXB looks for
<tt>@XmlRootElement</tt> annotation on given class to operate on whole XML tree.
This is useful but not always - sometimes generated code does not have @XmlRootElement annotation,
sometimes you need unmarshall only part of tree.<br/>
In that case you can use partial unmarshalling. To enable this behaviours you need set property
<tt>partClass</tt>. Camel will pass this class to JAXB's unmarshaler.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml"><span class="code-tag">&lt;camelContext id=<span
class="code-quote">"camel"</span> xmlns=<span class="code-quote">"http://camel.apache.org/schema/spring"</span>&gt;</span>
    <span class="code-tag">&lt;route&gt;</span>
        <span class="code-tag">&lt;from uri=<span class="code-quote">"direct:marshal"</span>/&gt;</span>
        <span class="code-tag">&lt;marshal&gt;</span>
            &lt;jaxb prettyPrint=<span class="code-quote">"false"</span> contextPath=<span
class="code-quote">"org.apache.camel.example"</span>
                partClass=<span class="code-quote">"org.apache.camel.example.PurchaseOrder"</span>
                fragment=<span class="code-quote">"true"</span>
                partNamespace=<span class="code-quote">"{http://example.camel.org/apache}po"</span>
/&gt;
        <span class="code-tag">&lt;/marshal&gt;</span>
        <span class="code-tag">&lt;to uri=<span class="code-quote">"mock:marshal"</span>/&gt;</span>
    <span class="code-tag">&lt;/route&gt;</span>
    <span class="code-tag">&lt;route&gt;</span>
        <span class="code-tag">&lt;from uri=<span class="code-quote">"direct:unmarshal"</span>/&gt;</span>
        <span class="code-tag">&lt;unmarshal&gt;</span>
            &lt;jaxb prettyPrint=<span class="code-quote">"false"</span> contextPath=<span
class="code-quote">"org.apache.camel.example"</span>
                partClass=<span class="code-quote">"org.apache.camel.example.Partial"</span>
/&gt;
        <span class="code-tag">&lt;/unmarshal&gt;</span>
        <span class="code-tag">&lt;to uri=<span class="code-quote">"mock:unmarshal"</span>/&gt;</span>
    <span class="code-tag">&lt;/route&gt;</span>
<span class="code-tag">&lt;/camelContext&gt;</span>
</pre>
</div></div>

<p>For marshalling you have to add <tt>partNamespace</tt> attribute with
QName of destination namespace. Example of Spring DSL you can find above.</p>

<h3><a name="JAXB-Fragment"></a>Fragment</h3>
<p><b>This feature is new to Camel 2.8.0.</b><br/>
JaxbDataFormat has new property fragment which can set the the <tt>Marshaller.JAXB_FRAGMENT</tt>
encoding property on the JAXB Marshaller. If you don't want the JAXB Marshaller to generate
the XML declaration, you can set this option to be true. The default value of this property
is fales. </p>

<h3><a name="JAXB-IgnoringtheNonXMLCharacter"></a>Ignoring the NonXML Character</h3>
<p><b>This feature is new to Camel 2.2.0.</b><br/>
JaxbDataFromat supports to ignore the <a href="http://www.w3.org/TR/2004/REC-xml-20040204/#NT-Char"
class="external-link" rel="nofollow">NonXML Character</a>, you just need to set the
filterNonXmlChars property to be true, JaxbDataFormat will replace the NonXML character with
" " when it is marshaling or unmarshaling the message. You can also do it by setting the <a
href="/confluence/display/CAMEL/Exchange" title="Exchange">Exchange</a> property
<tt>Exchange.FILTER_NON_XML_CHARS</tt>.</p>

<div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<td class='confluenceTd'>&nbsp;</td>
<th class='confluenceTh'> JDK 1.5 </th>
<th class='confluenceTh'> JDK 1.6+ </th>
</tr>
<tr>
<th class='confluenceTh'> Filtering in use </th>
<td class='confluenceTd'> StAX API and implementation </td>
<td class='confluenceTd'> No </td>
</tr>
<tr>
<th class='confluenceTh'> Filtering not in use </th>
<td class='confluenceTd'> StAX API only </td>
<td class='confluenceTd'> No </td>
</tr>
</tbody></table>
</div>


<p>This feature has been tested with Woodstox 3.2.9 and Sun JDK 1.6 StAX implementation.</p>

<h3><a name="JAXB-WorkingwiththeObjectFactory"></a>Working with the ObjectFactory</h3>

<p>If you use XJC to create the java class from the schema, you will get an ObjectFactory
for you JAXB context. Since the ObjectFactory uses <a href="http://java.sun.com/javase/6/docs/api/javax/xml/bind/JAXBElement.html"
class="external-link" rel="nofollow">JAXBElement</a> to hold the reference of the
schema and element instance value, jaxbDataformat will ignore the JAXBElement by default and
you will get the element instance value instead of the JAXBElement object form the unmarshaled
message body. <br/>
If you want to get the JAXBElement object form the unmarshaled message body, you need to set
the JaxbDataFormat object's ignoreJAXBElement property to be false.</p>


<h3><a name="JAXB-Settingencoding"></a>Setting encoding</h3>
<p>You can set the <b>encoding</b> option to use when marshalling. Its the
<tt>Marshaller.JAXB_ENCODING</tt> encoding property on the JAXB Marshaller.<br/>
You can setup which encoding to use when you declare the JAXB data format. You can also provide
the encoding in the <a href="/confluence/display/CAMEL/Exchange" title="Exchange">Exchange</a>
property <tt>Exchange.CHARSET_NAME</tt>. This property will overrule the encoding
set on the JAXB data format.</p>

<p>In this Spring DSL we have defined to use <tt>iso-8859-1</tt> as the
encoding:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml"><span class="code-tag">&lt;camelContext id=<span
class="code-quote">"camel"</span> xmlns=<span class="code-quote">"http://camel.apache.org/schema/spring"</span>&gt;</span>
    <span class="code-tag">&lt;route&gt;</span>
        <span class="code-tag">&lt;from uri=<span class="code-quote">"direct:start"</span>/&gt;</span>
        <span class="code-tag">&lt;marshal&gt;</span>
            <span class="code-tag">&lt;jaxb prettyPrint=<span class="code-quote">"false"</span>
encoding=<span class="code-quote">"iso-8859-1"</span> contextPath=<span class="code-quote">"org.apache.camel.example"</span>/&gt;</span>
        <span class="code-tag">&lt;/marshal&gt;</span>
        <span class="code-tag">&lt;to uri=<span class="code-quote">"mock:result"</span>/&gt;</span>
    <span class="code-tag">&lt;/route&gt;</span>
<span class="code-tag">&lt;/camelContext&gt;</span>
</pre>
</div></div>


<h3><a name="JAXB-Controllingnamespaceprefixmapping"></a>Controlling namespace
prefix mapping</h3>
<p><b>Available as of Camel 2.11</b></p>

<p>When marshalling using <a href="/confluence/display/CAMEL/JAXB" title="JAXB">JAXB</a>
or <a href="/confluence/display/CAMEL/SOAP" title="SOAP">SOAP</a> then the JAXB
implementation will automatic assign namespace prefixes, such as ns2, ns3, ns4 etc. To control
this mapping, Camel allows you to refer to a map which contains the desired mapping. </p>

<p>Notice this requires having JAXB-RI 2.1 or better (from SUN) on the classpath, as
the mapping functionality is dependent on the implementation of JAXB, whether its supported.</p>

<p>For example in Spring XML we can define a Map with the mapping. In the mapping file
below, we map SOAP to use soap as prefix. While our custom namespace "http://www.mycompany.com/foo/2"
is not using any prefix.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
  <span class="code-tag">&lt;util:map id=<span class="code-quote">"myMap"</span>&gt;</span>
    <span class="code-tag">&lt;entry key=<span class="code-quote">"http://www.w3.org/2003/05/soap-envelope"</span>
value=<span class="code-quote">"soap"</span>/&gt;</span>
    <span class="code-tag"><span class="code-comment">&lt;!-- we dont want
any prefix for our namespace --&gt;</span></span>
    <span class="code-tag">&lt;entry key=<span class="code-quote">"http://www.mycompany.com/foo/2"</span>
value=""/&gt;</span>
  <span class="code-tag">&lt;/util:map&gt;</span>
</pre>
</div></div>

<p>To use this in <a href="/confluence/display/CAMEL/JAXB" title="JAXB">JAXB</a>
or <a href="/confluence/display/CAMEL/SOAP" title="SOAP">SOAP</a> you refer to
this map, using the <tt>namespacePrefixRef</tt> attribute as shown below. Then
Camel will lookup in the <a href="/confluence/display/CAMEL/Registry" title="Registry">Registry</a>
a <tt>java.util.Map</tt> with the id "myMap", which was what we defined above.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
  <span class="code-tag">&lt;marshal&gt;</span>
    <span class="code-tag">&lt;soapjaxb version=<span class="code-quote">"1.2"</span>
contextPath=<span class="code-quote">"com.mycompany.foo"</span> namespacePrefixRef=<span
class="code-quote">"myMap"</span>/&gt;</span>
  <span class="code-tag">&lt;/marshal&gt;</span>
</pre>
</div></div>


<h3><a name="JAXB-Schemavalidation"></a>Schema validation</h3>
<p><b>Available as of Camel 2.11</b></p>

<p>The JAXB <a href="/confluence/display/CAMEL/Data+Format" title="Data Format">Data
Format</a> supports validation by marshalling and unmarshalling from/to XML. Your can
use the prefix <b>classpath:</b>, <b><a href="file:*" class="external-link"
rel="nofollow">file:*</a> or *http:</b> to specify how the resource should
by resolved. You can separate multiple schema files by using the <b>','</b> character.</p>

<p>Using the Java DSL, you can configure it in the following way:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
JaxbDataFormat jaxbDataFormat = <span class="code-keyword">new</span> JaxbDataFormat();
jaxbDataFormat.setContextPath(Person.class.getPackage().getName());
jaxbDataFormat.setSchema(<span class="code-quote">"classpath:person.xsd,classpath:address.xsd"</span>);
</pre>
</div></div>

<p>You can do the same using the XML DSL:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;marshal&gt;</span>
    <span class="code-tag">&lt;jaxb id=<span class="code-quote">"jaxb"</span>
schema=<span class="code-quote">"classpath:person.xsd,classpath:address.xsd"</span>/&gt;</span>
<span class="code-tag">&lt;/marshal&gt;</span>
</pre>
</div></div>


<h3><a name="JAXB-Dependencies"></a>Dependencies</h3>

<p>To use JAXB in your camel routes you need to add the a dependency on <b>camel-jaxb</b>
which implements this data format. </p>

<p>If you use maven you could just add the following to your pom.xml, substituting the
version number for the latest &amp; greatest release (see <a href="/confluence/display/CAMEL/Download"
title="Download">the download page for the latest versions</a>).</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
&lt;dependency&gt;
  &lt;groupId&gt;org.apache.camel&lt;/groupId&gt;
  &lt;artifactId&gt;camel-jaxb&lt;/artifactId&gt;
  &lt;version&gt;x.x.x&lt;/version&gt;
&lt;/dependency&gt;
</pre>
</div></div>
    </div>
        <div id="commentsSection" class="wiki-content pageSection">
        <div style="float: right;">
            <a href="https://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
        </div>
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/JAXB">View Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=68444&revisedVersion=22&originalVersion=21">View
Changes</a>
                |
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/JAXB?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message