以前用过Axis2的,但是想再次用的时候居然还得花大半天时间,哭了都,老老实实做个笔记吧。
本示例是在 Axis2 通过 外部 Spring容器 获取指定的实现Bean,以提供服务。全部自己写WSDL和服务实现类,不使用 Axis2提供的 Java2WSDL 和 WSDL2Java 工具(生成的太丑,太臃肿)。
|
文件: |
SampleAxis2SpringPojoWebService.zip |
大小: |
13KB |
下载: |
下载 | |
压缩包中省略了 axis2-web中所有的JSP,和Axis2、Spring及其所以来的jar包,请自行追加。
README.txt
Axis2 工程一般的目录结构
SampleWebApp ┣ * ┣ META-INF/* ┗ WEB-INF/ ┣ classes/* ┣ lib/* ┣ conf ┃ ┗ axis2.xml ┣ modules/ ┃ ┣ modules.list ┃ ┗ *.mar ┗ services ┣ services.list ┣ *.aar ┗ sampleWebService (sampleWebService.aar) ┣ * ┗ META-INF ┣ services.wsdl ┗ service.xml
----------------------------------------- axis2.xml axis2.xsd (axis2-1.4.1-src.zip\axis2-1.4.1\modules\kernel\resources\) ----------------------------------------- service.xml services.xsd (axis2-1.4.1-src.zip\axis2-1.4.1\modules\kernel\resources\) 如果你使用Axis2在 0.91 版本以上,你可以不使用 ARR 压缩文件,而直接创建一个相应的目录结构即可。 ----------------------------------------- services.wsdl 该文件的主文件名可以任意 注意: 的 name 属性的值 必须为 services.wsdl 父目录(META-INF)的父目录名(sampleWebService)
示例工程 SampleAxis2SpringPojoWebService 运行步骤
拷贝以下Jar文件到 SampleAxis2SpringPojoWebService/WebContent/WEB-INF/lib 目录下: axis2-1.5.1-bin.zip/axis2-1.5.1/lib/*.jar spring-framework-2.5.6-with-dependencies.zip/spring-framework-2.5.6/dist/spring.jar spring-framework-2.5.6-with-dependencies.zip/spring-framework-2.5.6/dist/modules/*.jar spring-framework-2.5.6-with-dependencies.zip/spring-framework-2.5.6/lib/*/*.jar 拷贝以下Jar文件到 SampleAxis2SpringPojoWebService/WebContent/axis2-web/ 目录下: axis2-1.5.1-bin.zip/axis2-1.5.1/webapp/axis2-web
|
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="" xmlns="" xmlns:web="/web-app_2_5.xsd" xsi:schemaLocation=" /web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>SampleAxis2SpringPojoWebService</display-name>
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param>
<servlet> <servlet-name>AxisServlet</servlet-name> <servlet-class>org.apache.axis2.transport.http.AxisServlet</servlet-class> <!--<init-param> --> <!--<param-name>axis2.xml.path</param-name> --> <!--<param-value>/WEB-INF/conf/axis2.xml</param-value> --> <!--<param-name>axis2.xml.url</param-name> --> <!--<param-value>http://localhot/myrepo/axis2.xml</param-value> --> <!--<param-name>axis2.repository.path</param-name> --> <!--<param-value>/WEB-INF</param-value> --> <!--<param-name>axis2.repository.url</param-name> --> <!--<param-value>http://localhot/myrepo</param-value> --> <!--</init-param> --> <load-on-startup>1</load-on-startup> </servlet> <servlet> <servlet-name>AxisAdminServlet</servlet-name> <servlet-class>org.apache.axis2.transport.http.AxisAdminServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>AxisServlet</servlet-name> <url-pattern>/servlet/AxisServlet</url-pattern> </servlet-mapping>
<servlet-mapping> <servlet-name>AxisServlet</servlet-name> <url-pattern>*.jws</url-pattern> </servlet-mapping>
<servlet-mapping> <servlet-name>AxisServlet</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping>
<servlet-mapping> <servlet-name>AxisAdminServlet</servlet-name> <url-pattern>/axis2-admin/*</url-pattern> </servlet-mapping>
<welcome-file-list> <welcome-file>index.jsp</welcome-file> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list>
</web-app>
|
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "">
<beans> <bean id="applicationContext" class="org.apache.axis2.extensions.spring.receivers.ApplicationContextHolder" />
<bean id="myAddServiceBean" class="me.sample.MyAddServiceImpl"> </bean> </beans>
|
services.xml
<?xml version="1.0" encoding="UTF-8"?> <!-- 该XSD文件可以在axis2-1.4.1-src.zip文件的以下目录下找到: axis2-1.4.1\modules\kernel\resources\ javadoc: org.apache.axis2.description.AxisService index-all.html
Contract First Development with Axis2 (Part II) http://wso2.org/library/2935
Tutorial: Contract-First Web Services with Apache Axis2 http://www.roseindia.net/software-tutorials/detail/17781 <!DOCTYPE service SYSTEM "services.xsd"> -->
<service name="myAddService"> <description>this is a Sample by btpka3.com.</description>
<parameter name="ServiceObjectSupplier"> org.apache.axis2.extensions.spring.receivers.SpringServletContextObjectSupplier </parameter> <!-- <parameter name="ServiceObjectSupplier"> org.apache.axis2.extensions.spring.receivers.SpringAppContextAwareObjectSupplier </parameter> --> <parameter name="SpringBeanName">myAddServiceBean</parameter> <parameter name="ServiceClass">me.sample.MyAddService</parameter>
<parameter name="useOriginalwsdl">true</parameter> <parameter name="modifyUserWSDLPortAddress">true</parameter> <messageReceivers> <messageReceiver mep="" class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" /> <messageReceiver mep="" class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" /> </messageReceivers> </service>
|
services.wsdl
<?xml version="1.0" encoding="UTF-8" ?> <!-- name="myAddWebService" standalone="no"--> <wsdl:definitions xmlns:wsdl="" xmlns:tns="" xmlns:xsd="" xmlns:soap="soap/" targetNamespace=""> <wsdl:documentation>myAdddfdiiiioooooooddee</wsdl:documentation> <wsdl:types> <xsd:schema targetNamespace=""> <xsd:element name="myAdd"> <xsd:complexType> <xsd:sequence> <xsd:element name="param0" type="xsd:float" /> <xsd:element name="param1" type="xsd:float" /> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name="myAddResponse"> <xsd:complexType> <xsd:sequence> <xsd:element name="return" type="xsd:float"/> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema> </wsdl:types> <wsdl:message name="myAddRequest"> <wsdl:part name="parameters" element="tns:myAdd"/> </wsdl:message> <wsdl:message name="myAddResponse"> <wsdl:part name="parameters" element="tns:myAddResponse"/> </wsdl:message> <wsdl:portType name="myAddPortType"> <wsdl:operation name="myAdd"> <wsdl:input message="tns:myAddRequest" /> <wsdl:output message="tns:myAddResponse" /> </wsdl:operation> </wsdl:portType> <wsdl:binding name="myAddSOAP11Binding" type="tns:myAddPortType"> <soap:binding transport="" style="document"/> <wsdl:operation name="myAdd"> <soap:operation soapAction="urn:myAdd"/> <wsdl:input> <soap:body use="literal"/> </wsdl:input> <wsdl:output> <soap:body use="literal"/> </wsdl:output> </wsdl:operation> </wsdl:binding>
<wsdl:service name="myAddService"> <wsdl:port name="myAddPort" binding="tns:myAddSOAP11Binding"> <soap:address location=""/> </wsdl:port> </wsdl:service> </wsdl:definitions>
|
MyAddService.java
package me.sample;
/** * 一个示例用Web Service的接口。该接口中的方法与WSDL上公布的方法原型一致。 * * @author btpka3@163.com * */ public interface MyAddService {
/** * 用于计算两个数的和。 * * @param num1 * 数值1 * @param num2 * 数值2 * @return num1 与num2 的和 */ float myAdd(float num1, float num2); }
|
MyAddServiceImpl.java
package me.sample;
/** * 这是Web Service接口的实现类,可以额外拥有其他一些不公开的方法。 * * @author btpka3@163.com * */ public class MyAddServiceImpl implements MyAddService {
/** * {@inheritDoc} */ @Override public float myAdd(float num1, float num2) { System.out.println("INVOKED : num1="+num1+", num2="+num2); return num1 + num2; } }
|
MyClient.java
package me.sample;
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 MyClient { public static void main(String[] args1) throws AxisFault {
// Step 1. 创建 Client
RPCServiceClient serviceClient = new RPCServiceClient();
// Step 2. 访问设定(路径等)
Options options = serviceClient.getOptions(); EndpointReference targetEPR = new EndpointReference( ""); options.setTo(targetEPR);
// Step 3.1. 准备方法调用(设定方法名)
QName opGetWeather = new QName("", "myAdd");
// Step 3.2. 准备方法调用(设定参数)
float num1 = 3; float num2 = 2; Object[] opGetWeatherArgs = new Object[] { num1, num2 };
// Step 3.3. 准备方法调用(设定返回值类型)
@SuppressWarnings("unchecked") Class[] returnTypes = new Class[] { Float.class };
// Step 4. 执行方法调用
Object[] response = serviceClient.invokeBlocking(opGetWeather, opGetWeatherArgs, returnTypes);
// Step 5. 取的返回值
Float result = (Float) response[0];
// Step 6. 检查结果并输出
if (result == null) { System.out.println("result is null!"); } else { System.out.println("result = " + num1 + " + " + num2 + " = " + result); } } }
|
阅读(1342) | 评论(0) | 转发(0) |