使用Axis2创建Web Service
参考地址:http://jingyan.baidu.com/article/ce09321b5546662bff858f21.html
1 下载axis2插件
从官网中下载插件axis2-eclipse-codegen-plugin-1.6.2.zip和axis2-eclipse-service-plugin-1.6.2.zip,解压缩,得到org.apache.axis2.eclipse.codegen.plugin_1.6.2.jar和org.apache.axis2.eclipse.service.plugin_1.6.2.jar
Service Archive Wizard下载地址:
http://www.apache.org/dyn/mirrors/mirrors.cgi/axis/axis2/java/core/1.6.2/axis2-eclipse-service-plugin-1.6.2.zip
Code Generator wizard下载地址:
http://www.apache.org/dyn/mirrors/mirrors.cgi/axis/axis2/java/core/1.6.2/axis2-eclipse-codegen-plugin-1.6.2.zip
2 安装插件
将这两个jar包,放到MyEclipse-8.6\dropins目录下,重启eclipse,在File-->New-->Other下会发现Axis2 Wizards
3 部署
部署axis2很简单,将axis2.war复制到tomcat\webapps目录下,重启tomcat即可,此时打开tomcat目录,会发现在webapps中多了一个axis2的工程名,这个就是我们要对外发布的webservice服务工程
4 创建webservice服务
4.1 新建一个web工程,创建一个类Greeting,用于当作webservice服务
代码如下:
package amyservices; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.List; import Com.WebService.Model.Person; public class Greeting { public String getGreeting(String name){ return "你好:" + name; } public int getPrice() { return new java.util.Random().nextInt(1000); } public List<Person> GetPersonList() { List<Person> list = new ArrayList<Person>(); Person model = new Person(); model.setKeyID("1214324234234234324"); model.setName("張三"); Calendar birth = Calendar.getInstance(); birth.set(1990, 11, 22); model.setBirth(birth); list.add(model); model = new Person(); model.setKeyID("1214324234234234324"); model.setName("李四"); birth = Calendar.getInstance(); birth.set(1990, 11, 22); model.setBirth(birth); System.out.println(birth.toString()); list.add(model); for(Person item : list) { System.out.println(item.getName()); } return list; } public String PrintNow() { Calendar cale = Calendar.getInstance(); Date tasktime=cale.getTime(); SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); System.out.println(df.format(tasktime)); return df.format(tasktime); } }
.classpath文件如下:
<?xml version="1.0" encoding="UTF-8"?> <classpath> <classpathentry kind="src" path="src"/> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"> <attributes> <attribute name="owner.project.facets" value="java"/> </attributes> </classpathentry> <classpathentry kind="con" path="com.genuitec.runtime.library/com.genuitec.generic_7.0"> <attributes> <attribute name="owner.project.facets" value="jst.web"/> </attributes> </classpathentry> <classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.web.container"/> <classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.module.container"/> <classpathentry kind="con" path="com.genuitec.runtime.library/com.genuitec.jstl_1.2.2"> <attributes> <attribute name="org.eclipse.jst.component.dependency" value="WEB-INF/lib"/> <attribute name="owner.project.facets" value="jst.web.jstl"/> </attributes> </classpathentry> <classpathentry kind="output" path="WebRoot/WEB-INF/classes"/> </classpath>
4.2 点击File-->New-->other,选择Axis2 Service Archiver
配置Class File Location,配置路径到工程的WEB-INF\classess目录下,同时将下图的复选框勾选去掉,继续根据向导完成创建。
创建成功,在axis2的工程目录下,多了我们刚配置好的文件
输入网址http://localhost:8080/axis2/services/listServices,会发现增加了我们刚创建的服务。
5 创建客户端
5.1 创建一个java项目
5.2 新建一个WsClient测试类
代码如下:
package com.amy.client; import javax.xml.namespace.QName; import org.apache.axis2.AxisFault; import org.apache.axis2.addressing.EndpointReference; import org.apache.axis2.client.Options; import org.apache.axis2.rpc.client.RPCServiceClient; public class WsClient { private RPCServiceClient serviceClient; private Options options; private EndpointReference targetERP; public WsClient(String endpoint) throws AxisFault{ this.serviceClient = new RPCServiceClient(); this.options = this.serviceClient.getOptions(); this.targetERP = new EndpointReference(endpoint); this.options.setTo(this.targetERP); } public Object[] invokeOp(String targetNamespace, String opName,Object[] opArgs, Class<?>[]opReturnType) throws AxisFault, ClassNotFoundException{ QName opQName = new QName(targetNamespace, opName); return this.serviceClient.invokeBlocking(opQName, opArgs, opReturnType); } public static void main(String args[]) { final String endpointReference = "http://localhost:8080/axis2/services/WebServiceTest1"; final String targetNamesPace = "http://amyservices"; try { WsClient client = new WsClient(endpointReference); String opName = "GetPersonList"; Object[] opArgs = new Object[]{}; Class<?>[] opReturnType = new Class[]{Person.class}; Object[] response = client.invokeOp(targetNamesPace, opName, opArgs, opReturnType); if (response != null && response.length > 0) { System.out.println(response.length); for(Object obj : response) { Person model = (Person)obj; System.out.println(model.getName()); } } } catch (AxisFault e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
5.3 要引用的jar包有
.classpath文件如下:
<?xml version="1.0" encoding="UTF-8"?> <classpath> <classpathentry kind="src" path="src"/> <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/> <classpathentry kind="lib" path="lib/axiom-impl-1.2.13.jar"/> <classpathentry kind="lib" path="lib/axis2-adb-1.6.2.jar"/> <classpathentry kind="lib" path="lib/axis2-kernel-1.6.2.jar" sourcepath="C:/Users/zhujinrong/.m2/repository/org/apache/axis2/axis2-kernel/1.6.2/axis2-kernel-1.6.2-sources.jar"/> <classpathentry kind="lib" path="lib/axis2-transport-http-1.6.2.jar"/> <classpathentry kind="lib" path="lib/axis2-transport-local-1.6.2.jar"/> <classpathentry kind="lib" path="lib/commons-codec-1.3.jar"/> <classpathentry kind="lib" path="lib/commons-httpclient-3.1.jar"/> <classpathentry kind="lib" path="lib/commons-logging-1.1.1.jar"/> <classpathentry kind="lib" path="lib/httpcore-4.0.jar"/> <classpathentry kind="lib" path="lib/neethi-3.0.2.jar"/> <classpathentry kind="lib" path="lib/wsdl4j-1.6.2.jar"/> <classpathentry kind="lib" path="lib/XmlSchema-1.4.7.jar"/> <classpathentry kind="lib" path="lib/axiom-api-1.2.13.jar"/> <classpathentry kind="lib" path="lib/activation-1.1.jar"/> <classpathentry kind="lib" path="lib/axis2-adb-codegen-1.6.2.jar"/> <classpathentry kind="lib" path="lib/axis2-java2wsdl-1.6.2.jar"/> <classpathentry kind="lib" path="lib/mail-1.4.jar"/> <classpathentry kind="lib" path="lib/woden-api-1.0M9.jar"/> <classpathentry kind="lib" path="lib/woden-impl-commons-1.0M9.jar"/> <classpathentry kind="lib" path="lib/woden-impl-dom-1.0M9.jar"/> <classpathentry kind="lib" path="lib/wstx-asl-3.2.9.jar"/> <classpathentry kind="lib" path="lib/xmlbeans-2.3.0.jar"/> <classpathentry kind="output" path="bin"/> </classpath>
6 执行结果
6.1 查看服务器启动
6.2 客户端
6.3 服务端情况
6.4 直接查询
可以看到遗留一个问题,客户端查询出来不是一个列表,本来应该是一个列表的。
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。