camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > Bean Validation
Date Fri, 15 Oct 2010 07:13:00 GMT
<html>
<head>
    <base href="https://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/1810/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/Bean+Validation">Bean
Validation</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~joed">Johan
Edstrom</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" >{div} <br> <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">h3.
ServiceMix4/OSGi Deployment. <br> <br>The bean-validator when deployed in an OSGi
environment requires a little help to accommodate the resource loading specified in JSR303,
this was fixed in Servicemix-Specs 1.6-SNAPSHOT. <br> <br></td></tr>
            <tr><td class="diff-unchanged" >h3. Example <br> <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
        </table>
</div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h2><a name="BeanValidation-BeanValidationComponent"></a>Bean Validation
Component</h2>

<p><b>Available as of Camel 2.3</b></p>

<p>The Validation component performs bean validation of the message body using the Java
Bean Validation API (<a href="http://jcp.org/en/jsr/detail?id=303" class="external-link"
rel="nofollow">JSR 303</a>). Camel uses the reference implementation, which is <a
href="http://docs.jboss.org/hibernate/stable/validator/reference/en/html_single/" class="external-link"
rel="nofollow">Hibernate Validator</a>.</p>

<p>Maven users will need to add the following dependency to their <tt>pom.xml</tt>
for this component:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;dependency&gt;</span>
    <span class="code-tag">&lt;groupId&gt;</span>org.apache.camel<span
class="code-tag">&lt;/groupId&gt;</span>
    <span class="code-tag">&lt;artifactId&gt;</span>camel-bean-validator<span
class="code-tag">&lt;/artifactId&gt;</span>
    <span class="code-tag">&lt;version&gt;</span>x.x.x<span class="code-tag">&lt;/version&gt;</span>
    <span class="code-tag"><span class="code-comment">&lt;!-- use the same
version as your Camel core version --&gt;</span></span>
<span class="code-tag">&lt;/dependency&gt;</span>
</pre>
</div></div>


<h3><a name="BeanValidation-URIformat"></a>URI format</h3>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
bean-validator:something[?options]
</pre>
</div></div>

<p>or</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
bean-validator:<span class="code-comment">//something[?options]</span>
</pre>
</div></div>

<p>Where <b>something</b> must be present to provide a valid url<br/>
You can append query options to the URI in the following format, ?option=value&amp;option=value&amp;...</p>

<h3><a name="BeanValidation-URIOptions"></a>URI Options</h3>
<div class="confluenceTableSmall"><div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Option </th>
<th class='confluenceTh'> Default </th>
<th class='confluenceTh'> Description </th>
</tr>
<tr>
<td class='confluenceTd'> <tt>group</tt> </td>
<td class='confluenceTd'> <tt>javax.validation.groups.Default</tt> </td>
<td class='confluenceTd'> The custom validation group to use. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>messageInterpolator</tt> </td>
<td class='confluenceTd'> <tt>org.hibernate.validator.engine.<br class="atl-forced-newline"
/>ResourceBundleMessageInterpolator</tt> </td>
<td class='confluenceTd'> Reference to a custom <tt>javax.validation.MessageInterpolator</tt>
in the <a href="/confluence/display/CAMEL/Registry" title="Registry">Registry</a>.
</td>
</tr>
<tr>
<td class='confluenceTd'> <tt>traversableResolver</tt> </td>
<td class='confluenceTd'> <tt>org.hibernate.validator.engine.resolver.<br class="atl-forced-newline"
/>DefaultTraversableResolver</tt> </td>
<td class='confluenceTd'> Reference to a custom <tt>javax.validation.TraversableResolver</tt>
in the <a href="/confluence/display/CAMEL/Registry" title="Registry">Registry</a>.
</td>
</tr>
<tr>
<td class='confluenceTd'> <tt>constraintValidatorFactory</tt> </td>
<td class='confluenceTd'> <tt>org.hibernate.validator.engine.<br class="atl-forced-newline"
/>ConstraintValidatorFactoryImpl</tt> </td>
<td class='confluenceTd'> Reference to a custom <tt>javax.validation.ConstraintValidatorFactory</tt>
in the <a href="/confluence/display/CAMEL/Registry" title="Registry">Registry</a>.
</td>
</tr>
</tbody></table>
</div>
</div>

<h3><a name="BeanValidation-ServiceMix4%2FOSGiDeployment."></a>ServiceMix4/OSGi
Deployment.</h3>

