akash777.sharma
akash777.sharma

Reputation: 702

cvc-elt.1: Cannot find the declaration of element

I am new to xsd and soap. I created an xsd and want to configure it for spring ws .Currently I am getting an error. I followed this . I think I am making some small mistake regarding 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) I created stub class from this xsd by using JAXB (generate classes from schema)

(3) wsdl file created from xsd

<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) Endpoint class for 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("[email protected]");
        ud.setUserGender(UserGender.MALE);
        ud.setUserId("1");
        ud.setUsername("obama");
        ud.setUserSalary(200.20);
        udr.setAccountDetails(ud);
        return udr;
    }
}

(5) Soap request from 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 response

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

please help.

Upvotes: 2

Views: 4444

Answers (1)

akash777.sharma
akash777.sharma

Reputation: 702

I got the solution for the problem.My xsd file was valid but I had configured a validator interceptor in my xml file which was validating my request and response object.

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

currently I do not know how to validate my xsd request and response object but WS communication I disabled this feature by request and response object as false.

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

Upvotes: 1

Related Questions