1.环境搭建,准备相应环境jar包
axis2-1.6.0.0下载地址: http://axis.apache.org/axis2/java/core/download.cgi
spring相关jar包下载地址:
http://maven.springframework.org/release/org/springframework/spring/
2.创建web工程导入axis2和spring所需jar
3.构建axis2发布webservice服务目录结构
/WEB-INF/services/任意名称/META-INF/services.xml
4.配置文件
web.xml:
org.springframework.web.context.ContextLoaderListener
/WEB-INF/conf/applicationContext-*.xml
Axis2Servlet
org.apache.axis2.transport.http.AxisServlet
Axis2Servlet
/services/*
spring.xml:
class="org.apache.axis2.extensions.spring.receivers.ApplicationContextHolder" />
services.xml:
用户信息service
org.apache.axis2.extensions.spring.receivers.SpringAppContextAwareObjectSupplier
class="org.apache.axis2.receivers.RawXMLINOutMessageReceiver" />
5.以上配置完成启动tomcat,请求http://localhost:8080/axis2/services/UserService?wsdl如成功会看到wsdl文件的定义。
6.如需处理header头部分处理
wsdl文件header头部添加:
1):头部参数描述,如下图红色方框内代码片段
2)message节点定义,Input输入描述定义。注意对应关系
完整wsdl文件如下,红色部分为添加header处理:
UserService
7.服务端头部header处理
代码片段:
import java.util.Iterator;
import org.apache.axiom.om.impl.llom.OMElementImpl;
import org.apache.axis2.context.MessageContext;
import org.springframework.stereotype.Service;
import com.axis2.entity.UserInfo;
import com.axis2.service.UserService;
@Service("userService")
public class UserServiceImpl implements UserService
{
@Override
public int register(UserInfo user)
{
MessageContext msgContext = MessageContext.getCurrentMessageContext();
// 获取Head
Iterator list = (Iterator) msgContext.getEnvelope().getHeader()
.getFirstElement().getChildren();
while (list.hasNext())
{
Object obj = list.next();
if(obj instanceof OMElementImpl){
OMElementImpl omei = (OMElementImpl) obj;
String nodeName = omei.getLocalName();
String nodeValue = omei.getText();
}
}
//业务逻辑处理
return 0;
}
}
至此以上步骤基本完成axis2+spring发布webservice服务以及消息头处理。