CXF Schema Validation Example

CXF Schema Validation Example explains about How to validating wsdl/schema using CXF framework

How to enable schema validation on CXF SOAP requests and responses

How can I turn on schema validation for jaxws endpoint?‎

CXF is providing in built schema validation, so that we can define different rule on WSDL schema level and if the request/response is not satisfying according to the schema, an error is produced

It is very helpful because, we get error message very before than it need to validate on server side.

I am going to reuse  CXF Web Service Tutorial,Please see the project structure on below screenshot

CXF Schema Validation Example

Here we made only changes are inside cxf.xml we have added following tag for the CXF schema validation

<entry key="schema-validation-enabled" value="true" /> Also added wsdlLocation="WEB-INF/ChangeStudent.wsdl" in order to utilize contract first approach, please see the complete structure of cxf.xml below

Modify cxf.xml

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
	xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
	<import resource="classpath:META-INF/cxf/cxf.xml" />
	<jaxws:endpoint id="changeStudent"
		implementor="com.student.ChangeStudentDetailsImpl" address="/ChangeStudent"
		wsdlLocation="WEB-INF/ChangeStudent.wsdl">
		<jaxws:properties>
			<entry key="schema-validation-enabled" value="true" />
		</jaxws:properties>
	</jaxws:endpoint>
</beans>

Here I am showing an example where reqeust need to be validated that student name length must be less than 10. please check the student complexType extracted from WSDL below

<xs:complexType name="student">
	<xs:sequence>
		<xs:element name="name">
			<xsd:simpleType>
				<xsd:restriction base="xs:string">
					<xsd:maxLength value="10" />
				</xsd:restriction>
			</xsd:simpleType>
		</xs:element>
	</xs:sequence>
</xs:complexType>

please see the complete structure of ChangeStudent.wsdl below

ChangeStudent.wsdl

<?xml version="1.0" ?>
<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema"
	xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://student.com/"
	xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http"
	name="ChangeStudentDetailsImplService" targetNamespace="http://student.com/">
	<wsdl:types>
		<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
			xmlns:tns="http://student.com/" elementFormDefault="unqualified"
			targetNamespace="http://student.com/" version="1.0">
			<xs:element name="changeName" type="tns:changeName"></xs:element>
			<xs:element name="changeNameResponse" type="tns:changeNameResponse"></xs:element>
			<xs:complexType name="changeName">
				<xs:sequence>
					<xs:element minOccurs="0" name="arg0" type="tns:student"></xs:element>
				</xs:sequence>
			</xs:complexType>
			<xs:complexType name="student">
				<xs:sequence>
					<xs:element name="name">
						<xsd:simpleType>
							<xsd:restriction base="xs:string">
								<xsd:maxLength value="10" />
							</xsd:restriction>
						</xsd:simpleType>
					</xs:element>
				</xs:sequence>
			</xs:complexType>
			<xs:complexType name="changeNameResponse">
				<xs:sequence>
					<xs:element minOccurs="0" name="return" type="tns:student"></xs:element>
				</xs:sequence>
			</xs:complexType>
		</xs:schema>
	</wsdl:types>
	<wsdl:message name="changeName">
		<wsdl:part element="tns:changeName" name="parameters">
		</wsdl:part>
	</wsdl:message>
	<wsdl:message name="changeNameResponse">
		<wsdl:part element="tns:changeNameResponse" name="parameters">
		</wsdl:part>
	</wsdl:message>
	<wsdl:portType name="ChangeStudentDetails">
		<wsdl:operation name="changeName">
			<wsdl:input message="tns:changeName" name="changeName">
			</wsdl:input>
			<wsdl:output message="tns:changeNameResponse" name="changeNameResponse">
			</wsdl:output>
		</wsdl:operation>
	</wsdl:portType>
	<wsdl:binding name="ChangeStudentDetailsImplServiceSoapBinding"
		type="tns:ChangeStudentDetails">
		<soap:binding style="document"
			transport="http://schemas.xmlsoap.org/soap/http"></soap:binding>
		<wsdl:operation name="changeName">
			<soap:operation soapAction="" style="document"></soap:operation>
			<wsdl:input name="changeName">
				<soap:body use="literal"></soap:body>
			</wsdl:input>
			<wsdl:output name="changeNameResponse">
				<soap:body use="literal"></soap:body>
			</wsdl:output>
		</wsdl:operation>
	</wsdl:binding>
	<wsdl:service name="ChangeStudentDetailsImplService">
		<wsdl:port binding="tns:ChangeStudentDetailsImplServiceSoapBinding"
			name="ChangeStudentDetailsImplPort">
			<soap:address location="http://localhost:8080/CXFTutorial/ChangeStudent"></soap:address>
		</wsdl:port>
	</wsdl:service>
</wsdl:definitions>

Now deploy the project and run the below client and see the output

You need CXF client example in order to run this service

Output

Here we are sending Student name as 'Rockey', response will be 'Hello Rockey' is more than 10 character long, so we get the exception regarding length is not satisfied according to given WSDL

WARNING: Interceptor for
{http://student.com/}ChangeStudentDetailsImplService#{http://student.com/}changeName has thrown exception, 
unwinding now org.apache.cxf.interceptor.Fault:Marshalling Error: cvc-maxLength-valid: Value 'Hello Rockey' 
with length = '12' is not facet-valid with respect to maxLength '10' for type '#AnonType_namestudent'.

 

If we are sending Student name as 'Ramu', we get correct response as 'Hello Ramu' because the length of name is satisfied now.

 











Your email address will not be published. Required fields are marked *