Axis2实现WebService实例
Web Service是实现SOA(Service-Oriented Architecture)面向服务架构的技术,而Axis2是实现Web Service的一种技术框架(架构)。
先去apache官网上下载axis2-1.6.2-bin和axis2-1.6.2-war
下载地址:http://axis.apache.org/axis2/java/core/download.cgi
配置AXIS2的环境变量
将axis2-1.6.2-bin.zip解压
在环境变量里面配置AXIS2_HOME指向你的axis路径就行了。
解压axis2-1.6.2-war并将解压出来的axis2.war解压到tomcat的webapps中
访问:http://localhost:8082/axis2/端口自己改成自己的tomcat的端口就行了,
看到Welcome!就说明成功了。
WebService发布方式
我们先打开axis2\WEB-INF\conf下的axis2.xml。
有一段配置:
<parameter name="hotdeployment">true</parameter> <parameter name="hotupdate">false</parameter>
public class TestService {public String getName(String name){return name;}public String helloWorld(){return "hello world!";}}
<ns:getNameResponse> <return>xxx</return></ns:getNameResponse>
<ns:helloWorldResponse> <return>hello world!</return></ns:helloWorldResponse>
<deployer extension=".class" directory="pojo" name="code"> <deployer extension=".class" directory="xxx" name="code">package com.jynine.webservice;public class AxisService {public String sayHello(){return "hello";}public String saygoodBye(String name){return "Good Bye "+name +"!";}public void updateName(String name){System.out.println("Update "+name+" !");}}
<service name="AxisService"> <description> Web Service例子 </description> <parameter name="ServiceClass"> com.jynine.webservice.AxisService </parameter> <messageReceivers> <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out" /> <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only" /> </messageReceivers> </service>
- test - META-INF - services.xml - com - jynine - webservice - AxisService.class
<serviceGroup> <service name="AxisService"> <description> Web Service例子 </description> <parameter name="ServiceClass"> com.jynine.webservice.AxisService </parameter> <messageReceivers> <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out" /> <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only" /> </messageReceivers> </service> <service name="SimpleAxisService"> <description> 简单的axis服务 </description> <parameter name="ServiceClass"> com.jynine.webservice.SimpleAxisService </parameter> <messageReceivers> <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out" /> <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only" /> </messageReceivers> </service> </serviceGroup>
package com.jynine.webservice;import java.util.HashMap;import java.util.Map;/** * Axis webService 例子 * @author jynine * */public class EmployeeInfo {private Map<String, String> map = new HashMap<String, String>();/** * 初始化 */private void initEmployee(){map.put("1000", "Tom");map.put("1001", "Jim");map.put("1002", "Rose");map.put("1004", "Jack");}/** * 获取员工名称 * @param num 工号 * @return */public String getEmployeeName(String num){initEmployee();String name = "未知";if(map.get(num) != null){name = map.get(num);}return name;}/** * 更新员工信息 * @param num 工号 * @param name 员工姓名 */public void updateEmployee(String num,String name){initEmployee();map.put(num, name);}/** * 删除员工信息 * @param num 工号 */public void deleteEmployee(String num){initEmployee();map.remove(num);}}
package com.jynine.webservice;import org.apache.axiom.om.OMAbstractFactory;import org.apache.axiom.om.OMElement;import org.apache.axiom.om.OMFactory;import org.apache.axiom.om.OMNamespace;import org.apache.axiom.om.OMNode;import org.apache.axis2.Constants;import org.apache.axis2.addressing.EndpointReference;import org.apache.axis2.client.Options;import org.apache.axis2.client.ServiceClient;public class AxisClient {/** * 指定调用WebService的URL */ private static EndpointReference targetEPR = new EndpointReference("http://localhost:8082/axis2/services/EmployeeInfo"); public static OMElement getNameByNum(String num){ OMFactory fac = OMAbstractFactory.getOMFactory(); OMNamespace namespace = fac.createOMNamespace("http://webservice.jynine.com", "tns"); OMElement method = fac.createOMElement("getEmployeeName", namespace); OMElement value = fac.createOMElement("num", namespace); OMNode omNode = fac.createOMText(value, num); value.addChild(omNode); method.addChild(value); return method; } public static void main(String[] args) {//ServiceClient try { OMElement getName = getNameByNum("1000"); Options options = new Options(); options.setTo(targetEPR); options.setTransportInProtocol(Constants.TRANSPORT_HTTP); ServiceClient sender = new ServiceClient(); sender.setOptions(options); OMElement result = sender.sendReceive(getName); String response = result.getFirstElement().getText(); System.out.println("员工名称为:"+response); } catch (Exception e) { e.printStackTrace(); } }}
员工名称为:Tom
public static void main(String[] args) throws AxisFault { // 使用RPC方式调用WebService RPCServiceClient serviceClient = new RPCServiceClient(); Options options = serviceClient.getOptions(); // 指定调用WebService的URL options.setTo(targetEPR); // 指定getEmployeeName方法的参数值 Object[] opAddEntryArgs = new Object[] {"1001"}; // 指定getEmployeeName方法返回值的数据类型的Class对象 Class[] classes = new Class[] {String.class}; // 指定要调用的getEmployeeName方法及WSDL文件的命名空间 QName opAddEntry = new QName("http://webservice.jynine.com", "getEmployeeName"); // 调用getEmployeeName方法并输出该方法的返回值 System.out.println(serviceClient.invokeBlocking(opAddEntry, opAddEntryArgs, classes)[0]); }
%AXIS2_HOME%\bin\wsdl2java -uri http://localhost:8082/axis2/services/EmployeeInfo?wsdl -p com.jynine.webservice -s -o generator
public static void main(String[] args) throws RemoteException { EmployeeInfoStub stub = new EmployeeInfoStub(); EmployeeInfoStub.GetEmployeeName gg = new EmployeeInfoStub.GetEmployeeName(); gg.setNum("1002"); System.out.println( stub.getEmployeeName(gg).get_return()); }
%AXIS2_HOME%\bin\wsdl2java -uri http://localhost:8082/axis2/services/EmployeeInfo?wsdl -p com.jynine.webservice -d adb -s -o generator
%AXIS2_HOME%\bin\wsdl2java -uri http://localhost:8082/axis2/services/EmployeeInfo?wsdl -p com.jynine.webservice -d xmlbeans -s -o generator
%AXIS2_HOME%\bin\wsdl2java -uri http://localhost:8082/axis2/services/EmployeeInfo?wsdl -p com.jynine.webservice -d jibx -s -uw -o generator