cvc-elt.1:找不到元素的声明

时间:2014-11-30 15:22:46

标签: xml soap jaxb xsd

我是xsd和soap的新手。我创建了一个xsd,并希望为spring ws配置它。目前我收到一个错误。我跟着this。我想我对xsd犯了一些小错误。

(1)UserDetails.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
        xmlns="http://reqres.data.ws.akash.com/userdetails" 
        xmlns:tns="http://reqres.data.ws.akash.com/userdetails" 
        targetNamespace="http://reqres.data.ws.akash.com/userdetails"
        elementFormDefault="qualified">

    <xsd:element name="UserDetailsRequest">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="UserData" type="UserData"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>

    <xsd:complexType name="UserData">
        <xsd:sequence>
            <xsd:element name="userId" type="xsd:string"/>
        </xsd:sequence>
    </xsd:complexType>

    <xsd:element name="UserDetailsResponse">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element  name="AccountDetails" type="tns:UserDetails"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>

    <xsd:complexType name="UserDetails">
        <xsd:sequence>
            <xsd:element name="userId" type="xsd:string"/>
            <xsd:element name="username" type="xsd:string"/>
            <xsd:element name="age" type="xsd:int"/>
            <xsd:element name="emailId" type="xsd:string"/>
            <xsd:element name="userSalary" type="xsd:double"/>
            <xsd:element name="userGender" type="tns:UserGender"/>
        </xsd:sequence>
    </xsd:complexType>

    <xsd:simpleType name="UserGender">
        <xsd:restriction base="xsd:string">
            <xsd:enumeration value="Male"/>
            <xsd:enumeration value="Female"/>
        </xsd:restriction>
    </xsd:simpleType>

</xsd:schema>

(2)我使用JAXB从这个xsd创建了存根类(从模式生成类)

(3)从xsd

创建的wsdl文件
<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
    xmlns:sch="http://reqres.data.ws.akash.com/userdetails" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
    xmlns:tns="http://reqres.data.ws.akash.com/userdetails"
    targetNamespace="http://reqres.data.ws.akash.com/userdetails">
    <wsdl:types>
        <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            attributeFormDefault="unqualified" elementFormDefault="qualified"
            targetNamespace="http://reqres.data.ws.akash.com/userdetails">
            <xsd:element name="UserDetailsRequest">
                <xsd:complexType>
                    <xsd:sequence>
                        <xsd:element name="UserData" type="tns:UserData" />
                    </xsd:sequence>
                </xsd:complexType>
            </xsd:element>
            <xsd:complexType name="UserData">
                <xsd:sequence>
                    <xsd:element name="userId" type="xsd:string" />
                </xsd:sequence>
            </xsd:complexType>
            <xsd:element name="UserDetailsResponse">
                <xsd:complexType>
                    <xsd:sequence>
                        <xsd:element name="AccountDetails" type="tns:UserDetails" />
                    </xsd:sequence>
                </xsd:complexType>
            </xsd:element>
            <xsd:complexType name="UserDetails">
                <xsd:sequence>
                    <xsd:element name="userId" type="xsd:string" />
                    <xsd:element name="username" type="xsd:string" />
                    <xsd:element name="age" type="xsd:int" />
                    <xsd:element name="emailId" type="xsd:string" />
                    <xsd:element name="userSalary" type="xsd:double" />
                    <xsd:element name="userGender" type="tns:UserGender" />
                </xsd:sequence>
            </xsd:complexType>
            <xsd:simpleType name="UserGender">
                <xsd:restriction base="xsd:string">
                    <xsd:enumeration value="Male" />
                    <xsd:enumeration value="Female" />
                </xsd:restriction>
            </xsd:simpleType>
        </xsd:schema>
    </wsdl:types>
    <wsdl:message name="UserDetailsRequest">
        <wsdl:part element="tns:UserDetailsRequest" name="UserDetailsRequest"></wsdl:part>
    </wsdl:message>
    <wsdl:message name="UserDetailsResponse">
        <wsdl:part element="tns:UserDetailsResponse" name="UserDetailsResponse"></wsdl:part>
    </wsdl:message>
    <wsdl:portType name="UserDetailsServicePort">
        <wsdl:operation name="UserDetails">
            <wsdl:input message="tns:UserDetailsRequest" name="UserDetailsRequest"></wsdl:input>
            <wsdl:output message="tns:UserDetailsResponse" name="UserDetailsResponse"></wsdl:output>
        </wsdl:operation>
    </wsdl:portType>
    <wsdl:binding name="UserDetailsServicePortSoap11" type="tns:UserDetailsServicePort">
        <soap:binding style="document"
            transport="http://schemas.xmlsoap.org/soap/http" />
        <wsdl:operation name="UserDetails">
            <soap:operation soapAction="" />
            <wsdl:input name="UserDetailsRequest">
                <soap:body use="literal" />
            </wsdl:input>
            <wsdl:output name="UserDetailsResponse">
                <soap:body use="literal" />
            </wsdl:output>
        </wsdl:operation>
    </wsdl:binding>
    <wsdl:service name="UserDetailsServicePortService">
        <wsdl:port binding="tns:UserDetailsServicePortSoap11" name="UserDetailsServicePortSoap11">
            <soap:address location="http://localhost:8080/MySpringWS/endpoints/" />
        </wsdl:port>
    </wsdl:service>
