Axis2 开发

  • Post author:
  • Post category:其他



1、从官网中下载axis2.war文件,解压缩,获取里面的文件结构,将conf、lib、modules复制到工程中的WEB-INF目录中,axis2-web建议也放到工程WebRoot目录下,因为如果调用webservice失败,没有该文件,系统会报错

2、新建一个类CalculateService

3、修改web.xml

<?xml version=”1.0″ encoding=”UTF-8″?>

<web-app id=”WebApp_ID” version=”2.5″

xmlns=”

http://java.sun.com/xml/ns/javaee



xmlns:xsi=”

http://www.w3.org/2001/XMLSchema-instance



xsi:schemaLocation=”

http://java.sun.com/xml/ns/javaee


http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd

“>

<display-name>linzlWebservice</display-name>

<!–增加以下配置–>

<servlet>

<display-name>Apache-Axis Servlet</display-name>

<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>

</web-app>

4、在

WEB-INF/新建services目录/建

CalculateService类同名目录(方便阅读)/建META-INF目录/建services.xml文件

如果有多个service服务,则必须用serviceGroup 包含起来

第一种方式:

<serviceGroup>

<!–第一种服务发布方式 –>

<service name=”CalculateServiceFirst”>

<parameter name=”ServiceClass”>com.linzl.cn.webservice.CalculateService</parameter>

<!–处理WebService的处理器 –>

<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>

</service>

</serviceGroup>

第二种方式:

<serviceGroup>

<!–第二种服务发布方式 ,指定发布哪些方法–>

<service name=”CalculateServiceSecond”>

<parameter name=”ServiceClass”>com.linzl.cn.webservice.CalculateService</parameter>

<operation name=”plus”>

<messageReceiver class=”org.apache.axis2.rpc.receivers.RPCMessageReceiver” />

</operation>

<operation name=”minus”>

<messageReceiver class=”org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver” />

</operation>

</service>

</serviceGroup>

5、访问


http://IP:端口/工程名/services/

CalculateServiceFirst

?wsdl


http://IP:端口/工程名/services/

CalculateServiceSecond

?wsdl


访问具体某个方法


http://IP:端口/工程名/services/

CalculateServiceFirst

/方法名称?参数名称=&参数名称=


6、注意

org.apache.axis2.AxisFault: The ServiceClass object does not implement the required method in the following form: OMElement getDocSummary(OMElement e)

原因:在services.xml中没有配置对应的返回值类型:

<messageReceivers>

<messageReceiver mep=”http://www.w3.org/2004/08/wsdl/in-out”

class=”org.apache.axis2.rpc.receivers.RPCMessageReceiver” />

</messageReceivers>


Axis2客户端 调用webservice服务(适用于Axis1)

import org.apache.axis2.AxisFault;

import org.apache.axis2.addressing.EndpointReference;

import org.apache.axis2.client.Options;

import org.apache.axis2.rpc.client.RPCServiceClient;

String endpoint = ”

http://localhost:8080/Axis2/services/CalculateServiceFirst?wsdl

“;

//调用的方法名称

String method = “plus”;

// 命名空间,

Axis1

没有可为null(

Axis2必须加上命名空间



String targetNamespace = ”

http://webservice.cn.linzl.com

“;

RPCServiceClient serviceClient = new RPCServiceClient();

Options options = serviceClient.getOptions();

EndpointReference targetEPR = new EndpointReference(endpoint );

options.setTo(targetEPR);

// 在创建QName对象时,QName类的构造方法的第一个参数表示WSDL文件的命名空间名,也就是<wsdl:definitions>元素的targetNamespace属性值

QName opAddEntry = new QName(targetNamespace, method);

Object[] opAddEntryArgs = new Float[] { 1f, 2f };

Class[] classes = new Class[] { Float.class };

// 返回参数类型,这个和axis1有点区别

// invokeBlocking方法有三个参数,其中第一个参数的类型是QName对象,表示要调用的方法名;

// 第二个参数表示要调用的WebService方法的参数值,参数类型为Object[];

// 第三个参数表示WebService方法的返回值类型的Class对象,参数类型为Class[]。

// 当方法没有参数时,invokeBlocking方法的第二个参数值不能是null,而要使用new Object[]{}

// 如果被调用的WebService方法没有返回值,应使用RPCServiceClient类的invokeRobust方法,

// 该方法只有两个参数,它们的含义与invokeBlocking方法的前两个参数的含义相同

Object[] result = serviceClient.invokeBlocking(opAddEntry,

opAddEntryArgs, classes);

System.out.println(result[0].toString());

Axis2生成客户端,调用服务端

进入安装目录E:\Apache\axis\axis2-1.6.2\bin,执行

wsdl2java –noBuildXML -uri

http://32.63.248.17:8080/wxzwptmh/service/wsdept?wsdl

详细用法 wsdl2java  -help查看

AxisServerStub stub = new AxisServerStub();

AxisServerStub.Plus plus = new AxisServerStub.Plus();

plus.setX(1f);

plus.setY(1001.2f);

PlusResponse plusResponse = stub.plus(plus);

System.out.println(plusResponse.get_return());

7、集成spring依赖于服务器端接口的方式(待补充)

8、

org.apache.axis2.AxisFault: Namespace URI may not be null

服务端

缺少wstx-asl-3.2.9.jar


9、


部署websphere注意

1,将modules目录下的mar包都拷贝一份到lib下,然后都重新命名扩展名为jar。

2,在发布到websphere上后不要立刻启动这个应用,启动也报错,

需要修改这个应用的管理模块处的类装载顺序,为 本应用优先



版权声明:本文为wuyu10FG原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。