我正在尝试通过xsds验证传入的jsons。我暴露了几个服务,每个服务都有不同的JSON,每个都映射到不同的POJO。所以我写了一个主xsd,它将包含JSON的所有根元素作为子元素,并将导入相应的xsds。
现在的问题是我总是遇到这个例外:
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
我的xsd是:
<?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>
我的配置如下:
<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>
答案 0 :(得分:0)
首先要检查的是命名空间或XML文档元素。您的错误指向一个非限定名称,而您的XSD使用RequestBean的目标命名空间。