部署WebService服务(Axis2,spring)
部署WebService(Axis2和spring集成)
spring集成Axis2
server 实现
步骤一 创建Web工程
步骤二 下载Axis2二进制包
步骤三 将Axis2包加载到创建的Web工程(将Axis->lib文件夹下的jar包复制到Web工程->WebRoot->WEB-INF->lib文件夹下(还需要加上spring的核心包))
activation-1.1.jar
axiom-api-1.2.12.jar
axiom-impl-1.2.12.jar
axis2-adb-1.6.1.jar
axis2-adb-codegen-1.6.1.jar
axis2-java2wsdl-1.6.1.jar
axis2-kernel-1.6.1.jar
axis2-spring-1.5.jar
axis2-transport-http-1.6.1.jar
axis2-transport-local-1.6.1.jar
commons-codec-1.3.jar
commons-httpclient-3.1.jar
commons-logging-1.1.1.jar
dom4j-1.6.1.jar
geronimo-stax-api_1.0_spec-1.0.1.jar
httpcore-4.0.jar
mail-1.4.jar
neethi-3.0.1.jar
spring.jar
woden-api-1.0M8.jar
wsdl4j-1.6.2.jar
wstx-asl-3.2.9.jar
XmlSchema-1.4.7.jar
步骤四 在Web工程下创建一个接口并定义一个方法
例:
package com.soap.axis2;
public interface IServiceServer
{
public String sayhello(String name);
}
步骤五 新建一个类继承接口
例:
package com.soap.axis2;
public class ServiceServerImpl implements IServiceServer
{
@Override
public String sayhello(String name)
{
return "my name is "+name;
}
}
步骤六 在工程src目录下创建spring配置文件applicationContext.xml
applicationContext.xml文件内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
<bean id="SayHelloService" language="java"
%><%@
page import="org.apache.axis2.Constants,
org.apache.axis2.description.AxisOperation,
org.apache.axis2.description.AxisService,
java.util.Collection,
java.util.HashMap,
java.util.Iterator"
%><html>
<head><title>List Services</title>
<style>
h2{margin:20 0 5 0;}
ul{margin-top:5;}
</style>
</head>
<body>
<h1>Available services</h1>
<%
HashMap serviceMap = (HashMap) request.getSession().getAttribute(Constants.SERVICE_MAP);
Collection servicecol = serviceMap.values();
if(servicecol.size()==0){%>Available services is Empty.<%}
for (Iterator iterator = servicecol.iterator(); iterator.hasNext();) {
AxisService axisService = (AxisService) iterator.next();
Iterator opItr = axisService.getOperations();
String serviceName = axisService.getName();
%>
<h2><font color="blue"><a href="<%=serviceName %>?wsdl" target="_blank"><%=serviceName%></a></font></h2>
<i>Available Operations</i>
<ul>
<%
while (opItr.hasNext()) {
AxisOperation axisOperation = (AxisOperation) opItr.next();
%><li><%=axisOperation.getName().getLocalPart()%></li><%
}
%>
</ul>
<%
}
%>
</body>
</html>
步骤八 在Web工程->WebRoot->WEB-INF下新建services->service(这个目录自定义)->META-INF->services.xml目录和文件
services.xml文件内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<serviceGroup>
<service name="sayHelloService">
<description>
sayHelloService:Spring POJO Axis2 Service Sample
</description>
<parameter name="ServiceClass">
com.soap.axis2.ServiceServer
</parameter>
<parameter name="ServiceObjectSupplier">
org.apache.axis2.extensions.spring.receivers.SpringServletContextObjectSupplier
</parameter>
<parameter name="SpringBeanName">SayHelloService</parameter>
<messageReceivers>
<messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out"
/>
</messageReceivers>
<operation name="sayHello">
<messageReceiver
/>
</operation>
</service>
</serviceGroup>
步骤九 修改Web工程->WebRoot->WEB-INF->web.xml
在web.xml文件<web-app></web-app>中加上:
<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>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
发布我们新建的Web工程
打开浏览器访问:http://localhost:8080/WebService//services/listServices
会出现我们部署的服务 sayHelloService
client 实现
步骤一 在工程中新建一个类(可以重新建一个工程但需要导入Axis2的相关包)
例:
package com.soap.axis2.client;
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 ServiceClient
{
public static void main(String[] args1) throws AxisFault
{
String path = "http://localhost:8080/WebService/services/sayHelloService";
EndpointReference targetEPR = new EndpointReference(path);
RPCServiceClient serviceClient = new RPCServiceClient();
Options options = serviceClient.getOptions();
options.setTo(targetEPR);
QName opGetWeather = new QName("http://axis2.soap.com","sayhello");
Object[] response = serviceClient.invokeBlocking(opGetWeather,
new Object[]{"zhangsan"},new Class[]{String.class});
System.out.println(response[0]);
}
}
执行此类