1

I implemented a working web service using CXF (2.7.1) with a WSDL & XSD that include, among other things, the following type:

<xs:simpleType name="SimpleIdType">
  <xs:restriction base="xs:string">
    <xs:pattern value="[A-Za-z0-9:\.\-]{20}"/>
  </xs:restriction>
</xs:simpleType>

I interpret this to be: Accept only 20 character strings which only contain alphanumeric characaters and ':', '.' and '-'.

When I send a SOAP message to my web service with the corresponding element containing FAAAAAAAAAAAAAAAAAAA, the service of course accepts properly without any error.

However, if I send an identical SOAP message with the # instead of F (i.e. #AAAAAAAAAAAAAAAAAAA), the service still accepts the message, without issuing any validation error (unmarshalling or otherwise).

Why?

Isn't the default ValidationEventHandler supposed to handle that by throwing an "Unmarshalling Error"?

Withheld
  • 4,603
  • 10
  • 45
  • 76

2 Answers2

1

The JAXB model (generated or hand coded) does not contain all the metadata from the XML schema in its annotations. If you want to validate against all aspects of the schema you can enable this be specifying an instance of Schema on the Unmarshaller.

bdoughan
  • 147,609
  • 23
  • 300
  • 400
  • Wow. +1 already. I am going to read your article thoroughly and let you know if that worked for me. Do I understand correctly that this implies also setting my own [ValidationEventHandler](https://jaxb.java.net/jaxb20-ed/api/javax/xml/bind/ValidationEventHandler.html)? – Withheld Oct 03 '13 at 20:40
  • OK, I tried to implement your solution but I have no `main()` in [my CXF-generated code](http://stackoverflow.com/q/19184721/1864054). [Where](http://stackoverflow.com/q/19184721/1864054) do I hook the Schema and Unmarshaller code? – Withheld Oct 04 '13 at 16:37
  • Looks like I am approaching this the wrong way (because CXF is involved): According to [this](http://svn.apache.org/repos/asf/cxf/trunk/distribution/src/main/release/samples/wsdl_first_xmlbeans/), the presence of the cxf.xml configuration file on the classpath (and its content) change CXF's default behavior that message parameters would not be validated. This is confusing because I can definitely get an "unmarshaling error" (by default!) if I send a "?" instead of an `xs:timestamp`... What am I missing? – Withheld Oct 04 '13 at 16:57
0

I finally found the correct answer for this CXF-based case.

CXF already has runtime schema validation built-in. It is named schema validation via configuration and the only thing that was missing in my code was the XML to enable it, inside the already existing <jaxws:endpoint element in beans.xml AKA application-context.xml:

<jaxws:properties>
    <entry key="schema-validation-enabled" value="true" />
</jaxws:properties>

This discovery was made possible thanks to the answer by @Patrick.

Community
  • 1
  • 1
Withheld
  • 4,603
  • 10
  • 45
  • 76