I am trying to validate the incoming jsons by xsds. I had several services exposed and every service had different JSON each mapped to different POJOs. So i wrote a master xsd which would contain all root elements of JSONs as sub elements and will import the respective xsds.
Now the problem is that I always get this exception:
WARNING: javax.xml.bind.UnmarshalException
- with linked exception:
Caused by: org.xml.sax.SAXParseException: cvc-elt.1: Cannot find the declaration of element 'RequestBean'.
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
at com.sun.org.apache.xerces.internal.util.ErrorHandlerWrapper.error(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLErrorReporter.reportError(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.xs.XMLSchemaValidator.handleStartElement
My xsd is :
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/RequestBean"
xmlns:tns="http://www.example.org/RequestBean" elementFormDefault="unqualified"
xmlns:poi="http://www.example.org/PoiRequest" xmlns:sendcar="http://www.example.org/SendCar">
<xs:import namespace="http://www.example.org/PoiRequest"
schemaLocation="PoiRequest.xsd"/>
<xs:import namespace="http://www.example.org/SendCar"
schemaLocation="SendCar.xsd"/>
<xs:element name="RequestBean">
<xs:complexType>
<xs:all>
<xs:element name="PoiRequest" type="poi:PoiRequest" minOccurs="0"/>
<xs:element name="SendCar" type="sendcar:SendCar" minOccurs="0"/>
</xs:all>
</xs:complexType>
</xs:element>
</xs:schema>
My config is below:
<jaxrs:server id="restContainer" address="/">
<jaxrs:serviceBeans>
<ref bean="PoiSearch" />
<ref bean="SendCar" />
</jaxrs:serviceBeans>
<jaxrs:providers>
<bean class="org.apache.cxf.jaxrs.provider.json.JSONProvider">
<property name="dropRootElement" value="true" />
<property name="supportUnwrapped" value="true" /> </bean>
</jaxrs:providers>
<jaxrs:schemaLocations>
<jaxrs:schemaLocation>file:C:\Desktop\xsds\RequestBean.xsd</jaxrs:schemaLocation> <jaxrs:schemaLocation>file:C:\Desktop\xsds\PoiRequest.xsd</jaxrs:schemaLocation>
<jaxrs:schemaLocation>file:C:\Desktop\xsds\Route.xsd</jaxrs:schemaLocation>
<jaxrs:schemaLocation>file:C:\Desktop\xsds\Poi.xsd</jaxrs:schemaLocation>
<jaxrs:schemaLocation>file:C:\Desktop\xsds\SendCar.xsd</jaxrs:schemaLocation>
</jaxrs:schemaLocations>
</jaxrs:server>
Your first thing to check is the namespace or your XML document element. Your error points to an unqualified name, whereas your XSD uses a target namespace for RequestBean.