[英]How do I design a generic Response builder / RESTful Web Service using Spring MVC?
[英]How can I see and debug a web service response when using a Spring proxy?
我正在編寫一個Java + Spring客戶端,該客戶端呼出到遠程Web服務。 我知道我正確地訪問了Web服務,因為如果我發送了錯誤的ID,我會從該服務收到一個自定義的SOAP異常,說沒有那個ID的學生。 但是,如果我確實發送了有效的ID,則調用返回的StudentObject為null。 我還有另一種用於在他們的系統中創建學生的方法,該方法返回具有新ID的字符串。 同樣,我知道這可行(我已經檢查了主機系統上的數據),但是我的返回值為null。
因此,我希望能夠檢查來自Web服務的響應,以了解我們到底要返回什么,以及為什么它沒有正確映射到對象中。 我希望將響應寫到日志中,但是我不知道如何獲得該響應。 我該怎么做呢?
我像這樣定義了Web服務的接口:
@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創建服務(然后將其自動連線並注入到我的應用程序中):
<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() {
}
}
我建議暫時不要使用Spring並嘗試SOAP UI 。 它使您可以輕松地提出請求並在響應返回時查看響應的所有詳細信息。
一種方法是使用tcpmon(http://java.net/projects/tcpmon/)設置代理。 使它在端口上偵聽並將其轉發到服務器。 所有請求/響應將被記錄。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.