2014-11-21 21 views
1

我一直在嘗試整合WS WS來測試SOAP webservice.Intergration已經遇到了障礙,因爲我無法在unmarshelled對象中收到正確的響應。 下面是詳細信息:Spring Web服務解組不正確發生

應用上下文低於:

<!-- Define the SOAP version used by the WSDL --> 
<bean id="soapMessageFactory" class="org.springframework.ws.soap.saaj.SaajSoapMessageFactory"> 
    <property name="soapVersion"> 
     <util:constant static-field="org.springframework.ws.soap.SoapVersion.SOAP_12"/> 
    </property> 
</bean> 

<!-- The location of the generated Java files --> 
<oxm:jaxb2-marshaller id="marshaller" contextPath="com.uk.fs.generatedsources"/> 

<!-- Configure Spring Web Services --> 
<bean id="webServiceTemplate" class="org.springframework.ws.client.core.WebServiceTemplate"> 
    <constructor-arg ref="soapMessageFactory"/> 
    <property name="marshaller" ref="marshaller"/> 
    <property name="unmarshaller" ref="marshaller"/> 
    <property name="defaultUri" value="http://192.168.243.21/test/services/testservice?wsdl"/> 
</bean> 

下面是它調用marshalSendAndReceive方法:

public List<FSChild> getFSChildren() { 
    GetFSChildren getFSChildren= new ObjectFactory().createGetFSChildren(); 
    getFSChildren.setCountry("CA"); 
    getFSChildren.setFsCode("5010"); 
    getFSChildren.setLimit(6); 
    GetFSChildrenResponse response = (GetFSChildrenResponse) webServiceTemplate.marshalSendAndReceive(
      getFSChildren); 

    return response.getGetFSChildrenReturn(); 
} 

類GetFSChildrenResponse是如下:

@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "", propOrder = { 
"getHSChildrenReturn" 
}) 
@XmlRootElement(name = "getFSChildrenResponse") 
public class GetFSChildrenResponse { 

@XmlElement(required = true) 
protected List<FSChild> getFSChildrenReturn; 

public List<FSChild> getGetFSChildrenReturn() { 
    if (getFSChildrenReturn == null) { 
     getFSChildrenReturn = new ArrayList<FSChild>(); 
    } 
    return this.getFSChildrenReturn; 
} 

}

類FSChild是如下:

@XmlAccessorType(XmlAccessType.FIELD) 
@XmlType(name = "FSChild", namespace = "http://hs.uk.com", propOrder = { 
"description", 
"fsCode" 
}) 
public class FSChild { 
@XmlElement(required = true, nillable = true) 
protected String description; 
@XmlElement(required = true, nillable = true) 
protected String fsCode; 

public String getDescription() { 
    return description; 
} 
public void setDescription(String value) { 
    this.description = value; 
} 
public String getFsCode() { 
    return hsCode; 
} 
public void setFsCode(String value) { 
    this.hsCode = value; 
} 

}

我覺得非常接近得到它的工作.. 請求適當整理,甚至請求被正確地發送到Web服務,其記錄的響應和正確返回。 但是當嘗試提取像下面的迴應:

GetFSChildrenResponse response = (GetFSChildrenResponse) webServiceTemplate.marshalSendAndReceive(getFSChildren); 
現在

在當我調試了這一點,我發現響應對象填充它包含包含一個項目這是預期,但代碼和說明1名getHSChildrenReturn列表fschild被設置爲null。

我甚至已經打開TRACE日誌messages.I看到XML響應正常返回的數據是如下:

<?xml version="1.0" encoding="utf-8"?> 
<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
    <soapenv:Body> 
     <getHSChildrenResponse xmlns="http://service.hs.pb.com"> 
      <getHSChildrenReturn> 
       <description>Silk: Silk-worm cocoons suitable for reeling.</description> 
       <fsCode>5001000000</hsCode> 
      </getFSChildrenReturn> 
     </getFSChildrenResponse> 
    </soapenv:Body> 
</soapenv:Envelope> 

請從中找到生成的類的WSDL如下:

<wsdl:definitions xmlns:apachesoap="http://xml.apache.org/xml-soap"   xmlns:impl="http://service.fs.uk.com" xmlns:intf="http://service.fs.uk.com" xmlns:tns1="http://fs.uk.com" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://service.fs.uk.com"> 


    <!-- 
