配置环境:
jdk1.6+Tomacat7.0.35+Spring4.14+cxf3.0.13
(注:配置相应高版本的cxf组件包需要更高一级的jdk实现)
接口服务端配置
1.引入cxf的相关依赖jar包
从Apache官网中下载的cxf包的lib中获取如下jar:
2.Web.xml中配置CXFServlet
假设SpringMVC本身的DispatcherServlet已经启用,则在第2启动顺序添加CXFServlet。并添加servlet-mapping匹配请求。
配置如下:
配置如下:
<!-- Apache CXFServlet -->
<servlet>
<servlet-name>CXFServlet</servlet-name>
<display-name>CXF Servlet</display-name>
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
<init-param>
<param-name>config-location</param-name>
<param-value>classpath:/webservice.xml</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<!-- CXFServlet Mapping -->
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>
3.配置自定义webservice.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:p="http://www.springframework.org/schema/p"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!-- cxf必要配置 -->
<import resource="classpath:META-INF/cxf/cxf.xml"/>
<import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
<bean id="surveyService" class="com.recuitment.ws.cxf.impl.SurveyService">
<property name="excludeName" value="Michael"/>
<property name="leastPonit" value="10"/>
</bean>
<!-- Expose SurveyWebService -->
<jaxws:server id="surveyWebService"
serviceClass="com.recuitment.ws.cxf.ISurveyService"
address="/surveyWebService">
<jaxws:serviceBean>
<ref bean="surveyService"/>
</jaxws:serviceBean>
</jaxws:server>
</beans>
4.编写相应接口及实现类
package com.recuitment.ws.cxf;
import javax.jws.WebService;
@WebService
public interface ISurveyService
{
/**
* @param username 名字
* @param point 分数
* @return
*/
public String vote(String username,int point);
}
5.查看相关服务发布情况
启动SpringMVC项目,根据配置文件定义,接口地址类似:
http://ip:port/项目名/ws/**
接口客户端配置
1.引入cxf的相关依赖jar包
从Apache官网中下载的cxf包的lib中获取如下jar(其中cxf.jar为服务端打包的jar):
2.增加客户端的xml文件和java文件,如下:
<?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:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<!-- 导入Apache CXF Bean -->
<import resource="classpath:META-INF/cxf/cxf.xml"/>
<import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
<jaxws:endpoint id="surveyService"
implementor="com.recuitment.ws.cxf.ISurveyService"
address="http://localhost:8080/recuitment/SurveyWebService"/>
</beans>
package com.recuitment.cxf;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import com.recuitment.ws.cxf.ISurveyService;
public class SurveyServiceClient
{
/**
* <一句话功能简述>
* <功能详细描述>
* @param argsO
* @see [类、类#方法、类#成员]
*/
public static void main(String[] args)
{
// TODO Auto-generated method stub
System.out.println(SurveyServiceClient.doSpringClient("Michael",9));
}
private static String doSpringClient(String username,int point){
ApplicationContext context = new FileSystemXmlApplicationContext("/resource/beanRefClient.xml");
ISurveyService surveyService = (ISurveyService)context.getBean("surveyServiceClient");
return surveyService.vote(username,point);
}
}