一路拾遗
Collect By Finding All The Way ......
posts - 81,comments - 41,trackbacks - 0
首先说一下使用AXIS调用WSDL文件时需要的JAR包:
(1)axis    其中包括axis.jar、axis-ant.jar、commons-discovery-0.2.jar、commons-logging-1.0.4.jar、jaxrpc.jar、log4j-1.2.8.jar、log4j.properties、saaj.jar、wsdl4j-1.5.1.jar。http://www.apache.org/dyn/closer.cgi/ws/axis/
(2)javamail    mail.jar。http://java.sun.com/products/javamail/
(3)jaf    activation.jar。http://java.sun.com/javase/technologies/desktop/javabeans/jaf/downloads/index.html
一、WSDL文件内容
该实例采用的WSDL文件是使用AXIS创建的Web服务,功能是输入字符串,返回欢迎语句。对于远程(其他服务器)的Web服务的调用一直不成功,原本想调用下DictionaryService.wsdl,可是运行时总是返回“ faultString: org.xml.sax.SAXException: Bad envelope tag:  definitions”错误,实在无奈、、、Hello.jws的内容如下:
<?xml version="1.0" encoding="UTF-8" ?> 
<wsdl:definitions targetNamespace="http://localhost:8080/axis/Hello.jws" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://localhost:8080/axis/Hello.jws" xmlns:intf="http://localhost:8080/axis/Hello.jws" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<!-
WSDL created by Apache Axis version: 1.4
Built on Apr 22, 2006 (06:55:48 PDT)
  --
> 
<wsdl:message name="helloResponse">
  
<wsdl:part name="helloReturn" type="xsd:string" /> 
  
</wsdl:message>
<wsdl:message name="helloRequest">
  
<wsdl:part name="name" type="xsd:string" /> 
  
</wsdl:message>
<wsdl:portType name="Hello">
<wsdl:operation name="hello" parameterOrder="name">
  
<wsdl:input message="impl:helloRequest" name="helloRequest" /> 
  
<wsdl:output message="impl:helloResponse" name="helloResponse" /> 
  
</wsdl:operation>
  
</wsdl:portType>
<wsdl:binding name="HelloSoapBinding" type="impl:Hello">
  
<wsdlsoap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http" /> 
<wsdl:operation name="hello">
  
<wsdlsoap:operation soapAction="" /> 
<wsdl:input name="helloRequest">
  
<wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://DefaultNamespace" use="encoded" /> 
  
</wsdl:input>
<wsdl:output name="helloResponse">
  
<wsdlsoap:body encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" namespace="http://localhost:8080/axis/Hello.jws" use="encoded" /> 
  
</wsdl:output>
  
</wsdl:operation>
  
</wsdl:binding>
<wsdl:service name="HelloService">
<wsdl:port binding="impl:HelloSoapBinding" name="Hello">
  
<wsdlsoap:address location="http://localhost:8080/axis/Hello.jws" /> 
  
</wsdl:port>
  
</wsdl:service>
  
</wsdl:definitions>

二、调用Web服务
据说有三种方法,参见:http://blog.csdn.net/boy_wh520/archive/2007/05/09/1601756.aspx
http://www.ibm.com/developerworks/cn/webservices/ws-startaxis/index.html
这里只介绍一下第一种方法,代码如下:
package wsdl;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;

public class CallService {

    
public static void main(String[] args) {

        
try {
                       
            String endpoint 
= "http://localhost:8080/axis/Hello.jws";

            
//调用过程
            Service service = new Service();
            
            Call call 
= (Call) service.createCall();
            
            call.setTargetEndpointAddress(
new  java.net.URL(endpoint));
            
            call.setOperationName(
"hello");//WSDL里面描述的操作名称
            
            call.addParameter(
"helloRequest", org.apache.axis.encoding.XMLType.XSD_STRING, javax.xml.rpc.ParameterMode.IN);//操作的参数
            
            call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);
//设置返回类型  
            
            call.setUseSOAPAction( 
true );
            
            
//给方法传递参数,并且调用方法
            String temp = "Tom";
            Object[] obj 
= new Object[]{temp};
            String result 
= (String)call.invoke(obj);
            
            System.out.println(
"Result is : "+result);
            }
 catch (Exception e) {
                e.printStackTrace();
            }

    }

}


posted on 2008-08-04 21:48 胖胖泡泡 阅读(9675) 评论(3)  编辑  收藏

FeedBack:
# re: 使用AXIS调用WSDL描述的Web服务[未登录]
2008-09-07 22:15 | dong
你这个问题解决了没?我也遇到了,不知道改如何做,如果解决了发心信息到我邮箱吧,hsd6181000@163.com  回复  更多评论
  
# re: 使用AXIS调用WSDL描述的Web服务
2008-09-08 09:25 | 胖胖泡泡
问题已解决 参见 使用AXIS调用WSDL描述的Web服务(续) http://www.blogjava.net/mrcold/archive/2008/08/05/220121.html  回复  更多评论
  
# re: 使用AXIS调用WSDL描述的Web服务
2009-06-17 22:48 | xc
@胖胖泡泡
这里是加?WSDL的目的
“?WSDL: Obtaining WSDL for deployed services
When you make a service available using Axis, there is typically a unique URL associated with that service. For JWS files, that URL is simply the path to the JWS file itself. For non-JWS services, this is usually the URL "http://<host>/axis/services/<service-name>".

If you access the service URL in a browser, you'll see a message indicating that the endpoint is an Axis service, and that you should usually access it using SOAP. However, if you tack on "?wsdl" to the end of the URL, Axis will automatically generate a service description for the deployed service, and return it as XML in your browser (try it!). The resulting description may be saved or used as input to proxy-generation, described next. You can give the WSDL-generation URL to your online partners, and they'll be able to use it to access your service with toolkits like .NET, SOAP::Lite, or any other software which supports using WSDL.

You can also generate WSDL files from existing Java classes (see Java2WSDL: Building WSDL from Java).

  回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: