validation
TRANSCRIPT
Validation
Abstract
• The main motto of this PPT is How to use Validation in our applications.
Introduction
• The Validations module provides an easy out-of-the-box way to verify that the content of a message in your flow matches a given set of criteria. The main advantage this has over using Filters is traceability, as filters all raise identical exceptions, making it hard for you to know where the exception was caused. Validators, on the other hand, raise an exception with a meaningful message attached. You can optionally customize this message and even the type of exception you want it to throw.
Example
.mflow• <?xml version="1.0" encoding="UTF-8"?>
• <mule xmlns:validation="http://www.mulesoft.org/schema/mule/validation" xmlns:context="http://www.springframework.org/schema/context" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
• xmlns:spring="http://www.springframework.org/schema/beans" • xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"• xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-current.xsd• http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd• http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd• http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-
current.xsd• http://www.mulesoft.org/schema/mule/validation http://www.mulesoft.org/schema/mule/validation/current/mule-
validation.xsd">• <http:listener-config name="HTTP_Listener_Configuration" host="${host}" port="${port}" doc:name="HTTP Listener
Configuration"/>• <context:property-placeholder location="myfile-${env}.properties"/>• <flow name="PropertyPlaceHolderFlow">• <http:listener config-ref="HTTP_Listener_Configuration" path="/" doc:name="HTTP"/>• <validation:is-not-empty value="#[message.inboundProperties.'http.query.params'.name]" doc:name="Validation"/>• <logger message="--Hello" level="INFO" doc:name="Logger"/>• </flow>• </mule>
• Output:
• ERROR 2016-12-21 11:36:25,918 [[PropertyPlaceHolder].HTTP_Listener_Configuration.worker.01] org.mule.exception.DefaultMessagingExceptionStrategy:
• ********************************************************************************
• Message : value is null• Payload : {NullPayload}• Payload Type : org.mule.transport.NullPayload• Element : /PropertyPlaceHolderFlow/processors/0 @ PropertyPlaceHolder• --------------------------------------------------------------------------------• Root Exception stack trace:• org.mule.extension.validation.api.ValidationException: value is null• at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)• at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)• at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)• at java.lang.reflect.Constructor.newInstance(Unknown Source)
• Flow of execution:
1. URL to trigger the service from browserhttp://localhost:8086 – Failure http://localhost:8086?name=hi - Success
Note:1. Create myfile-DEV.proerties file in src/main/resources folder2. Specify the env name in mule-app.properties file
References
• https://docs.mulesoft.com/mule-user-guide/v/3.7/validations-module