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:52: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>edited</b> by             <a href="http://cwiki.apache.org/confluence/display/~muellerc">Christian
Mueller</a>
    </h4>
     
          <br/>
     <div class="notificationGreySide">
         <h2><a name="BeanValidation-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="BeanValidation-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="BeanValidation-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="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">
<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>

<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">
<script type="syntaxhighlighter" class="toolbar: false; theme: default; brush: java; gutter:
false"><![CDATA[
&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;validation-config
	xmlns="http://jboss.org/xml/ns/javax/validation/configuration"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://jboss.org/xml/ns/javax/validation/configuration"&gt;
	&lt;default-provider&gt;org.hibernate.validator.HibernateValidator&lt;/default-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;
]]></script>
</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">
<script type="syntaxhighlighter" class="toolbar: false; theme: default; brush: java; gutter:
false"><![CDATA[
&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;constraint-mappings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://jboss.org/xml/ns/javax/validation/mapping validation-mapping-1.0.xsd"
	xmlns="http://jboss.org/xml/ns/javax/validation/mapping"&gt;
	&lt;default-package&gt;org.apache.camel.component.bean.validator&lt;/default-package&gt;
	
	&lt;bean class="CarWithoutAnnotations" ignore-annotations="true"&gt;
		&lt;field name="manufacturer"&gt;
			&lt;constraint annotation="javax.validation.constraints.NotNull" /&gt;
		&lt;/field&gt;
		
		&lt;field name="licensePlate"&gt;
			&lt;constraint annotation="javax.validation.constraints.NotNull" /&gt;
			
			&lt;constraint annotation="javax.validation.constraints.Size"&gt;
				&lt;groups&gt;
					&lt;value&gt;org.apache.camel.component.bean.validator.OptionalChecks&lt;/value&gt;
				&lt;/groups&gt;
				&lt;element name="min"&gt;5&lt;/element&gt;
				&lt;element name="max"&gt;14&lt;/element&gt;
			&lt;/constraint&gt;
		&lt;/field&gt;
	&lt;/bean&gt;
&lt;/constraint-mappings&gt;
]]></script>
</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="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/pages/diffpagesbyversion.action?pageId=17268887&revisedVersion=3&originalVersion=2">View
Change</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