项目中集成 axis2 发布webservice(转)
在网上找了很多例子,发觉很多都是脱离项目,单独使用axis2包发布webservice接口
实际开发中,一般都是跟随项目启动之后,自动发布接口
接下来简单讲解一下 在项目中集成发布axis2 webservice接口
1.打开axis2.war包,将conf,lib,modules三个文件夹复制到项目的WEB-INF文件夹下
2.新建一个services文件夹,然后在services文件下新建一个文件夹(任意取名),再新建META-INF文件夹,最后再新增services.xml,接口信息就写在这里面。
具体路径:WEB-INF/services/myservice/META-INF/services.xml
3.开发java类
- package Axis2Service.service;
- /**
- * desc:计算俩个数和值的 webservice接口
- *
- * @version SVN $Revision: 1.1 $ $Date: 2011/07/12 02:06:49 $
- */
- public class Calculate {
- public Integer sum(Integer num1, Integer num2) {
- return num1 + num2;
- }
- }
4.现在编写services.xml文件
- <service name="AxisService">
- <description>AxisService</description>
- <parameter name="ServiceClass">
- Axis2Service.service.Calculate
- </parameter>
- <operation name="sum">
- <messageReceiver class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
- </operation>
- </service>
5.修改web.xml
- <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>
5.发布项目,然后在地址栏上输入http://localhost:8080/MyAxis/services/AxisService?wsdl,就能看到发布的项目了哦!!
已上传附件(去除所有jar包),相关axis2的jar包从axis2.war中获得,复制到项目的lib下即可
警告:services文件夹和services.xml文件 名字必须为services
多个服务 service.xml 的配置内容:
<serviceGroup>
<service name="AxisService">
<description>AxisService</description>
<parameter name="ServiceClass">
Axis2Service.service.Calculate
</parameter>
<operation name="sum">
<messageReceiver
class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
</operation>
</service>
<service name="AxisService2">
<description>AxisService</description>
<parameter name="ServiceClass">
Axis2Service.service.Cc
</parameter>
<operation name="ss">
<messageReceiver
class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
</operation>
</service>
</serviceGroup>
附调用方式代码:
package Axis2Service.service;
import javax.xml.namespace.QName;
import javax.xml.rpc.Call;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axis2.AxisFault;
import org.apache.axis2.Constants;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.rpc.client.RPCServiceClient;
public class Cc {
public String ss(String s){
System.out.println(s);
return s;
}
private static RPCServiceClient serviceClient;
/**
* RPC调用AXIS2 webservice
* @param endpoint 服务地址 如:http://192.168.0.1:2597/aixs2/services/jqservice?wsdl
* @param localPart 方法名 如<xs:element name="Receive">
* @param opArgs 方法参数 如Object[] opArgs = new Object[] { param };
* @param namespaceURI 命名空间 如 :targetNamespace="http://server.test.com.cn">
* @param opReturnType 返回类型 如字符串:Class[] opReturnType = new Class[] { String[].class };
*/
public static String axis2RPCInvoke(String endpoint,String localPart,Object[] opArgs,String namespaceURI,Class[] opReturnType)
{
Object[] ret = null;
try
{
serviceClient = new RPCServiceClient();
Options options = serviceClient.getOptions();
EndpointReference targetEPR = new EndpointReference(endpoint);
options.setTo(targetEPR);
QName opQName = new QName(namespaceURI, localPart);
ret = serviceClient.invokeBlocking(opQName, opArgs, opReturnType);
System.out.println(((String[]) ret[0])[0]);
}
catch (AxisFault e)
{
e.printStackTrace();
}
return ((String[]) ret[0])[0];
}
public static void main(String[] args){
String s=axis2RPCInvoke("http://localhost:8080/wt/services/AxisService2?wsdl", "ss", new Object[] {"122"}, "http://service.Axis2Service", new Class[] { String[].class });
System.out.println(s);
}
}
上一篇: 林芝让我爱上这里的一切美好的风景!
下一篇: 旅游出行 机场危险要关注