[英]Remove custom tags from web service response
我使用Java和軸2創建了一個簡單的Web服務。我得到的輸出如下,
輸入-http:// localhost:8088 / Newwww / services / NewFile / newFile?s = New%20data3
輸出 -
<ns:newFileResponse xmlns:ns="http://Services.tcs.com">
- <ns:return>
- <TestData>
<testData1>New data1</testData1>
<testData2>New data2</testData2>
<testData3>New data3</testData3>
</TestData>
</ns:return>
</ns:newFileResponse>
如何刪除以ns開頭的標簽:簡而言之,我希望響應只是
<TestData>
<testData1>New data1</testData1>
<testData2>New data2</testData2>
<testData3>New data3</testData3>
</TestData>
我的WSDL文件看起來像這樣
<?xml version="1.0" encoding="UTF-8" ?>
- <wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:ns1="http://org.apache.axis2/xsd" xmlns:ns="http://Services.tcs.com" xmlns:wsaw="http://www.w3.org/2006/05/addressing/wsdl" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" targetNamespace="http://Services.tcs.com">
<wsdl:documentation>Please Type your service description here</wsdl:documentation>
- <wsdl:types>
- <xs:schema attributeFormDefault="qualified" elementFormDefault="qualified" targetNamespace="http://Services.tcs.com">
- <xs:element name="newFile">
- <xs:complexType>
- <xs:sequence>
<xs:element minOccurs="0" name="s" nillable="true" type="xs:string" />
</xs:sequence>
</xs:complexType>
</xs:element>
- <xs:element name="newFileResponse">
- <xs:complexType>
- <xs:sequence>
<xs:element minOccurs="0" name="return" nillable="true" type="xs:anyType" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
</wsdl:types>
- <wsdl:message name="newFileRequest">
<wsdl:part name="parameters" element="ns:newFile" />
</wsdl:message>
- <wsdl:message name="newFileResponse">
<wsdl:part name="parameters" element="ns:newFileResponse" />
</wsdl:message>
- <wsdl:portType name="NewFilePortType">
- <wsdl:operation name="newFile">
<wsdl:input message="ns:newFileRequest" wsaw:Action="urn:newFile" />
<wsdl:output message="ns:newFileResponse" wsaw:Action="urn:newFileResponse" />
</wsdl:operation>
</wsdl:portType>
- <wsdl:binding name="NewFileSoap11Binding" type="ns:NewFilePortType">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" />
- <wsdl:operation name="newFile">
<soap:operation soapAction="urn:newFile" style="document" />
- <wsdl:input>
<soap:body use="literal" />
</wsdl:input>
- <wsdl:output>
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
- <wsdl:binding name="NewFileSoap12Binding" type="ns:NewFilePortType">
<soap12:binding transport="http://schemas.xmlsoap.org/soap/http" style="document" />
- <wsdl:operation name="newFile">
<soap12:operation soapAction="urn:newFile" style="document" />
- <wsdl:input>
<soap12:body use="literal" />
</wsdl:input>
- <wsdl:output>
<soap12:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
- <wsdl:binding name="NewFileHttpBinding" type="ns:NewFilePortType">
<http:binding verb="POST" />
- <wsdl:operation name="newFile">
<http:operation location="NewFile/newFile" />
- <wsdl:input>
<mime:content type="text/xml" part="newFile" />
</wsdl:input>
- <wsdl:output>
<mime:content type="text/xml" part="newFile" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
- <wsdl:service name="NewFile">
- <wsdl:port name="NewFileHttpSoap11Endpoint" binding="ns:NewFileSoap11Binding">
<soap:address location="http://localhost:8088/Newwww/services/NewFile.NewFileHttpSoap11Endpoint/" />
</wsdl:port>
- <wsdl:port name="NewFileHttpSoap12Endpoint" binding="ns:NewFileSoap12Binding">
<soap12:address location="http://localhost:8088/Newwww/services/NewFile.NewFileHttpSoap12Endpoint/" />
</wsdl:port>
- <wsdl:port name="NewFileHttpEndpoint" binding="ns:NewFileHttpBinding">
<http:address location="http://localhost:8088/Newwww/services/NewFile.NewFileHttpEndpoint/" />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
謝謝..
您的WSDL揭示了您使用Web服務中最糟糕的反模式之一:使用anyType。 您的界面應該定義其他部分發送和接受的數據類型 - 使用anyType強制界面的兩側知道另一方的內部。
<xs:element name="newFileResponse">
<xs:complexType>
<xs:sequence>
<xs:element minOccurs="0" name="return" nillable="true" type="xs:anyType" />
</xs:sequence>
</xs:complexType>
</xs:element>
由於您將“newFileResponse”(元素)定義為“newFileResponse”(消息)的消息部分,因此您當然會接收newFileResponse元素作為內容。
首先:擺脫anyType,在界面中定義testdate元素。
第二:重命名元素和消息,使它們沒有相同的名稱 - 這是為了提高可讀性。
第三:為您的消息使用以下內容
<wsdl:message name="newFileResponse">
<wsdl:part name="parameters" element="ns:TestData" />
</wsdl:message>
通過將TestData定義為消息部分而不是anyType-eating newFileResponse-element,可以擺脫混淆的包裝元素並直接使用TestData類型。
沒有直接的方法可以做到這一點。 有兩種選擇。
在那里,您需要為您修改生成的wsdl for suite或創建一個新的wsdl並使用wsdl2java工具為其生成代碼並部署該服務。
在這里,您可以在ESB中創建代理服務,並在out序列中對響應進行任何轉換[2]。
[1] http://wso2.org/library/2873 [2] http://wso2.org/project/esb/java/4.0.0/docs/samples/message_mediation_samples.html#Sample8
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.