myEclipse6.5下配置Axis2并发布webservice
近期项目需要发布一个对外webservice接口,因为之前没弄过,所以在网上找了很多资料,最终找到了一个适合自已的。
目标:发布一个对外webservice接口(整合myeclipse6.5+axis2)
开发环境:myeclipse6.5+tomcat6
所用插件:axis2的1.3版本
一、安装axis2
1、下载 axis2插件及jar包,我用的版本是1.3,大家直接从下面的附件中下载就好了。
2、下载完后,直接解压成Axis2_Codegen_Wizard_1.3.0、Axis2_Service_Archiver_1.3.0文件夹,放入myeclipse6.5安装目录的plugins目录下,然后重启myeclipse6.5就好了。
3、查看是否成功:打开File->New->Other,会看见如下弹出框:
二、发布webService准备
1、web.xml中添加配置
<servlet> <servlet-name>AxisServlet</servlet-name> <servlet-class>org.apache.axis.transport.http.AxisServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>AxisServlet</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping>
<?xml version="1.0" encoding="UTF-8"?><deployment xmlns="http://xml.apache.org/axis/wsdd/" xmlns:java="http://xml.apache.org/axis/wsdd/providers/java"> <globalConfiguration> <parameter name="adminPassword" value="admin"/> <parameter name="attachments.Directory" value="./attachments"/> <parameter name="attachments.implementation" value="org.apache.axis.attachments.AttachmentsImpl"/> <parameter name="sendXsiTypes" value="true"/> <parameter name="sendMultiRefs" value="true"/> <parameter name="sendXMLDeclaration" value="true"/> <parameter name="axis.sendMinimizedElements" value="true"/> <requestFlow> <handler type="java:org.apache.axis.handlers.JWSHandler"> <parameter name="scope" value="session"/> </handler> <handler type="java:org.apache.axis.handlers.JWSHandler"> <parameter name="scope" value="request"/> <parameter name="extension" value=".jwr"/> </handler> </requestFlow> </globalConfiguration> <handler name="LocalResponder" type="java:org.apache.axis.transport.local.LocalResponder"/> <handler name="URLMapper" type="java:org.apache.axis.handlers.http.URLMapper"/> <handler name="Authenticate" type="java:org.apache.axis.handlers.SimpleAuthenticationHandler"/> <service name="AdminService" provider="java:MSG"> <parameter name="allowedMethods" value="AdminService"/> <parameter name="enableRemoteAdmin" value="false"/> <parameter name="className" value="org.apache.axis.utils.Admin"/> <namespace>http://xml.apache.org/axis/wsdd/</namespace> </service> <service name="Version" provider="java:RPC"> <parameter name="allowedMethods" value="getVersion"/> <parameter name="className" value="org.apache.axis.Version"/> </service> <!-- 新添加的service接口: name表示接口名称 --> <service name="MyMethod" provider="java:RPC"> <!-- value中添加对外可以调用的方法 --> <parameter name="allowedMethods" value="method1,method2"/> <!-- value中添加类在包中所在位置 --> <parameter name="className" value="com.test.Method"/> </service><transport name="http"> <requestFlow> <handler type="URLMapper"/> <handler type="java:org.apache.axis.handlers.http.HTTPAuthHandler"/> </requestFlow> </transport> <transport name="local"> <responseFlow> <handler type="LocalResponder"/> </responseFlow> </transport></deployment>
import org.apache.axis.client.Call;import org.apache.axis.client.Service;public class TestClient {public static void main(String[] args) throws Exception {// 指出service所在URLString endpoint = "http://127.0.0.1:8080/test/services/MyMethod";// 创建一个服务(service)调用(call)Service service = new Service();Call call = (Call) service.createCall();// 通过service创建call对象// 设置service所在URLcall.setTargetEndpointAddress(new java.net.URL(endpoint));// 方法名(processService)与MyService.java方法名保持一致call.setOperationName("method2");// Object 数组封装了参数,参数为"This is Test!",调用processService(String arg)String ret = (String) call.invoke(new Object[] { "123", "456" });System.out.println(ret);}}