Java調用WebService方法總結(1)--準備工做

WebService是一種跨編程語言、跨操做系統平臺的遠程調用技術,已存在不少年了,不少接口也都是經過WebService方式來發布的;本系列文章主要介紹Java調用WebService的各類方法,使用在線的中文簡體字<->繁體字轉換服務(http://www.webxml.com.cn/WebServices/TraditionalSimplifiedWebService.asmx)做爲測試服務端,使用其中的簡體字轉換爲繁體字方法toTraditionalChinese來演示WebService的調用。本文主要作些準備工做,方便後續文章中實際的調用。html

一、WSDL

爲了防止該在線WebService不可用,這裏把WSDL貼在這裏,方便與代碼對照。java

http://www.webxml.com.cn/WebServices/TraditionalSimplifiedWebService.asmx?wsdlweb

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:tns="http://webxml.com.cn/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://webxml.com.cn/">
<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
<a href="http://www.webxml.com.cn/" target="_blank">WebXml.com.cn</a> <strong>中文簡體字<->繁體字轉換 WEB 服務</strong><br />此中文簡體字<->繁體字轉換Web Services請不要用於任何商業目的,如有須要請<a href="http://www.webxml.com.cn/zh_cn/contact_us.aspx" target="_blank">聯繫咱們</a>,歡迎技術交流。 QQ:8409035<br /><strong>使用本站 WEB 服務請註明或連接本站:http://www.webxml.com.cn/ 感謝你們的支持</strong><br /><br />&nbsp;
</wsdl:documentation>
<wsdl:types>
<s:schema elementFormDefault="qualified" targetNamespace="http://webxml.com.cn/">
<s:element name="toSimplifiedChinese">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="sText" type="s:string"/>
</s:sequence>
</s:complexType>
</s:element>
<s:element name="toSimplifiedChineseResponse">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="toSimplifiedChineseResult" type="s:string"/>
</s:sequence>
</s:complexType>
</s:element>
<s:element name="toTraditionalChinese">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="sText" type="s:string"/>
</s:sequence>
</s:complexType>
</s:element>
<s:element name="toTraditionalChineseResponse">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="toTraditionalChineseResult" type="s:string"/>
</s:sequence>
</s:complexType>
</s:element>
<s:element name="string" nillable="true" type="s:string"/>
</s:schema>
</wsdl:types>
<wsdl:message name="toSimplifiedChineseSoapIn">
<wsdl:part name="parameters" element="tns:toSimplifiedChinese"/>
</wsdl:message>
<wsdl:message name="toSimplifiedChineseSoapOut">
<wsdl:part name="parameters" element="tns:toSimplifiedChineseResponse"/>
</wsdl:message>
<wsdl:message name="toTraditionalChineseSoapIn">
<wsdl:part name="parameters" element="tns:toTraditionalChinese"/>
</wsdl:message>
<wsdl:message name="toTraditionalChineseSoapOut">
<wsdl:part name="parameters" element="tns:toTraditionalChineseResponse"/>
</wsdl:message>
<wsdl:message name="toSimplifiedChineseHttpGetIn">
<wsdl:part name="sText" type="s:string"/>
</wsdl:message>
<wsdl:message name="toSimplifiedChineseHttpGetOut">
<wsdl:part name="Body" element="tns:string"/>
</wsdl:message>
<wsdl:message name="toTraditionalChineseHttpGetIn">
<wsdl:part name="sText" type="s:string"/>
</wsdl:message>
<wsdl:message name="toTraditionalChineseHttpGetOut">
<wsdl:part name="Body" element="tns:string"/>
</wsdl:message>
<wsdl:message name="toSimplifiedChineseHttpPostIn">
<wsdl:part name="sText" type="s:string"/>
</wsdl:message>
<wsdl:message name="toSimplifiedChineseHttpPostOut">
<wsdl:part name="Body" element="tns:string"/>
</wsdl:message>
<wsdl:message name="toTraditionalChineseHttpPostIn">
<wsdl:part name="sText" type="s:string"/>
</wsdl:message>
<wsdl:message name="toTraditionalChineseHttpPostOut">
<wsdl:part name="Body" element="tns:string"/>
</wsdl:message>
<wsdl:portType name="TraditionalSimplifiedWebServiceSoap">
<wsdl:operation name="toSimplifiedChinese">
<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
<br /><h3>繁體字轉換爲簡體字</h3><p>輸入參數:sText = 字符串; 返回數據:字符串。</p><br />
</wsdl:documentation>
<wsdl:input message="tns:toSimplifiedChineseSoapIn"/>
<wsdl:output message="tns:toSimplifiedChineseSoapOut"/>
</wsdl:operation>
<wsdl:operation name="toTraditionalChinese">
<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
<br /><h3>簡體字轉換爲繁體字</h3><p>輸入參數:sText = 字符串; 返回數據:字符串。</p><br />
</wsdl:documentation>
<wsdl:input message="tns:toTraditionalChineseSoapIn"/>
<wsdl:output message="tns:toTraditionalChineseSoapOut"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:portType name="TraditionalSimplifiedWebServiceHttpGet">
<wsdl:operation name="toSimplifiedChinese">
<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
<br /><h3>繁體字轉換爲簡體字</h3><p>輸入參數:sText = 字符串; 返回數據:字符串。</p><br />
</wsdl:documentation>
<wsdl:input message="tns:toSimplifiedChineseHttpGetIn"/>
<wsdl:output message="tns:toSimplifiedChineseHttpGetOut"/>
</wsdl:operation>
<wsdl:operation name="toTraditionalChinese">
<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
<br /><h3>簡體字轉換爲繁體字</h3><p>輸入參數:sText = 字符串; 返回數據:字符串。</p><br />
</wsdl:documentation>
<wsdl:input message="tns:toTraditionalChineseHttpGetIn"/>
<wsdl:output message="tns:toTraditionalChineseHttpGetOut"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:portType name="TraditionalSimplifiedWebServiceHttpPost">
<wsdl:operation name="toSimplifiedChinese">
<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
<br /><h3>繁體字轉換爲簡體字</h3><p>輸入參數:sText = 字符串; 返回數據:字符串。</p><br />
</wsdl:documentation>
<wsdl:input message="tns:toSimplifiedChineseHttpPostIn"/>
<wsdl:output message="tns:toSimplifiedChineseHttpPostOut"/>
</wsdl:operation>
<wsdl:operation name="toTraditionalChinese">
<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
<br /><h3>簡體字轉換爲繁體字</h3><p>輸入參數:sText = 字符串; 返回數據:字符串。</p><br />
</wsdl:documentation>
<wsdl:input message="tns:toTraditionalChineseHttpPostIn"/>
<wsdl:output message="tns:toTraditionalChineseHttpPostOut"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="TraditionalSimplifiedWebServiceSoap" type="tns:TraditionalSimplifiedWebServiceSoap">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="toSimplifiedChinese">
<soap:operation soapAction="http://webxml.com.cn/toSimplifiedChinese" style="document"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="toTraditionalChinese">
<soap:operation soapAction="http://webxml.com.cn/toTraditionalChinese" 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="TraditionalSimplifiedWebServiceSoap12" type="tns:TraditionalSimplifiedWebServiceSoap">
<soap12:binding transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="toSimplifiedChinese">
<soap12:operation soapAction="http://webxml.com.cn/toSimplifiedChinese" style="document"/>
<wsdl:input>
<soap12:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap12:body use="literal"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="toTraditionalChinese">
<soap12:operation soapAction="http://webxml.com.cn/toTraditionalChinese" 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="TraditionalSimplifiedWebServiceHttpGet" type="tns:TraditionalSimplifiedWebServiceHttpGet">
<http:binding verb="GET"/>
<wsdl:operation name="toSimplifiedChinese">
<http:operation location="/toSimplifiedChinese"/>
<wsdl:input>
<http:urlEncoded/>
</wsdl:input>
<wsdl:output>
<mime:mimeXml part="Body"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="toTraditionalChinese">
<http:operation location="/toTraditionalChinese"/>
<wsdl:input>
<http:urlEncoded/>
</wsdl:input>
<wsdl:output>
<mime:mimeXml part="Body"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:binding name="TraditionalSimplifiedWebServiceHttpPost" type="tns:TraditionalSimplifiedWebServiceHttpPost">
<http:binding verb="POST"/>
<wsdl:operation name="toSimplifiedChinese">
<http:operation location="/toSimplifiedChinese"/>
<wsdl:input>
<mime:content type="application/x-www-form-urlencoded"/>
</wsdl:input>
<wsdl:output>
<mime:mimeXml part="Body"/>
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="toTraditionalChinese">
<http:operation location="/toTraditionalChinese"/>
<wsdl:input>
<mime:content type="application/x-www-form-urlencoded"/>
</wsdl:input>
<wsdl:output>
<mime:mimeXml part="Body"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="TraditionalSimplifiedWebService">
<wsdl:documentation xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
<a href="http://www.webxml.com.cn/" target="_blank">WebXml.com.cn</a> <strong>中文簡體字<->繁體字轉換 WEB 服務</strong><br />此中文簡體字<->繁體字轉換Web Services請不要用於任何商業目的,如有須要請<a href="http://www.webxml.com.cn/zh_cn/contact_us.aspx" target="_blank">聯繫咱們</a>,歡迎技術交流。 QQ:8409035<br /><strong>使用本站 WEB 服務請註明或連接本站:http://www.webxml.com.cn/ 感謝你們的支持</strong><br /><br />&nbsp;
</wsdl:documentation>
<wsdl:port name="TraditionalSimplifiedWebServiceSoap" binding="tns:TraditionalSimplifiedWebServiceSoap">
<soap:address location="http://www.webxml.com.cn/WebServices/TraditionalSimplifiedWebService.asmx"/>
</wsdl:port>
<wsdl:port name="TraditionalSimplifiedWebServiceSoap12" binding="tns:TraditionalSimplifiedWebServiceSoap12">
<soap12:address location="http://www.webxml.com.cn/WebServices/TraditionalSimplifiedWebService.asmx"/>
</wsdl:port>
<wsdl:port name="TraditionalSimplifiedWebServiceHttpGet" binding="tns:TraditionalSimplifiedWebServiceHttpGet">
<http:address location="http://www.webxml.com.cn/WebServices/TraditionalSimplifiedWebService.asmx"/>
</wsdl:port>
<wsdl:port name="TraditionalSimplifiedWebServiceHttpPost" binding="tns:TraditionalSimplifiedWebServiceHttpPost">
<http:address location="http://www.webxml.com.cn/WebServices/TraditionalSimplifiedWebService.asmx"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
View Code

二、用SoapUI生成Soap消息

該在線WebService同時實現Soap1.1和Soap1.2,這裏分別生成消息。編程

2.一、Soap1.1

查詢xmlapp

查看Raw編程語言

2.一、Soap1.2

查看xmlide

查看Raw測試

三、本地WebService服務

因爲咱們用的在線WebService服務端爲.NET實現的,而咱們客戶端是JAVA,若是是RPC方式調用則沒法調用;這裏建立一個本地的JAX-WS實現的WebService,用於這個系列文章中RPC方式的調用。RPC已通過時,但爲了技術的完整性,也記錄下。url

3.一、接口類

package com.inspur.zsyw.ws;

import javax.jws.WebParam;
import javax.jws.WebService;

@WebService
@SOAPBinding(style = SOAPBinding.Style.RPC)
public interface ITestService { String hello(@WebParam(name = "name") String name); }

3.一、實現類

package com.inspur.zsyw.ws.impl;

import com.inspur.zsyw.ws.ITestService;

@javax.jws.WebService(endpointInterface="com.inspur.zsyw.ws.ITestService", targetNamespace = "http://ws.zsyw.inspur.com/", serviceName = "TestService")
public class TestServiceImpl implements ITestService {
    @Override
    public String hello(String name) {
        return "hello," + name;
    }
}

3.三、本地WSDL

http://10.40.103.48:9006/zsywservice/TestService?wsdlspa

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<!--
 Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is Oracle JAX-WS 2.1.5. 
-->
<!--
 Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is Oracle JAX-WS 2.1.5. 
-->
<definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://ws.zsyw.inspur.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://ws.zsyw.inspur.com/" name="TestService">
<types/>
<message name="hello">
<part name="name" type="xsd:string"/>
</message>
<message name="helloResponse">
<part name="return" type="xsd:string"/>
</message>
<portType name="ITestService">
<operation name="hello">
<input message="tns:hello"/>
<output message="tns:helloResponse"/>
</operation>
</portType>
<binding name="TestServiceImplPortBinding" type="tns:ITestService">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="rpc"/>
<operation name="hello">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal" namespace="http://ws.zsyw.inspur.com/"/>
</input>
<output>
<soap:body use="literal" namespace="http://ws.zsyw.inspur.com/"/>
</output>
</operation>
</binding>
<service name="TestService">
<port name="TestServiceImplPort" binding="tns:TestServiceImplPortBinding">
<soap:address location="http://10.40.103.48:9006/zsywservice/TestService"/>
</port>
</service>
</definitions>
View Code

四、調用

本文主要介紹一些準備工做,具體調用參見下列文章:

Java調用WebService方法總結(2)--JAX-WS調用WebService

Java調用WebService方法總結(3)--wsimport調用WebService

Java調用WebService方法總結(4)--Axis調用WebService

Java調用WebService方法總結(5)--Axis2調用WebService

Java調用WebService方法總結(6)--XFire調用WebService

Java調用WebService方法總結(7)--CXF調用WebService

Java調用WebService方法總結(8)--soap.jar調用WebService

Java調用WebService方法總結(9,end)--Http方式調用WebService

相關文章
相關標籤/搜索