AXIS2发布webservice有2中方式,一种是打包成aar另一中是直接发布web项目就可以,来让我们一起学习一下:
第一步:添加jar包 (下载地址:) http://download.csdn.net/detail/qq_36291682/9720214
导入jar包:
activation-1.1.jar
antlr-2.7.7.jar
apache-mime4j-core-0.7.2.jar
axiom-api-1.2.20.jar
axiom-dom-1.2.20.jar
axiom-impl-1.2.20.jar
axiom-jaxb-1.2.20.jar
axis2-adb-1.7.4.jar
axis2-adb-codegen-1.7.4.jar
axis2-clustering-1.7.4.jar
axis2-codegen-1.7.4.jar
axis2-corba-1.7.4.jar
axis2-fastinfoset-1.7.4.jar
axis2-java2wsdl-1.7.4.jar
axis2-jaxbri-1.7.4.jar
axis2-jaxws-1.7.4.jar
axis2-jibx-1.7.4.jar
axis2-json-1.7.4.jar
axis2-kernel-1.7.4.jar
axis2-metadata-1.7.4.jar
axis2-mtompolicy-1.7.4.jar
axis2-saaj-1.7.4.jar
axis2-soapmonitor-servlet-1.7.4.jar
axis2-spring-1.7.4.jar
axis2-transport-http-1.7.4.jar
axis2-transport-local-1.7.4.jar
axis2-xmlbeans-1.7.4.jar
bcel-findbugs-6.0.jar
commons-cli-1.2.jar
commons-codec-1.3.jar
commons-fileupload-1.3.1.jar
commons-httpclient-3.1.jar
commons-io-2.1.jar
commons-logging-1.1.1.jar
geronimo-annotation_1.0_spec-1.1.jar
geronimo-jaxws_2.2_spec-1.0.jar
geronimo-jta_1.1_spec-1.1.jar
geronimo-saaj_1.3_spec-1.0.1.jar
geronimo-stax-api_1.0_spec-1.0.1.jar
geronimo-ws-metadata_2.0_spec-1.1.2.jar
gson-2.1.jar
httpclient-4.5.2.jar
httpcore-4.4.4.jar
jaxb-api-2.2.6.jar
jaxb-impl-2.2.6.jar
jaxb-xjc-2.2.6.jar
jaxen-1.1.6.jar
jaxws-tools-2.2.6.jar
jettison-1.3.jar
jibx-bind-1.2.jar
jibx-run-1.2.jar
jsr311-api-1.1.1.jar
juli-6.0.16.jar
log4j-1.2.15.jar
mail-1.4.jar
mex-1.7.4-impl.jar
neethi-3.0.3.jar
stax2-api-3.1.1.jar
tribes-6.0.16.jar
woden-core-1.0M10.jar
woodstox-core-asl-4.2.0.jar
wsdl4j-1.6.2.jar
xalan-2.7.0.jar
xml-resolver-1.2.jar
xmlbeans-2.5.0.jar
xmlschema-core-2.2.1.jar
第二步:编写方法类
public class TestImpl {
public String sayHello(String name){
System.out.println("不是我聪明 就是你笨!");
return "hello:"+name;
}
}
第三步:修改web.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<servlet>
<servlet-name>AxisServlet</servlet-name>
<servlet-class>org.apache.axis2.transport.http.AxisServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>AxisServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>/index.jsp</welcome-file>
</welcome-file-list>
</web-app>
第四步: 增加WEN-INF内容 这个很重要
将axis.war解压下的WEN-INF文件夹内的conf、modules复制到项目WEB-INF下(axis.war 下载地址http://download.csdn.net/detail/qq_36291682/9720217 )
第五步,在WEB-INF下创建文件夹services(名字不可改),
在该目录下创建文件夹(名称随意),
在该目录下创建文件夹META-INF(名称不可改),
在该目录下创建文件services.xml(名称不可改),该文件内容为:
<?xml version="1.0" encoding="UTF-8"?>
<service name="axisDemo">
<description>
Web Service
</description>
<parameter name="ServiceClass">com.TestImpl</parameter>
<messageReceivers>
<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"
class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only"
class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" />
</messageReceivers>
<operation name="sayHello">
<messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
<!-- 有返回值 -->
</operation>
<!-- <operation name="sss">
<messageReceiver class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver"/>
// 无返回值
</operation> -->
</service>
最后一步当然就是发布项目 访问:
http://localhost:端口/项目名称/services/axisDemo?wsdl
客户端调用方式:
package com;
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 TestClient {
public static void main(String[] args) throws AxisFault {
try {
//实体类
RPCServiceClient client = new RPCServiceClient();
Options options = client.getOptions();
String url = "http://localhost:9999/test/services/axisDemo?wsdl";
EndpointReference end = new EndpointReference(url);
options.setTo(end);
Object[] obj = new Object[] { "sss"};
Class<?>[] classes = new Class[] { String.class };
//命名空间 方法名
QName qname = new QName("http://com", "sayHello");
System.out.println(client.invokeBlocking(qname, obj, classes)[0]);
} catch (AxisFault e) {
e.printStackTrace();
}
}
}