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 Thu, 15 Apr 2010 20:14:00 GMT
<html>
<head>
    <base href="http://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/1520/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/bean-validation">bean-validation</a></h2>
    <h4>Page  <b>added</b> by             <a href="http://cwiki.apache.org/confluence/display/~muellerc">Christian
Mueller</a>
    </h4>
         <br/>
    <div class="notificationGreySide">
         <h2><a name="bean-validation-BeanValidationComponentCamel2.3onwards"></a>Bean
Validation Component - <b>Camel 2.3 onwards</b></h2>

<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" 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/"
rel="nofollow">Hibernate Validator</a>.</p>

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

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<script type="syntaxhighlighter" class="toolbar: false; theme: default; brush: java; gutter:
false"><![CDATA[
bean-validator:something
]]></script>
</div></div>

<p>or</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<script type="syntaxhighlighter" class="toolbar: false; theme: default; brush: java; gutter:
false"><![CDATA[
bean-validator://something
]]></script>
</div></div>

<div class='panelMacro'><table class='infoMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/information.gif" width="16"
height="16" align="absmiddle" alt="" border="0"></td><td><p>Where <b>something</b>
must be present to provide a valid url, but could be any string. It's a bit ugly, but it works.
<img class="emoticon" src="/confluence/images/icons/emoticons/smile.gif" height="20" width="20"
align="absmiddle" alt="" border="0"/></p></td></tr></table></div>

<p>You can append query options to the URI in the following format, ?option=value&amp;option=value&amp;...</p>

<h3><a name="bean-validation-URIOptions"></a>URI Options</h3>

<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.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.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.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>

<h3><a name="bean-validation-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">
<script type="syntaxhighlighter" class="toolbar: false; theme: default; brush: java; gutter:
false"><![CDATA[
public class Car {

    @NotNull
    private String manufacturer;

    @NotNull
    @Size(min = 5, max = 14, groups = OptionalChecks.class)
    private String licensePlate;
    
    // getter and setter
}
]]></script>
</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">
<script type="syntaxhighlighter" class="toolbar: false; theme: default; brush: java; gutter:
false"><![CDATA[
public interface OptionalChecks {
}
]]></script>
</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">
<script type="syntaxhighlighter" class="toolbar: false; theme: default; brush: java; gutter:
false"><![CDATA[
from("direct:start")
.to("bean-validator://x")
.to("mock:end")
]]></script>
</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">
<script type="syntaxhighlighter" class="toolbar: false; theme: default; brush: java; gutter:
false"><![CDATA[
from("direct:start")
.to("bean-validator://x?group=OptionalChecks")
.to("mock:end")
]]></script>
</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">
<script type="syntaxhighlighter" class="toolbar: false; theme: default; brush: java; gutter:
false"><![CDATA[
@GroupSequence({Default.class, OptionalChecks.class})
public interface AllChecks {
}
]]></script>
</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">
<script type="syntaxhighlighter" class="toolbar: false; theme: default; brush: java; gutter:
false"><![CDATA[
from("direct:start")
.to("bean-validator://x?group=AllChecks")
.to("mock:end")
]]></script>
</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">
<script type="syntaxhighlighter" class="toolbar: false; theme: default; brush: java; gutter:
false"><![CDATA[
&lt;bean id="myMessageInterpolator" class="my.ConstraintValidatorFactory" /&gt;
&lt;bean id="myTraversableResolver" class="my.TraversableResolver" /&gt;
&lt;bean id="myConstraintValidatorFactory" class="my.ConstraintValidatorFactory" /&gt;

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


<h3><a name="bean-validation-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="http://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
       </div>
       <a href="http://cwiki.apache.org/confluence/display/CAMEL/bean-validation">View
Online</a>
              |
       <a href="http://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