今天抽时间整理了下cxf发布webservice的具体步骤,写下来供大家参考,废话不多说,直接上贴图,上代码:
注:该此文档所有项目源码及jar包下载地址为:cxf发布webservice及客户端调用示例demo下载
一,服务端
整体项目结构如下
1,xml配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/classes/applicationContext.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<listener>
<listener-class>
org.springframework.web.context.request.RequestContextListener
</listener-class>
</listener>
<servlet>
<servlet-name>CXFServlet</servlet-name>
<display-name>CXF Servlet</display-name>
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
2,导入与CXF框架有关的xml及发布WebService接口
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
<!--导入与CXF框架有关的xml-->
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<!--发布WebService接口-->
<jaxws:endpoint id="service"
implementor="com.test.cxf.service.impl.ServiceImpl" address="/IService">
</jaxws:endpoint>
</beans>
二,客户端
try {
ServiceImplServiceLocator iService = new ServiceImplServiceLocator();
ServiceImplServiceSoapBindingStub service = (ServiceImplServiceSoapBindingStub) iService.getServiceImplPort();
String str = service.get("test");
System.out.println(str+" ---------------------");
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}