Java+Axis WebService接口开发实例
创建两个Web项目
1、WebService项目:负责提供接口,导入Axis的Jar包
(1)创建server-config.wsdd
xmlns:java="http://xml.apache.org/axis/wsdd/providers/java"> < name="attachments.implementation"> value="org.apache.axis.attachments.AttachmentsImpl" /> <> name="URLMapper" /> class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver" />
(2)在web.xml中添加
AxisServlet org.apache.axis.transport.http.AxisServlet AxisServlet /services/*
(3)创建接口类
package com.donghaiair.test; public class TestWebsService { public String testWebsServiceNull(){ return "test"; } public String testWebsServiceOne(String s){ return "test:" + s; } public String testWebsServiceTwo(int i,String s){ return "test:" + i + "\n" + s; } }
2、WebServiceClient项目:负责调用接口,导入Axis的Jar包
package com.donghaiair.test; import java.net.MalformedURLException; import java.rmi.RemoteException; import javax.xml.rpc.ServiceException; import org.apache.axis.client.Call; import org.apache.axis.client.Service; public class TestWebsService { public static void main(String[] args) { String url = "http://localhost:8080/WebService/services/TestWebService.jws";// Service service = new Service(); Call call; try { call = (Call) service.createCall(); call.setTargetEndpointAddress(new java.net.URL(url) ); // call.setOperationName("testWebsServiceNull" );// 这是要调用的方法 // String res = (String) call.invoke(new Object[] {null}); call.setOperationName("testWebsServiceOne" );// 这是要调用的方法 String res = (String) call.invoke( new Object[] {"徐士宽"} ); // call.setOperationName("testWebsServiceTwo" );// 这是要调用的方法 // String res = (String) call.invoke( new Object[] {17,"徐士宽"} ); System.out.println(res); }catch (MalformedURLException e) { } catch (RemoteException e) { e.printStackTrace(); } catch (ServiceException e) { e.printStackTrace(); } } }