调用webService接口

java调用webservice接口 三种方法
原创 哈啰沃尔の 最后发布于2018-09-05 17:40:42 阅读数 52798 收藏
展开

摘自其它:webservice的 发布一般都是使用WSDL(web service descriptive language)文件的样式来发布的,在WSDL文件里面,包含这个webservice暴露在外面可供使用的接口。今天搜索到了非常好的 webservice provider列表

http://www.webservicex.net/WCF/default.aspx

这上面列出了70多个包括很多方面的free webservice provider,utilities->global weather就可以获取全球的天气预报。

直接粘贴代码:

方法一:直接AXIS调用远程的web service

public void doSelectRiskReportForm(HttpServletRequest request,
				HttpServletResponse response){
			//调用接口		
			//方法一:直接AXIS调用远程的web service
			try {  				
	            String endpoint = "http://localhost:8080/platform-jxcx-service/services/settlementServiceImpl?wsdl";  	            
	            Service service = new Service();  
	            Call call = (Call) service.createCall();
	            call.setTargetEndpointAddress(endpoint);
	            String parametersName = "settle_num"; 		// 参数名//对应的是 public String printWord(@WebParam(name = "settle_num") String settle_num); 
//	            call.setOperationName("printWord");  		// 调用的方法名//当这种调用不到的时候,可以使用下面的,加入命名空间名
	            call.setOperationName(new QName("http://jjxg_settlement.platform.bocins.com/", "printWord"));// 调用的方法名
	            call.addParameter(parametersName, XMLType.XSD_STRING, ParameterMode.IN);//参数名//XSD_STRING:String类型//.IN入参
	            call.setReturnType(XMLType.XSD_STRING); 	// 返回值类型:String
	            String message = "123456789";  
	            String result = (String) call.invoke(new Object[] { message });// 远程调用
	            System.out.println("result is " + result);  
	        } catch (Exception e) {  
	            System.err.println(e.toString());  
	        }  
			
			
		} 

方法二:直接SOAP调用远程的webservice

这个方法我没有试验,需要下载jar,SOAP 使用 HTTP 传送 XML,尽管HTTP 不是有效率的通讯协议,而且 XML 还需要额外的文件解析(parse),两者使得交易的速度大大低于其它方案。但是XML 是一个开放、健全、有语义的讯息机制,而 HTTP 是一个广泛又能避免许多关于防火墙的问题,从而使SOAP得到了广泛的应用。但是如果效率对你来说很重要,那么你应该多考虑其它的方式,而不要用 SOAP。

import org.apache.soap.util.xml.*;  
import org.apache.soap.*;  
import org.apache.soap.rpc.*;  
  
import java.io.*;  
import java.net.*;  
import java.util.Vector;  
  
public class caService {  
    public static String getService(String user) {  
        URL url = null;  
        try {  
            url = new URL(  
                    "http://192.168.0.100:8080/ca3/services/caSynrochnized");  
        } catch (MalformedURLException mue) {  
            return mue.getMessage();  
        }  
        // This is the main SOAP object  
        Call soapCall = new Call();  
        // Use SOAP encoding  
        soapCall.setEncodingStyleURI(Constants.NS_URI_SOAP_ENC);  
        // This is the remote object we're asking for the price  
        soapCall.setTargetObjectURI("urn:xmethods-caSynrochnized");  
        // This is the name of the method on the above object  
        soapCall.setMethodName("getUser");  
        // We need to send the ISBN number as an input parameter to the method  
        Vector soapParams = new Vector();  
  
        // name, type, value, encoding style  
        Parameter isbnParam = new Parameter("userName", String.class, user,  
                null);  
        soapParams.addElement(isbnParam);  
        soapCall.setParams(soapParams);  
        try {  
            // Invoke the remote method on the object  
            Response soapResponse = soapCall.invoke(url, "");  
            // Check to see if there is an error, return "N/A"  
            if (soapResponse.generatedFault()) {  
                Fault fault = soapResponse.getFault();  
                String f = fault.getFaultString();  
                return f;  
            } else {  
                // read result  
                Parameter soapResult = soapResponse.getReturnValue();  
                // get a string from the result  
                return soapResult.getValue().toString();  
            }  
        } catch (SOAPException se) {  
            return se.getMessage();  
        }  
    }  
}  

方法三:直接使用eclipse生成客户端.idea类同

以天气预报的为例:

http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl

在页面上直接Ctrl+s保存文件,把xml改成wsdl就行

然后复制到项目里.

webservice服务是需要引入axis的jar包的,如下图:

使用eclipse自动生成WSDL客户端代码

如果没有引入saaj-*.jar,可能会报错。

右键点击WeatherWebService.wsdl文件,找到Web Service项,点击Generate Client子菜单。如下图:

使用eclipse自动生成WSDL客户端代码

在弹出的窗口中点击Next,如下图:

使用eclipse自动生成WSDL客户端代码

选择生成的代码要防止的包位置,如下图:

使用eclipse自动生成WSDL客户端代码

点击完成,稍等片刻,就能看到生成的Java代码了。如下图:

使用eclipse自动生成WSDL客户端代码

生成客户端后,只需要在使用的时候,引入这个接口即可,就好像使用本地类一样.

补充:在使用方式一时遇到几个问题

1.Message part settlementService was not recognized.

解决方式:消息部分未被识别。

其实就是就是方法名错了,如果只输入方法名不行,就加入命名空间

// call.setOperationName(“printWord”); // 调用的方法名//当这种调用不到的时候,可以使用下面的,加入命名空间名
call.setOperationName(new QName(“http://jjxg_settlement.platform.bocins.com/”, “printWord”));// 调用的方法名
2.Unexpected wrapper element printWord found. Expected {http://jjxg_settlement.platform.bocins.com/}printWord.

解决方式:

Call call = (Call) service.createCall();
call.setOperationName(new QName(“命名空间地址”, “方法名”));

3.Unmarshalling Error: 意外的元素 (uri:“http://jjxg_settlement.platform.bocins.com/”, local:“settle_num”)。所需元素为<{}settle_num>

解决方式:

call.addParameter(parametersName, XMLType.XSD_STRING, ParameterMode.IN);//参数名//XSD_STRING:String类型//.IN入参

// 参数名//对应的是 public String printWord(@WebParam(name = “settle_num”) String settle_num);

实际走过的弯路:

命名空间的名称,缺少了"/"

call.setOperationName(new QName(“http://jjxg_settlement.platform.bocins.com/”, “printWord”));// 调用的方法名

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值