<p>The bean-validator when deployed in an OSGi environment requires a little help to
accommodate the resource loading specified in JSR303, this was fixed in Servicemix-Specs 1.6-SNAPSHOT.</p>

<h3><a name="BeanValidation-Example"></a>Example</h3>

<p>Assumed we have a java bean with the following annotations</p>

<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader"
style="border-bottom-width: 1px;"><b>Car.java</b></div><div class="codeContent
panelContent">
<pre class="code-java">
<span class="code-keyword">public</span> class Car {

    @NotNull
    <span class="code-keyword">private</span> <span class="code-object">String</span>
manufacturer;

    @NotNull
    @Size(min = 5, max = 14, groups = OptionalChecks.class)
    <span class="code-keyword">private</span> <span class="code-object">String</span>
licensePlate;
    
    <span class="code-comment">// getter and setter
</span>}
</pre>
</div></div>

<p>and an interface definition for our custom validation group</p>

<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader"
style="border-bottom-width: 1px;"><b>OptionalChecks.java</b></div><div
class="codeContent panelContent">
<pre class="code-java">
<span class="code-keyword">public</span> <span class="code-keyword">interface</span>
OptionalChecks {
}
</pre>
</div></div>

<p>with the following Camel route, only the <b>@NotNull</b> constraints
on the attributes manufacturer and licensePlate will be validated (Camel uses the default
group <tt>javax.validation.groups.Default</tt>).</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"direct:start"</span>)
.to(<span class="code-quote">"bean-validator:<span class="code-comment">//x"</span>)
</span>.to(<span class="code-quote">"mock:end"</span>)
</pre>
</div></div>

<p>If you want to check the constraints from the group <tt>OptionalChecks</tt>,
you have to define the route like this</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"direct:start"</span>)
.to(<span class="code-quote">"bean-validator:<span class="code-comment">//x?group=OptionalChecks"</span>)
</span>.to(<span class="code-quote">"mock:end"</span>)
</pre>
</div></div>

<p>If you want to check the constraints from both groups, you have to define a new interface
first</p>

<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader"
style="border-bottom-width: 1px;"><b>AllChecks.java</b></div><div
class="codeContent panelContent">
<pre class="code-java">
@GroupSequence({Default.class, OptionalChecks.class})
<span class="code-keyword">public</span> <span class="code-keyword">interface</span>
AllChecks {
}
</pre>
</div></div>

<p>and then your route definition should looks like this</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
from(<span class="code-quote">"direct:start"</span>)
.to(<span class="code-quote">"bean-validator:<span class="code-comment">//x?group=AllChecks"</span>)
</span>.to(<span class="code-quote">"mock:end"</span>)
</pre>
</div></div>

<p>And if you have to provide your own message interpolator, traversable resolver and
constraint validator factory, you have to write a route like this</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
&lt;bean id=<span class="code-quote">"myMessageInterpolator"</span> class=<span
class="code-quote">"my.ConstraintValidatorFactory"</span> /&gt;
&lt;bean id=<span class="code-quote">"myTraversableResolver"</span> class=<span
class="code-quote">"my.TraversableResolver"</span> /&gt;
&lt;bean id=<span class="code-quote">"myConstraintValidatorFactory"</span>
class=<span class="code-quote">"my.ConstraintValidatorFactory"</span> /&gt;

from(<span class="code-quote">"direct:start"</span>)
.to(<span class="code-quote">"bean-validator:<span class="code-comment">//x?group=AllChecks&amp;messageInterpolator=#myMessageInterpolator&amp;traversableResolver=#myTraversableResolver&amp;constraintValidatorFactory=#myConstraintValidatorFactory"</span>)
</span>.to(<span class="code-quote">"mock:end"</span>)
</pre>
</div></div>

<p>It's also possible to describe your constraints as XML and not as Java annotations.
In this case, you have to provide the file <tt>META-INF/validation.xml</tt> which
could looks like this</p>

<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader"
style="border-bottom-width: 1px;"><b>validation.xml</b></div><div
class="codeContent panelContent">
<pre class="code-java">
&lt;?xml version=<span class="code-quote">"1.0"</span> encoding=<span class="code-quote">"UTF-8"</span>?&gt;
&lt;validation-config
	xmlns=<span class="code-quote">"http:<span class="code-comment">//jboss.org/xml/ns/javax/validation/configuration"</span>
