0

我正在尝试通过 xsds 验证传入的 jsons。我暴露了几个服务,每个服务都有不同的 JSON,每个服务都映射到不同的 POJO。所以我写了一个主xsd,它将包含JSON的所有根元素作为子元素,并将导入相应的xsd。

现在的问题是我总是得到这个异常:

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>
4

1 回答 1

0

您首先要检查的是名称空间或您的 XML 文档元素。您的错误指向一个不合格的名称,而您的 XSD 使用 RequestBean 的目标名称空间。

于 2012-05-31T15:01:17.237 回答