使用SOAPHandler验证SOAP-Request

时间:2014-12-16 17:10:20

标签: validation xsd wsdl xml-namespaces soaphandler

我尝试针对WSDL中定义的模式验证SOAP-Request。我使用SOAPHandler和SOAP-Handler-Chain。它的唯一问题是,当我验证请求时,我收到此错误消息:

cvc-complex-type.2.4.a: Invalid content was found starting with element 'v1:latitude'. One of '{"http://schemas.domain.com/wsdl/fuelprice/v1/model":latitude}' is expected.

WSDL看起来像这样:

<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
      xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
      mlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
      mlns:http="http://schemas.xmlsoap.org/wsdl/http/"
      xmlns:xs="http://www.w3.org/2001/XMLSchema"
      xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
      xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:tns="http://schemas.domain.com/wsdl/fuelprice/v1"
      xmlns:model="http://schemas.domain.com/wsdl/fuelprice/v1/model"
      xmlns:exception="http://schemas.domain.com/wsdl/fuelprice/v1/exception"
      targetNamespace="http://schemas.domain.com/wsdl/fuelprice/v1">
<wsdl:types>
    <xsi:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema" targetNamespace="http://schemas.domain.com/wsdl/fuelprice/v1" elementFormDefault="qualified">
        <xsi:import namespace="http://schemas.domain.com/wsdl/fuelprice/v1/model" schemaLocation="common.xsd"/>
        <xsi:import namespace="http://schemas.domain.com/wsdl/fuelprice/v1/exception" schemaLocation="exception.xsd"/>
        <xsi:import namespace="http://schemas.xmlsoap.org/soap/envelope/" schemaLocation="http://schemas.xmlsoap.org/soap/envelope/"/>
        <xsi:element name="GetAreaFuelStationsRequest">
            <xsi:complexType>
                <xsi:sequence>
                    <xsi:element name="area" type="model:Area"/>
                </xsi:sequence>
                <xsi:attribute name="provider" type="xsi:string" use="optional"/>
                <xsi:attribute name="prices" type="xsi:boolean" use="required"/>
            </xsi:complexType>
        </xsi:element>

XSD:

<?xml version="1.0" encoding="UTF-8"?>
<xsi:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema" xmlns:model="http://schemas.domain.com/wsdl/fuelprice/v1/model" targetNamespace="http://schemas.domain.com/wsdl/fuelprice/v1/model" elementFormDefault="qualified">
<xsi:complexType name="FuelStation">
    <xsi:complexType name="Area">
        <xsi:sequence>
            <xsi:element name="center" type="model:GeoLocation"/>
            <xsi:element name="radius" type="xsi:float"/>
        </xsi:sequence>
    </xsi:complexType>
    <xsi:complexType name="GeoLocation">
        <xsi:sequence>
            <xsi:element name="latitude" type="xsi:double"/>
            <xsi:element name="longitude" type="xsi:double"/>
        </xsi:sequence>
        <xsi:attribute name="ellipsoid" type="xsi:string" use="required"/>
    </xsi:complexType>

源代码片段:

    try {
        context.getMessage().getSOAPPart().getEnvelope();
        schemas = getSchemas(new URL(wsdlURI.toString().concat(wsdl)));
        schema = factory.newSchema(schemas);
        validator = schema.newValidator();
        validator.validate(new DOMSource(xml));
    }

请求:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v1="http://schemas.domain.com/wsdl/fuelprice/v1" xmlns:mod="http://schemas.domain.com/wsdl/fuelprice/v1/model">
   <soapenv:Header/>
   <soapenv:Body>
      <v1:GetAreaFuelStationsRequest provider="ep1" prices="true">
         <v1:area>
            <mod:center ellipsoid="false">
                 <v1:latitude>49.8385</v1:latitude>
                 <v1:longitude>8.5014</v1:longitude>
            </mod:center>
            <mod:radius>10</mod:radius>
         </v1:area>
      </v1:GetAreaFuelStationsRequest>
   </soapenv:Body>
</soapenv:Envelope>

有人能看到这里有什么问题吗?我已经设置了

elementFormDefault="qualified"

但它不起作用。

2 个答案:

答案 0 :(得分:2)

错误消息有一个关键线索:

  

cvc-complex-type.2.4.a:找到无效的内容   元素'v1:纬度'。之一   '{“http://schemas.domain.com/wsdl/fuelprice/v1/model”:latitude}'是   预期

请注意XSD的targetNamespace

targetNamespace="http://schemas.domain.com/wsdl/fuelprice/v1/model"

不同意名称空间前缀

的声明
xmlns:v1="http://schemas.domain.com/wsdl/fuelprice/v1"
latitude在请求中使用

 <v1:latitude>49.8385</v1:latitude>

让它们匹配以消除您的错误。

答案 1 :(得分:0)

我做了什么才能让它发挥作用:

  1. 我将肥皂/信封架构导入了wsdl
  2. 我收到了请求body元素并从中创建了一个文档作为文档根
  3. 我在body-element中添加了soap / envelope命名空间