|
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
|---|---|
| 作者 | 正文 |
|
时间:2008-05-15 关键字: web service axis apache
第一部分: 安装Axis <?xml version="1.0" encoding="gb2312"?> <!-- Axis学习工程 --> <Context docBase="F:/AXIS_Study/axis-bin-1_4/webapps/axis" > </Context> public class SayHello{
private String name;
public String hello(){
return "Hello, axis Ver1.4 talking to you.";
}
}
<?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions targetNamespace="http://localhost/axis/SayHello.jws" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://localhost/axis/SayHello.jws" xmlns:intf="http://localhost/axis/SayHello.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:message>
<wsdl:portType name="SayHello">
<wsdl:operation name="hello">
<wsdl:input message="impl:helloRequest" name="helloRequest"/>
<wsdl:output message="impl:helloResponse" name="helloResponse"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="SayHelloSoapBinding" type="impl:SayHello">
<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/axis/SayHello.jws" use="encoded"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="SayHelloService">
<wsdl:port binding="impl:SayHelloSoapBinding" name="SayHello">
<wsdlsoap:address location="http://localhost/axis/SayHello.jws"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
这个文件结构跟我们以前用IDE生成的没什么两样。 <?xml version="1.0" encoding="UTF-8" ?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<helloResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<helloReturn xsi:type="xsd:string">Hello, axis Ver1.4 talking to you.</helloReturn>
</helloResponse>
</soapenv:Body>
</soapenv:Envelope>
这里返回的是整个SOAP数据包 public class SayHelloTest {
public void test(){
SayHelloSoapBindingStub binding;
try {
binding = (SayHelloSoapBindingStub) new SayHelloServiceLocator().getSayHello(); //取接口
binding.setTimeout(60000); //设置超时时间
String helloMsg = binding.hello(); //调用hello方法
System.out.println(">>>>>>>>>>>>>>>>服务器返回消息:");
System.out.println( helloMsg );
System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
}catch (javax.xml.rpc.ServiceException ex) {
if (ex.getLinkedCause() != null)
ex.getLinkedCause().printStackTrace();
System.out.println("JAX-RPC调用出错:");
ex.printStackTrace();
} catch (RemoteException ex) {
System.out.println("Web Service调用出错:");
ex.printStackTrace();
}
}
public static void main(String[] args) {
SayHelloTest sayhellotest = new SayHelloTest();
sayhellotest.test();
}
}
<?xml version="1.0" encoding="UTF-8"?>
<deployment name="defaultClientConfig"
xmlns:java="http://xml.apache.org/axis/wsdd/providers/java"
xmlns:handler="http://xml.apache.org/axis/wsdd/providers/handler" xmlns="http://xml.apache.org/axis/wsdd/">
<globalConfiguration name="defaultClientConfig">
<requestFlow name="RequestFlow1" type="">
<handler name="Handler1" type="java:org.apache.axis.handlers.JWSHandler">
<parameter name="scope" value="session"/>
</handler>
<handler name="Handler2" type="java:org.apache.axis.handlers.JWSHandler">
<parameter name="scope" value="request"/>
<parameter name="extension" value=".jwr"/>
</handler>
</requestFlow>
</globalConfiguration>
<handler name="URLMapper" type="java:org.apache.axis.handlers.http.URLMapper"/>
<handler name="LocalResponder" type="java:org.apache.axis.transport.local.LocalResponder"/>
<handler name="Authenticate" type="java:org.apache.axis.handlers.SimpleAuthenticationHandler"/>
<transport name="http" type="">
<requestFlow name="RequestFlow1" type="">
<handler name="Handler1" type="URLMapper"/>
<handler name="Handler2" type="java:org.apache.axis.handlers.http.HTTPAuthHandler"/>
</requestFlow>
</transport>
<transport name="local" type="">
<responseFlow name="ResponseFlow1" type="">
<handler name="Handler1" type="LocalResponder"/>
</responseFlow>
</transport>
<!--这里配置了一个Web Service,如果有多个Web Service,就按这种格式在下面增加即可-->
<service name="SayHello2" provider="java:RPC">
<parameter name="scope" value="Request"/>
<parameter name="className" value="SayHello"/>
<parameter name="allowedMethods" value="*"/>
</service>
</deployment>
<?xml version="1.0" encoding="UTF-8"?> <wsdl:definitions targetNamespace="http://localhost/axis/services/SayHello2" xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://localhost/axis/services/SayHello2" xmlns:intf="http://localhost/axis/services/SayHello2" 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="helloRequest"> </wsdl:message> <wsdl:message name="helloResponse"> <wsdl:part name="helloReturn" type="soapenc:string"/> </wsdl:message> <wsdl:portType name="SayHello"> <wsdl:operation name="hello"> <wsdl:input message="impl:helloRequest" name="helloRequest"/> <wsdl:output message="impl:helloResponse" name="helloResponse"/> </wsdl:operation> </wsdl:portType> <wsdl:binding name="SayHello2SoapBinding" type="impl:SayHello"> <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/axis/services/SayHello2" use="encoded"/> </wsdl:output> </wsdl:operation> </wsdl:binding> <wsdl:service name="SayHelloService"> <wsdl:port binding="impl:SayHello2SoapBinding" name="SayHello2"> <wsdlsoap:address location="http://localhost/axis/services/SayHello2"/> </wsdl:port> </wsdl:service> </wsdl:definitions> public class SayHello2Test {
public void test(){
SayHello2SoapBindingStub binding;
try {
binding = (SayHello2SoapBindingStub) new SayHello2ServiceLocator().getSayHello(); //取接口
binding.setTimeout(60000); //设置超时时间
String helloMsg = binding.hello(); //调用hello方法
System.out.println(">>>>>>>>>>>>>>>>服务器返回消息:");
System.out.println( helloMsg );
System.out.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
}catch (javax.xml.rpc.ServiceException ex) {
if (ex.getLinkedCause() != null)
ex.getLinkedCause().printStackTrace();
System.out.println("JAX-RPC调用出错:");
ex.printStackTrace();
} catch (RemoteException ex) {
System.out.println("Web Service调用出错:");
ex.printStackTrace();
}
}
public static void main(String[] args) {
SayHello2Test sayhellotest = new SayHello2Test();
sayhellotest.test();
}
}
声明:JavaEye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
|
|
| 返回顶楼 | |
|
时间:2008-06-01
在Eclipse开发客户端是不是需要什么插件??
这试了下创建不了Web Server Client的工程, |
|
| 返回顶楼 | |
|
时间:2008-06-01
guojun22300763 写道 在Eclipse开发客户端是不是需要什么插件?? 这试了下创建不了Web Server Client的工程, 个人感觉应该没有建立web server client工程的必要.就是一个普通的project就可以.再说,eclipse里好像没有这个选项吧.我没有弄错吧??:) 另外,如果你使用eclipse,有2个建议: 1.download一个axis-ant,这样就可以使用在ant里使用wsdl2java和java2wsdl,配置一下,就可以自动得到wsdl文件和webservice 文件. 2.web client和web service使用2个工程,这样就将service和client分开了. |
|
| 返回顶楼 | |
|
时间:2008-06-01
我用的Eclipse 3.3, 可以看到这个选项
在new => other => web services => web service client 我不习惯用ant(这习惯不太好), service和client是应该分开才好, 试着建了一个普通web 工程,用server()测试通过, 谢谢了,~~ |
|
| 返回顶楼 | |
|
时间:2008-06-02
建立普通的project即可,不用特别的项目,只要在WEB-INF下有 server-config.wsdd 即可
|
|
| 返回顶楼 | |
|
时间:2008-08-05
第一种 我在eclipse 下通过测试
第二种 不行 Error generating services Could not find type {http://localhost:8080/axis/services/SayHello2}helloReturn |
|
| 返回顶楼 | |
|
时间:2008-08-05
现在NetBeans下面也可以通过升级插件获得之间的Axis2集成了
|
|
| 返回顶楼 | |
|
时间:2008-08-16
楼主说的步骤太复杂了,初学者可能看得晕,这里有个捷径,web services和一般的java类没多大区别,主要是部署问题,eclipse自动能把一个类部署为web services,并生成客户机.所以我的做法是:
1,建立一个动态web工程,并选择将该工程自动加入到一个ear工程 2,在这个动态web工程创建类和方法,和一般的写法没什么区别 3,用eclipse把类部署为web services,会自动生成wsdl和server-config.wsdd 4,把ear工程导出为ear包,把ear包部署到web server中,这样web services就创建并部署好了,很简单. 在ie地址栏里输入wsdl中的那个url,如果能显示出wsdl的内容,部署就ok了 查看部署的所有web services: http://ip:port/动态web工程/servlet/AxisServlet 在第3步中,可以选择web services运行时和服务器,运行时一般有三种,一是apache axis,二是jax-rpc,三是jax-ws,这三种就是常用的web services实现 对于web services的客户端,开发工具也提供自动生成和部署的功能,但生成的都是stub存根,当web services的接口发生变化,就要重新生成stub客户端,并重新部署,所以一般不使用stub类型的客户端.其实只要几行代码,就可调用web services: String endpointURL = "...."; //wsdl中的url Service service = new Service(); Call call = ( Call ) service.createCall(); call.setTargetEndpointAddress( new java.net.URL( endpointURL ) ); call.setOperationName( new QName( "wsdl中的namespace", "要调用的方法名称" ) ); call.addParameter( "入参名称", QName实例, ParameterMode.IN ); call.addParameter( ... ); //加其它参数 call.setReturnClass( Class.forNam( "返回的类名称" ) ); call.setReturnType( new QName( "名字空间", "xml中的类型名称" ), Class.forName( "返回类的名称" ) ); Object[] inputs = ... //入参的值在这里面 call.invoke( inputs ) 所以,只要知道wsdl,就能够调用web services 注意: 1,入参和返回参数必须是基本类型或实现Serializable接口 2,如果是自定义的类,除了要实现Serializable接口,客户端调用时(动态,非stub),要做xml类型到java类的映射,如上面的addParameter()方法和setReturnType()方法 3,如要查看请求和响应soap的内容,apache axis中有一个soapmonitor,将它整合到你的web services应用中即可 |
|
| 返回顶楼 | |
浏览 2279 次





