我在寫一個調用遠程Web服務的Java + Spring客戶端。我知道我正確地擊中了Web服務,因爲如果我發送了一個錯誤的ID,我會從該服務收到一個自定義SOAP異常,表示沒有該ID的學生。但是,如果我確實發送了一個有效的id,則從該調用返回的StudentObject爲null。我有另一種方法用於在他們的系統中創建一個學生,並返回一個帶有新ID的字符串。再次,我知道這是有效的(我已經檢查了主機系統上的數據),但是我的返回值爲空。如何在使用Spring代理時查看和調試Web服務響應?
所以我想能夠檢查來自Web服務的響應,以便知道我們究竟返回的是什麼以及它爲什麼沒有正確映射到對象中。我希望看到寫入日誌的響應,但我無法弄清楚如何獲得響應。我該怎麼做呢?
我定義的接口的WebService是這樣的:
@WebService(name = "CSM_port", targetNamespace = "wsdl_namespace")
public interface TargetWebService {
@WebMethod(operationName = "getAsObjectBySchoolID", action = "http://blah/getAsObjectBySchoolID")
@WebResult(name = "student", targetNamespace = "namespace")
@RequestWrapper(localName = "getAsObjectBySchoolID", targetNamespace = "namespace", className = "com.example.IDRequest")
@ResponseWrapper(localName = "getAsObjectBySchoolIDResponse", targetNamespace = "namespace", className = "com.example.IDResponse")
public StudentObject getStudentById(@WebParam(name = "id", targetNamespace = "namespace") String id);
}
我使用ProxyFactory通過創建服務(然後自動裝配Autowired並注入我的應用程序):
<bean id="webService" class="org.springframework.remoting.jaxws.JaxWsPortProxyFactoryBean">
<property name="wsdlDocumentUrl" value="classpath:student_api.wsdl.xml" />
<property name="namespaceUri" value="namespace" />
<property name="serviceName" value="CSM_StudentWebService" />
<property name="portName" value="CSM_StudentWebServicePort" />
<property name="serviceInterface" value="TargetWebService" />
<property name="username" value="user" />
<property name="password" value="password" />
</bean>
下面是從WSDL相關大塊:
<xsd:complexType name="StudentObject">
<xsd:all>
<xsd:element name="student_id" type="xsd:string"/>
<xsd:element name="school_student_id" type="xsd:string" nillable="true"/>
<xsd:element name="fullname" type="xsd:string" nillable="true"/>
<xsd:element name="fname" type="xsd:string" nillable="true"/>
<xsd:element name="mi" type="xsd:string" nillable="true"/>
<xsd:element name="lname" type="xsd:string" nillable="true"/>
<xsd:element name="email" type="xsd:string" nillable="true"/>
... a billion other properties
</xsd:all>
</xsd:complexType>
<message name="getAsObjectBySchoolIDResponse">
<part name="student" type="tns:StudentObject"/>
</message>
<operation name="getAsObjectBySchoolID">
<input message="tns:getAsObjectBySchoolIDRequest"/>
<output message="tns:getAsObjectBySchoolIDResponse"/>
</operation>
最後,我StudentObject類:
public class StudentObject {
@XmlElement(name="student_id")
private String symplicityId;
@XmlElement(name="school_student_id", nillable=true)
private String schoolId;
@XmlElement(name="fname", nillable=true)
private String firstName;
@XmlElement(name="lname", nillable=true)
private String lastName;
@XmlElement(name="email", nillable=true)
private String email;
public StudentObject() {
}
}
雖然問題與調試的問題處理得越多,空對象解決了最終加入@SOAPBinding(樣式= SOAPBinding.Style.RPC)到我的服務接口解決。我已經完全錯過了WSDL(這是一個大文件),其中的樣式設置爲「RPC」的結合部。添加SOAPBinding註釋使一切正常。另外,我能夠從我的方法中刪除@RequestWrapper和@ResponseWrapper註釋。 – Travelsized 2011-04-25 15:51:14