</wsdl:definitions>

(4)wsdl的端点类

package com.akash.ws.endpoint;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.ws.server.endpoint.annotation.Endpoint;
import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
import org.springframework.ws.server.endpoint.annotation.RequestPayload;
import org.springframework.ws.server.endpoint.annotation.ResponsePayload;

import com.akash.ws.server.userdetails.stub.UserDetails;
import com.akash.ws.server.userdetails.stub.UserDetailsRequest;
import com.akash.ws.server.userdetails.stub.UserDetailsResponse;
import com.akash.ws.server.userdetails.stub.UserGender;
import com.akash.ws.service.UserDetailsService;

@Endpoint
public class UserDetailEndpoint {

    private static final String NAMESPACE="http://reqres.data.ws.akash.com/userdetails";

    @Autowired
    @Qualifier("userService")
    private UserDetailsService userDetailsService;

    @PayloadRoot(localPart="UserDetailsRequest",namespace=NAMESPACE)
    public @ResponsePayload UserDetailsResponse getUserDetail(@RequestPayload UserDetailsRequest userDetail) {

        UserDetailsResponse udr=new UserDetailsResponse();
        UserDetails ud=new UserDetails();
        ud.setAge(35);
        ud.setEmailId("obama@gmail.com");
        ud.setUserGender(UserGender.MALE);
        ud.setUserId("1");
        ud.setUsername("obama");
        ud.setUserSalary(200.20);
        udr.setAccountDetails(ud);
        return udr;
    }
}

(5)来自SOAP UI的肥皂请求

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:user="http://reqres.data.ws.akash.com/userdetails">
   <soapenv:Header/>
   <soapenv:Body>
      <user:UserDetailsRequest>
         <user:UserData>
            <user:userId>1</user:userId>
         </user:UserData>
      </user:UserDetailsRequest>
   </soapenv:Body>
</soapenv:Envelope>

(6)SOAP UI响应

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
   <SOAP-ENV:Header/>
   <SOAP-ENV:Body>
      <SOAP-ENV:Fault>
         <faultcode>SOAP-ENV:Client</faultcode>
         <faultstring xml:lang="en">Validation error</faultstring>
         <detail>
            <spring-ws:ValidationError xmlns:spring-ws="http://springframework.org/spring-ws">cvc-elt.1: Cannot find the declaration of element 'user:UserDetailsRequest'.</spring-ws:ValidationError>
         </detail>
      </SOAP-ENV:Fault>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

请帮忙。

1 个答案:

答案 0 :(得分:1)

我得到了问题的解决方案。我的xsd文件有效,但我在我的xml文件中配置了验证器拦截器,验证了我的请求和响应对象。

<sws:interceptors>
        <bean class="org.springframework.ws.server.endpoint.interceptor.PayloadLoggingInterceptor" />

        <bean id="validatingInterceptor"
                class="com.akash.ws.interceptor.PayloadValidatingInterceptorWithSourceFix">
            <property name="schema" value="schema/UserDetails.xsd"/>
            <property name="validateRequest" value="true"/>
            <property name="validateResponse" value="true"/>
        </bean>
    </sws:interceptors>

目前我不知道如何验证我的xsd请求和响应对象,但WS通信我通过请求和响应对象将此功能禁用为false。

<property name="validateRequest" value="false"/>
<property name="validateResponse" value="false"/>