</span>	xmlns:xsi=<span class="code-quote">"http:<span class="code-comment">//www.w3.org/2001/XMLSchema-instance"</span>
</span>	xsi:schemaLocation=<span class="code-quote">"http:<span class="code-comment">//jboss.org/xml/ns/javax/validation/configuration"</span>&gt;
</span>	&lt;<span class="code-keyword">default</span>-provider&gt;org.hibernate.validator.HibernateValidator&lt;/<span
class="code-keyword">default</span>-provider&gt;
	&lt;message-interpolator&gt;org.hibernate.validator.engine.ResourceBundleMessageInterpolator&lt;/message-interpolator&gt;
	&lt;traversable-resolver&gt;org.hibernate.validator.engine.resolver.DefaultTraversableResolver&lt;/traversable-resolver&gt;
	&lt;constraint-validator-factory&gt;org.hibernate.validator.engine.ConstraintValidatorFactoryImpl&lt;/constraint-validator-factory&gt;
	
	&lt;constraint-mapping&gt;/constraints-car.xml&lt;/constraint-mapping&gt;
&lt;/validation-config&gt;
</pre>
</div></div>

<p>and the <tt>constraints-car.xml</tt> file</p>

<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader"
style="border-bottom-width: 1px;"><b>constraints-car.xml</b></div><div
class="codeContent panelContent">
<pre class="code-java">
&lt;?xml version=<span class="code-quote">"1.0"</span> encoding=<span class="code-quote">"UTF-8"</span>?&gt;
&lt;constraint-mappings xmlns:xsi=<span class="code-quote">"http:<span class="code-comment">//www.w3.org/2001/XMLSchema-instance"</span>
</span>	xsi:schemaLocation=<span class="code-quote">"http:<span class="code-comment">//jboss.org/xml/ns/javax/validation/mapping
validation-mapping-1.0.xsd"</span>
</span>	xmlns=<span class="code-quote">"http:<span class="code-comment">//jboss.org/xml/ns/javax/validation/mapping"</span>&gt;
</span>	&lt;<span class="code-keyword">default</span>-<span class="code-keyword">package</span>&gt;org.apache.camel.component.bean.validator&lt;/<span
class="code-keyword">default</span>-<span class="code-keyword">package</span>&gt;
	
	&lt;bean class=<span class="code-quote">"CarWithoutAnnotations"</span> ignore-annotations=<span
class="code-quote">"<span class="code-keyword">true</span>"</span>&gt;
		&lt;field name=<span class="code-quote">"manufacturer"</span>&gt;
			&lt;constraint annotation=<span class="code-quote">"javax.validation.constraints.NotNull"</span>
/&gt;
		&lt;/field&gt;
		
		&lt;field name=<span class="code-quote">"licensePlate"</span>&gt;
			&lt;constraint annotation=<span class="code-quote">"javax.validation.constraints.NotNull"</span>
/&gt;
			
			&lt;constraint annotation=<span class="code-quote">"javax.validation.constraints.Size"</span>&gt;
				&lt;groups&gt;
					&lt;value&gt;org.apache.camel.component.bean.validator.OptionalChecks&lt;/value&gt;
				&lt;/groups&gt;
				&lt;element name=<span class="code-quote">"min"</span>&gt;5&lt;/element&gt;
				&lt;element name=<span class="code-quote">"max"</span>&gt;14&lt;/element&gt;
			&lt;/constraint&gt;
		&lt;/field&gt;
	&lt;/bean&gt;
&lt;/constraint-mappings&gt;
</pre>
</div></div>


<h3><a name="BeanValidation-SeeAlso"></a>See Also</h3>
<ul>
	<li><a href="/confluence/display/CAMEL/Configuring+Camel" title="Configuring Camel">Configuring
Camel</a></li>
	<li><a href="/confluence/display/CAMEL/Component" title="Component">Component</a></li>
	<li><a href="/confluence/display/CAMEL/Endpoint" title="Endpoint">Endpoint</a></li>
	<li><a href="/confluence/display/CAMEL/Getting+Started" title="Getting Started">Getting
Started</a></li>
</ul>

    </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/Bean+Validation">View
Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=17268887&revisedVersion=8&originalVersion=7">View
Changes</a>
                |
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/Bean+Validation?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message