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 Sun, 19 Jul 2009 06:00: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/~christian%2Bschneider">Christian
Schneider</a>
    </h4>
     
          <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>

<style type="text/css">
@import url(/confluence/download/resources/confluence.ext.code:code/shStyles.css);
</style>
<!--[if IE]>
<style type="text/css">
    .code textarea, .code input { padding: 0 !important; }
</style>
<![endif]-->
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shCore.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushCSharp.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushPhp.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushJScript.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushVb.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushSql.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushXml.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushShell.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushDelphi.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushPython.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushJava.js"></script>
<div class="code">
<textarea name="newcodemacro" class="java:nocontrols:nogutter" rows="10" readonly="readonly">DataFormat
jaxb = new JaxbDataFormat("com.acme.model");

from("activemq:My.Queue").
  unmarshal(jaxb).
  to("mqseries:Another.Queue");</textarea>
<script class="javascript">
    if(!window.newcodemacro_initialised)
    {
        window.newcodemacro_initialised = true;
        window.oldonloadmethod = window.onload;
        window.onload = function(){
            dp.SyntaxHighlighter.HighlightAll('newcodemacro');
            if(window.oldonloadmethod)
            {
                window.oldonloadmethod();
            }
        }
    }

</script>
</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">
<textarea name="newcodemacro" class="java:nocontrols:nogutter" rows="10" readonly="readonly">from("activemq:My.Queue").
  unmarshal("myJaxbDataType").
  to("mqseries:Another.Queue");</textarea>
<script class="javascript">
    if(!window.newcodemacro_initialised)
    {
        window.newcodemacro_initialised = true;
        window.oldonloadmethod = window.onload;
        window.onload = function(){
            dp.SyntaxHighlighter.HighlightAll('newcodemacro');
            if(window.oldonloadmethod)
            {
                window.oldonloadmethod();
            }
        }
    }

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

<style type="text/css">
@import url(/confluence/download/resources/confluence.ext.code:code/shStyles.css);
</style>
<!--[if IE]>
<style type="text/css">
    .code textarea, .code input { padding: 0 !important; }
</style>
<![endif]-->
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shCore.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushCSharp.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushPhp.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushJScript.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushVb.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushSql.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushXml.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushShell.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushDelphi.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushPython.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushJava.js"></script>
<div class="code">
<textarea name="newcodemacro" class="xml:nocontrols:nogutter" rows="10" readonly="readonly">&lt;camelContext
id="camel" xmlns="http://camel.apache.org/schema/spring"&gt;
  &lt;route&gt;
    &lt;from uri="direct:start"/&gt;
    &lt;unmarshal&gt;
      &lt;jaxb prettyPrint="true" contextPath="org.apache.camel.example"/&gt;
    &lt;/unmarshal&gt;
    &lt;to uri="mock:result"/&gt;
  &lt;/route&gt;
&lt;/camelContext&gt;</textarea>
<script class="javascript">
    if(!window.newcodemacro_initialised)
    {
        window.newcodemacro_initialised = true;
        window.oldonloadmethod = window.onload;
        window.onload = function(){
            dp.SyntaxHighlighter.HighlightAll('newcodemacro');
            if(window.oldonloadmethod)
            {
                window.oldonloadmethod();
            }
        }
    }

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

<style type="text/css">
@import url(/confluence/download/resources/confluence.ext.code:code/shStyles.css);
</style>
<!--[if IE]>
<style type="text/css">
    .code textarea, .code input { padding: 0 !important; }
</style>
<![endif]-->
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shCore.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushCSharp.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushPhp.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushJScript.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushVb.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushSql.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushXml.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushShell.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushDelphi.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushPython.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushJava.js"></script>
<div class="code">
<textarea name="newcodemacro" class="xml:nocontrols:nogutter" rows="10" readonly="readonly">&lt;camelContext
id="camel" xmlns="http://camel.apache.org/schema/spring"&gt;
  &lt;dataFormats&gt;
    &lt;jaxb id="myJaxb" prettyPrint="true" contextPath="org.apache.camel.example"/&gt;
  &lt;/dataFormats&gt;

  &lt;route&gt;
    &lt;from uri="direct:start"/&gt;
    &lt;marshal ref="myJaxb"/&gt;
    &lt;to uri="direct:marshalled"/&gt;
  &lt;/route&gt;
  &lt;route&gt;
    &lt;from uri="direct:marshalled"/&gt;
    &lt;unmarshal ref="myJaxb"/&gt;
    &lt;to uri="mock:result"/&gt;
  &lt;/route&gt;

&lt;/camelContext&gt;</textarea>
<script class="javascript">
    if(!window.newcodemacro_initialised)
    {
        window.newcodemacro_initialised = true;
        window.oldonloadmethod = window.onload;
        window.onload = function(){
            dp.SyntaxHighlighter.HighlightAll('newcodemacro');
            if(window.oldonloadmethod)
            {
                window.oldonloadmethod();
            }
        }
    }

</script>
</div>



<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>
<style type="text/css">
@import url(/confluence/download/resources/confluence.ext.code:code/shStyles.css);
</style>
<!--[if IE]>
<style type="text/css">
    .code textarea, .code input { padding: 0 !important; }
</style>
<![endif]-->
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shCore.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushCSharp.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushPhp.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushJScript.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushVb.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushSql.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushXml.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushShell.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushDelphi.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushPython.js"></script>
<script class="javascript" src="/confluence/download/resources/confluence.ext.code:code/shBrushJava.js"></script>
<div class="code">
<textarea name="newcodemacro" class="xml:nocontrols:nogutter" rows="10" readonly="readonly">&lt;camelContext
id="camel" xmlns="http://camel.apache.org/schema/spring"&gt;
    &lt;route&gt;
        &lt;from uri="direct:start"/&gt;
        &lt;marshal&gt;
            &lt;jaxb prettyPrint="false" encoding="iso-8859-1" contextPath="org.apache.camel.example"/&gt;
        &lt;/marshal&gt;
        &lt;to uri="mock:result"/&gt;
    &lt;/route&gt;
&lt;/camelContext&gt;</textarea>
<script class="javascript">
    if(!window.newcodemacro_initialised)
    {
        window.newcodemacro_initialised = true;
        window.oldonloadmethod = window.onload;
        window.onload = function(){
            dp.SyntaxHighlighter.HighlightAll('newcodemacro');
            if(window.oldonloadmethod)
            {
                window.oldonloadmethod();
            }
        }
    }

</script>
</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">
<textarea name="newcodemacro" class="java:nocontrols:nogutter" rows="10" readonly="readonly">&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;</textarea>
<script class="javascript">
    if(!window.newcodemacro_initialised)
    {
        window.newcodemacro_initialised = true;
        window.oldonloadmethod = window.onload;
        window.onload = function(){
            dp.SyntaxHighlighter.HighlightAll('newcodemacro');
            if(window.oldonloadmethod)
            {
                window.oldonloadmethod();
            }
        }
    }

</script>
</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=9&originalVersion=8">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