WSDL created by Apache Axis version: 1.4 
Built on Apr 22, 2006 (06:55:48 PDT) 
--> 
<wsdl:types> 
<schema xmlns="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="http://service.fs.uk.com"> 
<import namespace="http://fs.uk.com"/> 
<element name="getCountries"> 
<complexType/> 
</element> 
<element name="getCountriesResponse"> 
<complexType> 
<sequence> 
<element maxOccurs="unbounded" name="getCountriesReturn" type="xsd:string"/> 
</sequence> 
</complexType> 
</element> 
<element name="getFSChildren"> 
<complexType> 
<sequence> 
<element name="fsCode" type="xsd:string"/> 
<element name="country" type="xsd:string"/> 
<element name="limit" type="xsd:int"/> 
</sequence> 
</complexType> 
</element> 
<element name="getFSChildrenResponse"> 
<complexType> 
<sequence> 
<element maxOccurs="unbounded" name="getFSChildrenReturn" type="tns1:FSChild"/> 
</sequence> 
</complexType> 
</element> 
</schema> 
<schema xmlns="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" targetNamespace="http://fs.uk.com"> 
<complexType name="FSChild"> 
<sequence> 
<element name="description" nillable="true" type="xsd:string"/> 
<element name="fsCode" nillable="true" type="xsd:string"/> 
</sequence> 
</complexType> 
</schema> 
</wsdl:types> 
<wsdl:message name="getCountriesResponse"> 
<wsdl:part element="impl:getCountriesResponse" name="parameters"></wsdl:part> 
</wsdl:message> 
<wsdl:message name="getCountriesRequest"> 
<wsdl:part element="impl:getCountries" name="parameters"></wsdl:part> 
</wsdl:message> 
<wsdl:message name="getFSChildrenRequest"> 
<wsdl:part element="impl:getFSChildren" name="parameters"></wsdl:part> 
</wsdl:message> 
<wsdl:message name="getFSChildrenResponse"> 
<wsdl:part element="impl:getFSChildrenResponse" name="parameters"></wsdl:part> 
</wsdl:message> 
<wsdl:portType name="FSService"> 
<wsdl:operation name="getCountries"> 
<wsdl:input message="impl:getCountriesRequest" name="getCountriesRequest"></wsdl:input> 
<wsdl:output message="impl:getCountriesResponse" name="getCountriesResponse"></wsdl:output> 
</wsdl:operation> 
<wsdl:operation name="getFSChildren"> 
<wsdl:input message="impl:getFSChildrenRequest" name="getFSChildrenRequest"></wsdl:input> 
<wsdl:output message="impl:getFSChildrenResponse" name="getFSChildrenResponse"></wsdl:output> 
</wsdl:operation> 
</wsdl:portType> 
<wsdl:binding name="FSServiceSoapBinding" type="impl:FSService"> 
<wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/> 
<wsdl:operation name="getCountries"> 
<wsdlsoap:operation soapAction=""/> 
<wsdl:input name="getCountriesRequest"> 
<wsdlsoap:body use="literal"/> 
</wsdl:input> 
<wsdl:output name="getCountriesResponse"> 
<wsdlsoap:body use="literal"/> 
</wsdl:output> 
</wsdl:operation> 
<wsdl:operation name="getFSChildren"> 
<wsdlsoap:operation soapAction=""/> 
<wsdl:input name="getFSChildrenRequest"> 
<wsdlsoap:body use="literal"/> 
</wsdl:input> 
<wsdl:output name="getFSChildrenResponse"> 
<wsdlsoap:body use="literal"/> 
</wsdl:output> 
</wsdl:operation> 
</wsdl:binding> 
<wsdl:service name="testserviceService"> 
<wsdl:port binding="impl:FSServiceSoapBinding" name="testservice"> 
<wsdlsoap:address location="http://192.168.240.87/FSWS/testservice/FSService"/> 
</wsdl:port> 
</wsdl:service> 
</wsdl:definitions> 
+0

你還可以發佈JAXB類生成的XML模式嗎? – 2014-11-22 10:40:49

+0

@AndreasVeithen我已經添加了wsdl使用我生成的類。 – Rips 2014-11-22 11:11:10

回答

2

在經歷了幾個小時後回答我自己的問題。在wsdl中我似乎遇到了命名空間的問題。我在wsdl中改變了以下兩個方法,並再次生成了這些類,導致了正確的響應。 xmlns:tns1="http://fs.uk.com"xmlns:tns1="http://service.fs.uk.com"

targetNamespace="http://fs.uk.com"targetNamespace="http://service.fs.uk.com" 爲元件FSChild。 對於我來說這並不令人驚訝,因爲原始的wsdl沒有任何更改可與SOAPUI完美協作。如果任何人都可以解釋這一點,我會獎勵他賞金

+0

JAXB的實體名稱空間和WSDL定義的xml名稱空間必須匹配,每個xml元素都有名稱空間,如果不正確,解組發生的情況如何正確? – 2014-11-27 02:20:07

+0

那麼JAXB實體是從WSDL本身生成的,所以它們應該已經匹配。 – Rips 2014-11-27 06:27:46

+0

這是正確的。 – 2014-11-28 06:18:49