进公司实习要对以前的项目进行维修,设计到WebService,接下来对axis2框架发布web项目开发webservice和接口调用进行展开。因为是以前的技术,我真的搜了好久。太难了,不过还是要分享出来
下载地址:http://axis.apache.org/axis2/java/core/download.html
下载这两个即可
bin包:是安装包;
war包:用于在tomcat上发布服务;
service-plugin插件:用于将服务打包成为后缀名为.aar的文件;codegen-plugin插件:用来将服务代码生成wsdl文件以及解析wsdl文件并生成客户端代码文件(stub文件)
下载结束后
将之前下载的war包解压后的axis2.war放到tomcat的webapps目录下,然后启动tomcat
在地址栏输入:http://localhost:8080/axis2/,如下图表示成功
package com;
public class HelloWorld {
public String getHello(String name) {
return "Hello, " + name + ".";
}
public String getWorld(String name) {
return "World," + name + ".";
}
public String getHelloWorld() {
return "Hello,World";
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!-- 加载Axis -->
<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/*
把tomcat安装目录下的webapps/axis2/WEB-INF下的modules、service和conf文件件拷至HelloWorld下的WEB-INF目录下。把lib下的如下jar包也拷过去。然后在services下新建HelloWorld/META-INF路径,META-INF下新建services.xml,内容如下:
目录结构
services.xml如下
<?xml version="1.0" encoding="UTF-8"?>
<service name="HelloWorld">
<description>
HelloWorld Service Example
</description>
<parameter name="ServiceClass">
com.HelloWorld
</parameter>
<operation name="getHello">
<messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
</operation>
<operation name="getWorld">
<messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
</operation>
<operation name="getHelloWorld">
<!-- 这里要注意,当没有返回值时才用
org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver,没有参数还是用RPCMessageReceiver-->
<messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
</operation>
</service>
一定要再写一个ServiceData.xml不然后续会报错
<?xmlversion="1.0"encoding="UTF-8"?>
<!--此文件必须存在,否则执行异常 -->
<ServiceData>
</ServiceData>
对于service标签的一些解释
在services.xml文件中,我们可以直接在service节点下定义参数,这些参数供消息上下文(在运行时)、AxisService或者AxisOperation访问。参数有一个必选参数和可选参数:参数名称是必选参数。这里的服务参数为指定服务类。
Axis2中消息接收器是特殊的处理器,是In路径(请求路径)中的最后一个处理器。Web服务中的每个操作都有他自己的消息接收器,而且不同的操作可以有不同的消息接收器。消息接收器是依赖于消息交换模式的,所以我们必须为不同的消息交换模式指定不同的消息接收器。
怎样才能给所有的操作指定相同的消息接收器呢?只要添加服务级消息接收器即可。如此我们就不必在操作级别指定消息接收器了。我们要做的是指定服务级消息接收器。而在部署时,Axis2会自动给操作选择正确的消息接收器。这里我们指定
Operation 级消息接收器
前文描述了如何指定服务级消息接收器。但是,我们也可以为不同的操作指定不同的消息接收器,这需要在operation中指定messageReceiver标签
最后说明一个编写用于部署服务组的services.xml文件的问题
要在单个服务包文件中部署多个服务,服务组是一个便捷方法。当然,这些服务之间应该存在逻辑关系。用于服务组的services.xml文件和用于单个服务的,它们之间唯一的区别就是根元素。用于服务组的,根元素是serviceGroup,我们可以在serviceGroup元素内部定义多个service元素。
<serviceGroup>
<service name=service1>
......
<service>
<service name=service2>
..........
</service>
</serviceGroup>
访问http://localhost:8080/WebserviceWeather_war_exploded/services/HelloWorld?wsdl
package com.client;
import javax.xml.namespace.QName;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.rpc.client.RPCServiceClient;
public class ClientTest {
public static void main(String[] args) {
String url = "http://localhost:8080/WebserviceWeather_war_exploded/services/HelloWorld";
String result = null;
try {
// 使用RPC方式调用WebService
RPCServiceClient serviceClient = new RPCServiceClient();
Options options = serviceClient.getOptions();
// 指定调用WebService的URL
EndpointReference targetEPR = new EndpointReference(url);
options.setTo(targetEPR);
// 在创建QName对象时,QName类的构造方法的第一个参数表示WSDL文件的命名空间名,也就是元素的targetNamespace属性值
// // 指定要调用的getWorld方法及WSDL文件的命名空间.....
QName opAddEntry = new QName("http://com", "getWorld");
//
// 指定getGreeting方法的参数值,如果有多个,继续往后面增加即可,不用指定参数的名称
Object[] opAddEntryArgs = new Object[] { "java" };
// 返回参数类型,这个和axis1有点区别
// invokeBlocking方法有三个参数,其中第一个参数的类型是QName对象,表示要调用的方法名;
// 第二个参数表示要调用的WebService方法的参数值,参数类型为Object[];
// 第三个参数表示WebService方法的返回值类型的Class对象,参数类型为Class[]。
// 当方法没有参数时,invokeBlocking方法的第二个参数值不能是null,而要使用new Object[]{}
// 如果被调用的WebService方法没有返回值,应使用RPCServiceClient类的invokeRobust方法,
// 该方法只有两个参数,它们的含义与invokeBlocking方法的前两个参数的含义相同
// 指定getGreeting方法返回值的数据类型的Class对象.....
Class[] classes = new Class[] { String.class };
// 调用getGreeting方法并输出该方法的返回值.......
result = (String) serviceClient.invokeBlocking(opAddEntry,
opAddEntryArgs, classes)[0];
System.out.println(result);
// 下面是调用getHello方法的代码,这些代码与调用getWorld方法的代码类似
// classes = new Class[] {String.class};
opAddEntry = new QName("http://com", "getHello");
opAddEntryArgs = new Object[] { "曹胜欢" };
System.out.println(serviceClient.invokeBlocking(opAddEntry,
opAddEntryArgs, classes)[0]);
// 下面是调用getHelloWorld方法的代码
opAddEntry = new QName("http://com", "getHelloWorld");
System.out.println(serviceClient.invokeBlocking(opAddEntry,
new Object[]{}, classes)[0]);
} catch (Exception e) {
e.printStackTrace();
}
}
}
Axis2开发webservice详解
axis2发布web项目开发webservice和接口调用