一、下载jar包
axis2-1.6.3下载:
下载地址:http://axis.apache.org/axis2/java/core/download.cgi,
下载axis2-1.6.3-bin.zip,此压缩包包含了所有axis2的jar
spring3.1.4下载我就不废话了
二、创建整合工程并发布测试
1.在搭好的spring MVC框架中,引入axis2的jar
2.在WEB-INF下创建services文件夹,,名字一定是services,其他不能识别,在services一定要再创建一个文件夹(名字顺便,例如:MyService), 在MyService下创建一个文件夹,名字一定是META-INF,在META-INF下创建services.xml文件和一个空的ServiceData.xml,这个名字也是不变的。
最后路径是WEB-INF/services/MyService/META-INF/services.xml services.xml代码如下:
<?xml version="1.0" encoding="UTF-8"?> <!-- 访问地址中的访问那个webservice的名字 --> <service name="TestWSService"> <description>Spring 整合例子 </description> <parameter name="ServiceObjectSupplier"> org.apache.axis2.extensions.spring.receivers.SpringServletContextObjectSupplier </parameter> <!-- 配置在applicationContext中配置的bean,,这里的值域bean中的id事一样的,否则就报错找不到bean错 --> <parameter name="SpringBeanName">testWSService</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> </service>
<?xml version="1.0" encoding="UTF-8"?> <ServiceData> </ServiceData>三、在web.xml中加入以下信息
<!--axis2 WebService配置信息开始--> <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> <!-- axis2 WebService配置信息结束-->
四、在applicationContext.xml中注入引用的service
<bean id="testWSService" class="com.usi.jrtx.service.webservice.TestWSService"></bean>
五、TestWSService.java文件
package com.usi.jrtx.service.webservice; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import com.github.miemiedev.mybatis.paginator.domain.Order; import com.github.miemiedev.mybatis.paginator.domain.PageBounds; import com.usi.jrtx.dao.account.AccountMapper; import com.usi.jrtx.model.account.Account; public class TestWSService { @Autowired private AccountMapper accountMapper; /** * 计算減法 d1-d2 */ public Integer jianfa(Integer d1, Integer d2) { return d1-d2; } /** * 计算加法 */ public Integer jiafa(Integer d1, Integer d2) { PageBounds pageBounds = new PageBounds(1,1000,Order.formString("id.desc")); Account account=new Account(); List<Account> list=accountMapper.queryAccounts(account, pageBounds); System.out.println("接收d1:"+d1); System.out.println("接收d2:"+d2); for (int i = 0; i < list.size(); i++) { System.out.println("账户名:"+list.get(i).getAccountName()); } return d1+d2; } }
六、客户端使用
package com.usi.jrtx.webservice; import java.util.ArrayList; import java.util.List; import java.util.Random; import javax.xml.namespace.QName; import org.apache.axiom.om.OMElement; import org.apache.axis2.AxisFault; import org.apache.axis2.addressing.EndpointReference; import org.apache.axis2.client.Options; import org.apache.axis2.rpc.client.RPCServiceClient; import com.usi.jrtx.model.soap.AccountVO81010; import com.usi.jrtx.model.soap.ConsumeDetail; import com.usi.jrtx.model.soap.ConsumeVO88010; import com.usi.jrwl.util.XMLDomUtil; public class WebserviceTest { /** * 测试 * @throws Exception */ public static void my() throws Exception { String url = "http://192.168.20.34:8080/jrtxManager/services/TestWSService?wsdl"; try { // 使用RPC方式调用WebService RPCServiceClient serviceClient = new RPCServiceClient(); // 指定调用WebService的URL EndpointReference targetEPR = new EndpointReference(url); Options options = serviceClient.getOptions(); // 确定目标服务地址 options.setTo(targetEPR); // 确定调用方法 options.setAction("urn:jiafa"); options.setTimeOutInMilliSeconds(60000); QName qname = new QName("http://webservice.service.jrtx.usi.com","jiafa"); // 指定jiafa方法的参数值 String requestContent = ""; try { } catch (Exception e) { throw new Exception(e.getMessage(), e.getCause()); } Object[] parameters = new Object[] {5,9 }; // 指定getPrice方法返回值的数据类型的Class对象 Class[] returnTypes = new Class[] { String.class }; // 调用方法一 传递参数,调用服务,获取服务返回结果集 OMElement element = serviceClient.invokeBlocking(qname, parameters); // 值得注意的是,返回结果就是一段由OMElement对象封装的xml字符串。 String result = element.getFirstElement().getText(); System.out.println(result); } catch (AxisFault e) { e.printStackTrace(); } } public static void main(String[] args) throws Exception { my(); } }七、总结:就是那么简单!