axis2开发webservice程序
一、环境
eclipse + jdk 6.0 + win7 64位 +tomcat7.0
二、创建服务端程序
1、新建web项目,webserviceTest
2、下载axis2,将lib目录下的jar包复制到web项目lib目录下
3、创建服务端程序HelloWorld.java
1 package com.bwy.ws; 2 3 public class HelloWorld { 4 public String sayHello(String name) { 5 String sayResult = "Hello," + name; 6 System.out.println(sayResult); 7 return sayResult; 8 } 9 }
4、配置web容器,tomcat
5、发布服务端程序,启动tomcat
三、发布webservice服务
右击HelloWorld.java --> Web Service --> Creat web service 一直点击next 直到 finish
生成后的工程如下所示:
四、测试服务
右击HelloWorld.wsdl --> web service --> test with web service exploer
选择方法名,输入参数,点击GO,如图所示,表示测试成功:
注:这里如果不先启动tomcat发布web服务,会报错:IWAB0135E An unexpected error has occurred
五、编写客户端代码调用webservice
新建工程webServiceClient,新建类HelloWorldTest,
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.bwy.client; 2 3 import java.rmi.RemoteException; 4 5 import javax.xml.rpc.ParameterMode; 6 import javax.xml.rpc.ServiceException; 7 8 import org.apache.axis.client.Call; 9 import org.apache.axis.client.Service; 10 import org.apache.axis.encoding.XMLType; 11 12 public class HelloWorldTest { 13 14 public static void main(String[] args) { 15 HelloWorldTest test = new HelloWorldTest(); 16 String result = test.invokeService(); 17 System.out.println(result); 18 } 19 20 21 /** 22 * 调用webservice服务 23 * 24 * @return 服务信息 25 */ 26 public String invokeService() { 27 28 // 远程调用路径 29 String url = "http://localhost:8080/WebServiceTest/services/HelloWorld"; 30 31 //返回值 32 String result = ""; 33 Service serive = new Service(); 34 Call call = null; 35 try { 36 call = (Call) serive.createCall(); 37 call.setTargetEndpointAddress(url); 38 39 // 调用的方法名 40 call.setOperationName("sayHello"); 41 42 // 设置参数名 43 call.addParameter("name", // 参数名 44 XMLType.XSD_STRING, // 参数类型:String 45 ParameterMode.IN); // 参数模式:'IN' or 'OUT' 46 47 //设置返回值类型 48 call.setReturnTypeAsHeader(XMLType.XSD_STRING); 49 String name = "wayne"; 50 51 //远程调用 52 result = (String) call.invoke(new Object[]{name}); 53 } catch (ServiceException e) { 54 e.printStackTrace(); 55 } catch (RemoteException e) { 56 e.printStackTrace(); 57 } 58 return result; 59 } 60 }
右击运行:
执行的时候,发现能访问服务端程序,但接收服务端返回的信息时报错:
AxisFault
faultCode: {http://schemas.xmlsoap.org/soap/envelope/}Server.userException
faultSubcode:
faultString: org.xml.sax.SAXException: Found instance data for {http://ws.bwy.com}sayHelloReturn in the soap:body instead of the soap:header.
faultActor:
faultNode:
faultDetail:
{http://xml.apache.org/axis/}stackTrace:org.xml.sax.SAXException: Found instance data for {http://ws.bwy.com}sayHelloReturn in the soap:body instead of the soap:header.
at org.apache.axis.message.RPCHandler.onStartChild(RPCHandler.java:209)
at org.apache.axis.encoding.DeserializationContext.startElement(DeserializationContext.java:1035)
at org.apache.axis.message.SAX2EventRecorder.replay(SAX2EventRecorder.java:165)
at org.apache.axis.message.MessageElement.publishToHandler(MessageElement.java:1141)
at org.apache.axis.message.RPCElement.deserialize(RPCElement.java:236)
at org.apache.axis.message.RPCElement.getParams(RPCElement.java:384)
at org.apache.axis.client.Call.invoke(Call.java:2467)
at org.apache.axis.client.Call.invoke(Call.java:2366)
at org.apache.axis.client.Call.invoke(Call.java:1812)
at com.bwy.client.HelloWorldTest.invokeService(HelloWorldTest.java:52)
at com.bwy.client.HelloWorldTest.main(HelloWorldTest.java:16)
{http://xml.apache.org/axis/}hostname:bwy
org.xml.sax.SAXException: Found instance data for {http://ws.bwy.com}sayHelloReturn in the soap:body instead of the soap:header.
at org.apache.axis.AxisFault.makeFault(AxisFault.java:101)
at org.apache.axis.client.Call.invoke(Call.java:2470)
at org.apache.axis.client.Call.invoke(Call.java:2366)
at org.apache.axis.client.Call.invoke(Call.java:1812)
at com.bwy.client.HelloWorldTest.invokeService(HelloWorldTest.java:52)
at com.bwy.client.HelloWorldTest.main(HelloWorldTest.java:16)
Caused by: org.xml.sax.SAXException: Found instance data for {http://ws.bwy.com}sayHelloReturn in the soap:body instead of the soap:header.
at org.apache.axis.message.RPCHandler.onStartChild(RPCHandler.java:209)
at org.apache.axis.encoding.DeserializationContext.startElement(DeserializationContext.java:1035)
at org.apache.axis.message.SAX2EventRecorder.replay(SAX2EventRecorder.java:165)
at org.apache.axis.message.MessageElement.publishToHandler(MessageElement.java:1141)
at org.apache.axis.message.RPCElement.deserialize(RPCElement.java:236)
at org.apache.axis.message.RPCElement.getParams(RPCElement.java:384)
at org.apache.axis.client.Call.invoke(Call.java:2467)
... 4 more
后更改如下代码解决:
// 设置返回值类型 // call.setReturnTypeAsHeader(XMLType.XSD_STRING); call.setReturnType(XMLType.XSD_STRING);
这个什么原因还未搞懂,看到此文的朋友如果有了解的可以给我留言,感谢~~
六、生成客户端代码
1、通过命令窗口手动生存客户端代码
客户端输入如下命令:
C:\Users\bwy>wsimport -s F:\MYWORK\webservice\WebServiceClient\src -p com.bwy.client2 -keep http://localhost:8080/WebServiceTest/services/HelloWorld?wsdl
刷新工程,如下所示:
创建测试类,代码如下:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.bwy.client2; 2 3 public class ClientTest { 4 5 public static void main(String[] args) { 6 HelloWorld hw = new HelloWorldService().getHelloWorld(); 7 String result = hw.sayHello("zhangsan"); 8 System.out.println("the response is:"+result); 9 } 10 }
2、通过eclipse客户端插件自动生成客户端代码
将服务端工程生成的wsdl文件放入客户端工程下,右击-->webservice -->generate client
至此,axis2简单webservice实例完成。