1.首先创建一个webservice服务,然后发布出去;
2.然后创建一个webServiceClient客户端去访问;
(1)创建一个Dynamic Web Project
(2)创建包名,新建类,编写方法;
package com.zhouy.web; public class webServiceDemo { public void sayHello(){ System.out.println("hello webservice!"); } public String sayToSomebody(String str){ return "Hello "+str+",welcome to webservice!"; } }
(3)选中这个刚才新建的类,右键new-->创建Web Service
然后WebContent文件夹下就会出现一个wsdl文件夹,里边自动生成了一个.wsdl文件;
然后将这个webservice发布出去;点击项目右键-->Run onserver;
然后在浏览器中输入http://localhost:8080/webService01/services/webServiceDemo?wsdl;
出现如下所示证明此webservice接口发布成功.
(4)创建客户端调用此webservice接口;
继续创建一个Dynamic Web Project,新建包名;
选中包名然后右键创建Web Service Client;
输入webservice接口地址 http://localhost:8080/webService01/services/webServiceDemo?wsdl;
然后会自动创建出这些类和接口;
这里采用用两种代码测试,其实原理都是一样,从类中跟进去会发现代码都一个意思;
测试1:
package com.zhouy.test; import org.apache.axis.client.Call; import org.apache.axis.client.Service; import org.apache.axis.encoding.XMLType; public class AxisTest { public static void main(String[] args) { try { String str = "zhouy" ; String endpoint = "http://localhost:8080/webService01/services/webServiceDemo?wsdl" ; Service service = new Service() ; Call call = (Call) service.createCall() ; call.setTargetEndpointAddress(endpoint) ; call.setOperationName("sayToSomebody") ;//ws方法名 //一个输入参数,如果方法有多个参数,复制多条该代码即可,参数传入下面new Object后面 call.addParameter("parameter1",org.apache.axis.encoding.XMLType.XSD_DATE,javax.xml.rpc.ParameterMode.IN) ; // call.addParameter("parameter2",org.apache.axis.encoding.XMLType.XSD_DATE,javax.xml.rpc.ParameterMode.IN) ; call.setReturnType(XMLType.XSD_STRING) ; call.setUseSOAPAction(true) ; // call.setSOAPActionURI("http://172.20.35.123:6110/cea-ws/services/ChangeMemberPinService"); String result = (String) call.invoke(new Object[]{str}) ; System.out.println(result); } catch (Exception e) { e.printStackTrace(); } } }
测试2:
package com.zhouy.test; import java.rmi.RemoteException; import com.zhouy.web.WebServiceDemo; import com.zhouy.web.WebServiceDemoProxy; public class test { public static void main(String[] args) { // TODO Auto-generated method stub WebServiceDemo wd = new WebServiceDemoProxy().getWebServiceDemo(); try { // String str = wd.sayToSomebody("zhouy"); // System.out.println(str); wd.sayHello(); } catch (RemoteException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }