Axis用过一段时间后就听说Axis2横空出世,继而接触了CXF,如果不赶紧整理,怕是都要遗忘了,今天咱就先整理一下Axis1。?
Axis1
终于1.4版本,此后转为
Axis2
。?
先搭建一个简单的Axis1,日后根据需要逐步求精。?
相关链接:?
WebService框架整理(一) Axis1
?
WebService框架整理(二) Axis1+Spring
在开始构建前,我们需要获得以下jar包:?
引用
activation.jar?
axis.jar?
commons-discovery.jar?
commons-logging.jar?
log4j.jar?
jaxrpc.jar?
mail.jar?
wsdl4j.jar?
首先,需要在WEB-INF下构建server-config.wsdd文件,内容如下:?
- <?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">??
- ????transport??
- ????????name="http" ????????requestFlow ????????????handler??
- ????????????????type="java:org.apache.axis.handlers.http.URLMapper"?/>??
- </transport ????????name="local"responseFlow ????????????????type="java:org.apache.axis.transport.local.LocalResponder"?service??
- ????????name="Version"??
- ????????provider="java:RPC"parameter??
- ????????????name="allowedMethods"??
- ????????????value="getVersion"? ????????????name="className"??
- ????????????value="org.apache.axis.Version"?servicedeployment>??
我们将测试Axis1内部的Version服务。?
接下来,我们构建web.xml文件,如下所示:?
web-app??
????xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"??
????xmlns="http://java.sun.com/xml/ns/javaee"??
????xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"??
????xsi:schemaLocation="http://java.sun.com/xml/ns/javaee?http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"??
????id="WebApp_ID"??
????version="2.5"display-name>Apache-Axisservlet>Apache-Axis?Servletservlet-name>axisservlet-class>org.apache.axis.transport.http.AxisServletload-on-startup>0servlet-mappingurl-pattern>/services/*web-app所有以/services/开头的请求都将转交给AxisServlet处理。?
构建一个测试用例,如下所示:?
import?static?org.junit.Assert.*;??
??
import?java.net.URL;??
import?javax.xml.namespace.QName;??
import?org.apache.axis.client.Call;??
import?org.apache.axis.client.Service;??
import?org.junit.Before;??
import?org.junit.Test;??
?
?*?WebService测试?
?*??
?*?@author?梁栋?
?*?@version?1.0?
?*?@since?1.0?
?*/??
public?class?WebServiceTest?{??
????private?String?namespaceUri?=?"http://localhost:8080/axis/services/Version";??
private?String?wsdlUrl?=?namespaceUri?+?"?wsdl";??
?????
?????*??
?????*?@throws?Exception?
?????*/??
????@Test??
final?void?test()?throws?Exception?{??
??????????
????????Service?service?=?new?Service();??
????????Call?call?=?(Call)?service.createCall();??
//?调用?远程方法??
????????call.setOperationName(new?QName(namespaceUri,?"getVersion"));??
//?设置URL??
????????call.setTargetEndpointAddress(new?URL(wsdlUrl));??
//?执行远程调用,同时获得返回值??
????????String?version?=?(String)?call.invoke(new?Object[]?{});??
//?打印信息??
????????System.err.println(version);??
//?验证??
????????assertNotNull(version);??
????}??
}??