第一种:生成代码的调用方式
打开Dos窗口,然后使用jdk自带的wsimport生成代码,下面以翻译接口为例:
//翻译接口的webservice地址
http://fy.webxml.com.cn/webservices/EnglishChinese.asmx
输入下面命令,然后回车:
wsimport -keep http://fy.webxml.com.cn/webservices/EnglishChinese.asmx?wsdl
会报一个无法解析s:schema的错误,如下图:
此时需要将wsdl保存到本地,在浏览器访问下面的地址,然后ctrl+s保存为EnglishChinese.wsdl
http://fy.webxml.com.cn/webservices/EnglishChinese.asmx?wsdl
将EnglishChinese.wsdl文件中的<s:element ref="s:schema"/><s:any/>全部替换为<s:any minOccurs="2" maxOccurs="2"/>
替换完后,重新生成代码,在DOS窗口输入下面命令,然后回车:
wsimport -keep F:\bak\bak25\v9\EnglishChinese.wsdl
代码生成成功后,拷贝到Eclipse,然后写测试类,如下:
public static void main(String[] args) {
EnglishChinese objEnglishChinese = new EnglishChinese();
EnglishChineseSoap EnglishChineseSoap = objEnglishChinese.getEnglishChineseSoap();
TranslatorResult translatorResult = EnglishChineseSoap.translator("仙女");
}
第二种:使用axis调用
先下载axis的zip包,解压后将lib里面的所有jar导入项目,下面是zip下载地址:
https://archive.apache.org/dist/ws/axis/1_4/axis-bin-1_4.zip
导包以后直接写测试类即可,如下:
public static void main(String[] args) throws Exception {
//字符集
String encodingStyle = "utf-8";
//WSDL的地址
String endpoint = "http://fy.webxml.com.cn/webservices/EnglishChinese.asmx?wsdl";
//命名空间
String targetNamespace = "http://WebXml.com.cn/";
//具体方法的调用URI
String soapActionURI = "http://WebXml.com.cn/Translator";
//具体调用的方法名
String method = "Translator";
//调用接口的参数的名字
String[] paramNames = {"wordKey"};
//调用接口的参数的值
String[] paramValues = {"仙女"};
Service service = new Service();
Call call = (Call) service.createCall();
call.setTimeout(20000); //设置超时时间
call.setSOAPActionURI(soapActionURI);
call.setTargetEndpointAddress(new java.net.URL(endpoint)); //设置目标接口的地址
call.setEncodingStyle(encodingStyle); //设置传入服务端的字符集格式如utf-8等
call.setOperationName(new QName(targetNamespace, method));
call.setUseSOAPAction(true);
call.addParameter(new QName(targetNamespace, paramNames[0]),
org.apache.axis.encoding.XMLType.XSD_STRING,
javax.xml.rpc.ParameterMode.IN);// 接口的参数
// call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);// 设置返回类型 ,如String
// call.setReturnClass(java.lang.String[].class); //返回字符串数组类型
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_SCHEMA); //返回字符串数组类型
//给方法传递参数,并且调用方法
Object result = call.invoke(paramValues);
}
完整的调用代码如下:
package weather.test;
import java.io.ByteArrayInputStream;
import java.util.List;
import javax.xml.namespace.QName;
import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.types.Schema;
import org.dom4j.Document;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import com.alibaba.fastjson.JSON;
import cn.com.webxml.EnglishChinese;
import cn.com.webxml.EnglishChineseSoap;
import cn.com.webxml.TranslatorResponse.TranslatorResult;
public class EnglishChineseTest {
public static void main(String[] args) throws Exception {
//通过生成代码的方式调用webservice
System.out.println("================通过生成代码的方式调用webservice================");
callWebserviceViaGenCode();
//通过axis调用webservice
System.out.println("================通过axis调用webservice================");
callWebserviceViaAxis();
}
/**
* 通过生成代码的方式调用webservice
* @throws Exception
*/
public static void callWebserviceViaGenCode() throws Exception {
//调用webservice
EnglishChinese objEnglishChinese = new EnglishChinese();
EnglishChineseSoap EnglishChineseSoap = objEnglishChinese.getEnglishChineseSoap();
TranslatorResult translatorResult = EnglishChineseSoap.translator("仙女");
//将返回结果转换为xml字符串
String xmlStr = JSON.toJSONString(translatorResult.getAny().get(1));
xmlStr = xmlStr.substring(1, xmlStr.length() - 1)
.replaceAll("\\\\\"", "\"")
.replaceAll(":null", "aa")
.replaceAll("null:", "");
//处理返回结果
process(xmlStr);
}
/**
* 通过axis调用webservice
* @throws Exception
*/
public static void callWebserviceViaAxis() throws Exception {
//字符集
String encodingStyle = "utf-8";
//WSDL的地址
String endpoint = "http://fy.webxml.com.cn/webservices/EnglishChinese.asmx?wsdl";
//命名空间
String targetNamespace = "http://WebXml.com.cn/";
//具体方法的调用URI
String soapActionURI = "http://WebXml.com.cn/Translator";
//具体调用的方法名
String method = "Translator";
//调用接口的参数的名字
String[] paramNames = {"wordKey"};
//调用接口的参数的值
String[] paramValues = {"仙女"};
Service service = new Service();
Call call = (Call) service.createCall();
call.setTimeout(20000); //设置超时时间
call.setSOAPActionURI(soapActionURI);
call.setTargetEndpointAddress(new java.net.URL(endpoint)); //设置目标接口的地址
call.setEncodingStyle(encodingStyle); //设置传入服务端的字符集格式如utf-8等
call.setOperationName(new QName(targetNamespace, method));
call.setUseSOAPAction(true);
call.addParameter(new QName(targetNamespace, paramNames[0]),
org.apache.axis.encoding.XMLType.XSD_STRING,
javax.xml.rpc.ParameterMode.IN);// 接口的参数
// call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);// 设置返回类型 ,如String
// call.setReturnClass(java.lang.String[].class); //返回字符串数组类型
call.setReturnType(org.apache.axis.encoding.XMLType.XSD_SCHEMA); //返回字符串数组类型
//给方法传递参数,并且调用方法
Object result = call.invoke(paramValues);
//将返回结果转换为xml字符串
Schema schema = (Schema)result;
String xmlStr = JSON.toJSONString(schema.get_any()[1]);
xmlStr = xmlStr.substring(1, xmlStr.length()-1).replaceAll("\\\\\"", "\"");
//处理返回结果
process(xmlStr);
}
/**
* 通过dom4j处理返回结果
* @param xmlStr
* @throws Exception
*/
public static void process(String xmlStr) throws Exception {
SAXReader reader = new SAXReader();
Document document = reader.read(new ByteArrayInputStream(xmlStr.getBytes()));
Node nodeTrans = document.selectSingleNode( "//Dictionary/Trans");
Node nodeWordKey = nodeTrans.selectSingleNode( "./WordKey");
Node nodeTranslation = nodeTrans.selectSingleNode( "./Translation");
System.out.println("你需要翻译的是:"+nodeWordKey.getText());
System.out.println("翻译为:"+nodeTranslation.getText());
System.out.println("=========短语例句=========");
List<Node> listSentence = document.selectNodes( "//Dictionary/Sentence");
for(Node sentence : listSentence) {
Node sentenceOrig = sentence.selectSingleNode( "./Orig");
Node sentenceTrans = sentence.selectSingleNode( "./Trans");
System.out.println("例句:"+sentenceOrig.getText());
System.out.println("翻译为:"+sentenceTrans.getText());
System.out.println();
}
}
}