服务端:
1、 创建一个web项目作为服务端,名为:cxf-springDemo
2、导入jar包:
3、 看src目录结构:
接口SpringService类的代码如下:
该类是提供对外调用的接口
package com.spring.service;
import javax.jws.WebService;
@WebService
public interface SpringService {
String sayHello(String name);
}
实现类SpringServiceImpl类的代码如下:
package com.spring.service.impl;
import javax.jws.WebService;
import com.spring.service.SpringService;
@WebService(endpointInterface="com.spring.service.SpringService",targetNamespace=http://service.spring.com/) //一般是包名倒写
public class SpringServiceImpl implements SpringService {
public String sayHello(String name) {
return "Hello "+name;
}
}
4、 创建spring配置文件:
ApplicationContext.xml配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<!-- START SNIPPET: beans -->
<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">
<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" />
<jaxws:endpoint id="springService"
implementor="com.spring.service.impl.SpringServiceImpl" <!—implemetor: 服务实现类 -->
address="/SpringService" /> <!—address:等会要访问服务接口的地址:http://localhost:8080/项目名/SpringService -->
</beans>
注意:其中cxf.xml \ cxf-extension-soap.xml \ cxf-servlet.xml 这三个文件在cxf.jar包中
5、 web.xml配置:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!—配置spring配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!—spring监听器 -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!—配置CXF servlet -->
<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>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
客户端:
6、 创建一个客户端项目:
7、导入所需的jar包:
8、创建SpringClient类进行测试调用接口:
public class SpringClient {
public static void main(String[] args) throws Exception {
//调用接口 方法一:
String url=http://localhost:8080/cxf-springDemo/SpringService;
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(SpringService.class);
factory.setAddress(url);
SpringService s = (SpringService)factory.create();
System.out.println(s.sayHello("哈哈!成功了"));
//调用接口 方法二: 通常用这种,不需要知道接口类
JaxWsDynamicClientFactory factory2 = JaxWsDynamicClientFactory.newInstance();
Client client = factory2.createClient(http://localhost:8080/cxf-springDemo/SpringService?wsdl);
Object[] s2 = client.invoke("sayHello", "哈哈,运行成功了!");
System.out.println(s2[0]);
}
}
7、 布署cxf-springDemo项目,启动服务器。
运行SpringClient客户端,会输出如图所示: