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 Tue, 12 Jan 2010 08:32:00 GMT
<html>
<head>
    <base href="http://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/1519/1/1/_/styles/combined.css?spaceKey=CAMEL&amp;forWysiwyg=true"
type="text/css">
    </head>
<body style="background-color: white" bgcolor="white">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
     <h2><a href="http://cwiki.apache.org/confluence/display/CAMEL/JAXB">JAXB</a></h2>
     <h4>Page <b>edited</b> by             <a href="http://cwiki.apache.org/confluence/display/~njiang">willem
jiang</a>
    </h4>
     CAMEL-2330
          <div id="versionComment" class="noteMacro" style="display:none; padding: 5px;">
     CAMEL-2330<br />
     </div>
          <br/>
     <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"
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>

<h3><a name="JAXB-IgnoringtheNonXMLCharacter"></a>Ignoring the NonXML Character</h3>
<p>JaxbDataFromat supports to ignore the <a href="http://www.w3.org/TR/2004/REC-xml-20040204/#NT-Char"
rel="nofollow">NonXML Character</a>, you just need to set the filterNonXmlChars property
to be true, JaxbDataFromat 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>

<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"
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-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="http://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
       </div>

       <a href="http://cwiki.apache.org/confluence/display/CAMEL/JAXB">View Online</a>
       |
       <a href="http://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=68444&revisedVersion=10&originalVersion=9">View
Change</a>
              |
       <a href="http://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