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 Thu, 10 May 2012 21:51: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.
Using a custom NamespacePrefixMapper <br>Starting with Camel 2.9.3, you can use a custom
{{com.sun.xml.bind.marshaller.NamespacePrefixMapper}} to define the namespace prefix for a
given namespace uri. This feature is may only available in the JAXB RI! <br>{snippet:id=example|lang=xml|url=camel/trunk/components/camel-jaxb/src/test/resources/org/apache/camel/jaxb/marshalWithNamespacePrefixMapper.xml}
<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. For Camel versions below 1.5.0 you have to set the &lt;jaxb&gt; element directly
in &lt;camelContext&gt;.</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, from Camel 1.5.1 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>In Camel 1.6.1 and newer 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-UsingacustomNamespacePrefixMapper"></a>Using a custom
NamespacePrefixMapper</h3>
<p>Starting with Camel 2.9.3, you can use a custom <tt>com.sun.xml.bind.marshaller.NamespacePrefixMapper</tt>
to define the namespace prefix for a given namespace uri. This feature is may only available
in the JAXB RI!</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:marshall1"</span>/&gt;</span>
    <span class="code-tag">&lt;marshal&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> nameSpacePrefixMapper=<span
class="code-quote">"org.apache.camel.jaxb.MyNameSpacePrefixMapper"</span>/&gt;</span>
    <span class="code-tag">&lt;/marshal&gt;</span>
    <span class="code-tag">&lt;to uri=<span class="code-quote">"mock:marshall"</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:marshall2"</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">"mock:marshall"</span>/&gt;</span>
  <span class="code-tag">&lt;/route&gt;</span>
<span class="code-tag">&lt;/camelContext&gt;</span>

<span class="code-tag">&lt;bean id=<span class="code-quote">"myJaxb"</span>
class=<span class="code-quote">"org.apache.camel.converter.jaxb.JaxbDataFormat"</span>&gt;</span>
  <span class="code-tag">&lt;property name=<span class="code-quote">"contextPath"</span>
value=<span class="code-quote">"org.apache.camel.example"</span>/&gt;</span>
  <span class="code-tag">&lt;property name=<span class="code-quote">"nameSpacePrefixMapper"</span>&gt;</span>
      <span class="code-tag">&lt;bean class=<span class="code-quote">"org.apache.camel.jaxb.MyNameSpacePrefixMapper"</span>
/&gt;</span>
  <span class="code-tag">&lt;/property&gt;</span>
<span class="code-tag">&lt;/bean&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;1.6.0&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=17&originalVersion=16">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